//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
}
}
Sample Output:
Line 1: Uppercase a is A
Line 4: 4.2 to the power of 3.0 = 74.09
Line 5: 5 to the power of 4 = 625.00
Line 7: u = 31.20
Line 9: The absolute value of -15 = 15
This program works as follows: The statement in Line 1 outputs the uppercase letter that
corresponds to 'a', which is 'A'. In the statement in Line 4, themethod pow (of the class
Math) is used to output uv. In Java terminology, it is said that the method pow is called with the
(actual) parameters u and v. In this case, the values of u and v are passed to the method pow.
The statement in Line 5 uses the method pow to output 54. The statement in Line 6 uses the method
pow to determine 33, adds this value to the value of u, and then stores the new value into u.
Notice that in this statement, the method pow is called using the name of the class, which is
Math, and the dot operator. The statement in Line 7 outputs the value of u. The statement in
Line 8 stores -15 into x, and the statement in Line 9 outputs the absolute value of x.
No comments:
Post a Comment