Home Segments Top Top Previous Next

124: Sidetrip

Java offers a variety of powerful built-in class methods for the Math class. The following illustrates:

public class Demonstrate { 
 public static void main (String argv[]) { 
  System.out.println("Natural logarithm of 10:    " + Math.log(10));  
  System.out.println("Absolute value of -10:      " + Math.abs(-10));  
  System.out.println("Maximum of 2 and 3:         " + Math.max(2, 3));  
  System.out.println("5th power of 6:             " + Math.pow(6, 5));  
  System.out.println("Square root of 7:           " + Math.sqrt(7));  
  System.out.println("Sin of 8 radians:           " + Math.sin(8));  
  System.out.println("Random number (0.0 to 1.0): " + Math.random());  
 } 
} 
--- Result --- 
Natural logarithm of 10:    2.302585092994046 
Absolute value of -10:      10 
Maximum of 2 and 3:         3 
5th power of 6:             7776.0 
Square root of 7:           2.6457513110645907 
Sin of 8 radians:           0.9893582466233818 
Random number (0.0 to 1.0): 0.8520107471627543