Java Reference
In-Depth Information
Suppose that there are two class es Test1 and Test2 . Both classes contain the
static method printAll , and you want to use these classes in a program. To
correctly use the method printAll , you should call this method using the name of the
class and the dot operators.
The ( public ) static methods of the class Character have similar conventions.
Example 7-1 illustrates how to use predefined methods.
EXAMPLE 7-1
This example shows you how to use some of the predefined methods:
//How to use the predefined methods
import static java.lang.Math.*;
import static java.lang.Character.*;
public class PredefinedMethods
{
public static void main(String[] args)
{
int x;
double u;
double v;
System.out.println("Line 1: Uppercase a is "
+ toUpperCase('a'));
//Line 1
u = 4.2;
//Line 2
v = 3.0;
//Line 3
System.out.printf("Line 4: %.1f to the power "
+ "of %.1f = %.2f%n",
u, v, pow(u, v));
//Line 4
System.out.printf("Line 5: 5 to the power of "
+ "4 = %.2f%n", pow(5, 4));
//Line 5
u = u + Math.pow(3, 3);
//Line 6
System.out.printf("Line 7: u = %.2f%n", u);
//Line 7
x = -15;
//Line 8
System.out.printf("Line 9: The absolute value "
+ "of %d = %d%n", x, abs(x));
//Line 9
}
}
 
Search WWH ::




Custom Search