Math Class methods

class jamesprogram {

	public static void main(String args[]){
		
		// abs asks how much is this away from zero?
		// i.e. turns a negative into a positive
		System.out.println(Math.abs(-23.2)); // returns 23.2
		
		// ceil rounds a number up
		System.out.println(Math.ceil(7.4)); // returns 8.0
		
		// floor rounds a number down
		System.out.println(Math.floor(7.4)); // returns 7.0
		
		// max takes multiple parameters
		// takes 2 number and returns the maximum
		System.out.println(Math.max(7.4, 6.5)); // returns 7.4
		
		// min takes multiple parameters
		// takes 2 number and returns the maximum
	    System.out.println(Math.min(7.4, 6.5)); // returns 6
	    
	    // pow takes multiple parameters
	 	// takes 2 number e.g. 2 to the 3rd power
	 	System.out.println(Math.pow(2, 3)); // returns 8
	 	
	 	// sqrt is the square root, take 1 parameter
	 	System.out.println(Math.sqrt(9)); // returns 3
	}
}

Leave a Reply