Math Class


The Java Math class has many methods that allow you to perform mathematical tasks on numbers. The Math.max(x,y) method can be used to find the highest value of x and y:

 System.out.println( Math.max( 10, 20 ) );      // Output: 20

The Math.min(x,y) method can be used to find the lowest value of x and y:

 System.out.println( Math.min( 10.2, 20.5 ) );      // Output: 10.2

The Math.sqrt(x) method returns the square root of x:

 System.out.println( Math.sqrt( 38.0 ) );      // Output: 8.0

Monitor.java (multi-dimensional arrays)
// Elements of array are objects of a class Pixel.
public class Monitor {
  public static void main( String[ ] args ) {
    int  x = Integer.parseInt( args[0] );
    int  y = Integer.parseInt( args[1] );
    int  code[ ] = { 10, 20, 30, 40, 50, 60 };
    String  color[ ] = { "red", "green", "blue", "white", "black", "purple" };
    // Declaring an array of Pixels
    Pixel[ ][ ]  arr;
    // Allocating memory for 6 objects of type Pixel
    arr = new Pixel[3][2];

    for ( int i = 0; i < 3; i++ )
      for ( int j = 0; j < 2; j++ )
        arr[i][j] = new Pixel( code[i*2+j], color[i*2+j] );
    System.out.println( arr[x][y].getCode( ) + " " +  arr[x][y].getColor( ) );
  }
}

class Pixel {
  public int    code;
  public String color;

  // Constructor
  Pixel( int code1, String color1 ) {
    this.code  = code1;
    this.color = color1;
  }
  int    getCode(  ) { return code;  }
  String getColor( ) { return color; }
}
shell> java Monitor                 Output: 40 white




      “God will not look you over for medals, degrees or diplomas but for scars.”    
      ― Elbert Hubbard