How to use Predefined Method

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 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.
  1. import static java.lang.Math.*;
  2. import static java.lang.Character.*;
  3.  
  4. public class PredefinedMethod
  5. {
  6. public static void main(String[] args)
  7. {
  8.         int x;
  9.         double u, v;
  10.                
  11.         System.out.println("Uppercase a is "
  12.                 + toUpperCase('a'));
  13.                        
  14.                 u = 4.2;
  15.                 v = 3.0;
  16.                        
  17.                 System.out.printf("%.1f to the power of "      
  18.                 + "%.1f = %.2f%n", u, v, pow(u,v));
  19.                                        
  20.                 System.out.printf("5 to the power of 4 = "
  21.                                 + "%.2f%n", pow(5, 4));
  22.                                
  23.                 u = u + Math.pow(3, 3);
  24.                 System.out.printf("u = %.2f%n", u);
  25.                        
  26.                 x = -15;
  27.                 System.out.printf("The absoloute value of %d = "
  28.                         + "%d%n", x, abs(x));
  29.                 }
  30.  
  31.  
  32. }
Sample run: 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
  1.  System.out.println("Uppercase a is "
  2.                         + toUpperCase('a'));
outputs the uppercase letter that corresponds to ‘a’, which is ‘A’. In the statement
  1.  System.out.printf("%.1f to the power of "     
  2.                                         + "%.1f = %.2f%n", u, v, pow(u,v));
, the method pow (of the class Math) is used to output u. The method 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
  1.  System.out.printf("5 to the power of 4 = "
  2.                                         + "%.2f%n", pow(5, 4));
uses the method 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
  1.  System.out.printf("The absoloute value of %d = "
  2.                                         + "%d%n", x, abs(x));
outputs the value of x.

Add new comment