How to use Predefined Method
Submitted by GeePee on Saturday, May 16, 2015 - 00:20.
In this tutorial, you will learn on how to use Predefined Method. In java, predefined methods are organized as a collection of classes, called class libraries. For example, the class Math contains mathematical methods. The method type is the data type of the value returned by the method. The class Math is contained in the package
Sample run:
outputs the uppercase letter that corresponds to ‘a’, which is ‘A’.
In the statement , the method pow (of the class Math) is used to output u. The method uses the method outputs the value of x.
java.lang
. I will be using the JCreator IDE in developing the program.
To start in this tutorial, first open the JCreator IDE, click new and paste the following code.
- public class PredefinedMethod
- {
- {
- int x;
- double u, v;
- + toUpperCase('a'));
- u = 4.2;
- v = 3.0;
- + "%.1f = %.2f%n", u, v, pow(u,v));
- + "%.2f%n", pow(5, 4));
- x = -15;
- + "%d%n", x, abs(x));
- }
- }
Uppercase is A
4.2 to the power of 3.0 = 74.09
5 to the power of 4 = 625.00
u = 31.20
The absolute value of -15=15
This program works as follows:
The statement - + toUpperCase('a'));
- + "%.1f = %.2f%n", u, v, pow(u,v));
pow
is called with the parameters u and v. in this case, the values of u and v are passed to the method pow
The statement - + "%.2f%n", pow(5, 4));
pow
to output 5 to the power of 4.
The statement u = u + Math.pow(3, 3);
uses the methodpow
to determine 3 to the power of 3, adds this value to the value of u, and then stores the new value into u.
The statement System.out.printf("u = %.2f%n", u);
outputs the value of u.
The statement x = -15;
stores -15 to x, and the statement - + "%d%n", x, abs(x));
Add new comment
- 175 views