// MathDemo.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-05-05 File created. // 2005-05-30 Last modification. // This program demonstrates the use of the static mathematical // methods in class Math. class MathDemo { static final double EARTH_RADIUS_IN_KILOMETERS = 6379 ; public static void main( String[] not_in_use ) { double an_angle_in_radians = Math.toRadians( 45 ) ; double sine_of_an_angle = Math.sin( an_angle_in_radians ) ; System.out.print( "\n The sine of an angle of 45 degrees is " + sine_of_an_angle + "\n" ) ; double diameter_of_the_earth = 2 * Math.PI * EARTH_RADIUS_IN_KILOMETERS ; // Because our planet Earth is not exactly a ball, the // diameters and areas calculated below are approximate. System.out.printf( "\n Earth diameter in kilometers: %15.0f", diameter_of_the_earth ) ; System.out.printf( "\n Earth diameter in miles: %15.0f", diameter_of_the_earth / 1.6093 ) ; double surface_area_of_the_earth = 4 * Math.PI * Math.pow( EARTH_RADIUS_IN_KILOMETERS, 2 ); System.out.printf( "\n Earth area in square kilometers:%15.0f", surface_area_of_the_earth ) ; System.out.printf( "\n Earth area in square miles: %15.0f\n", surface_area_of_the_earth / Math.pow( 1.6093, 2 ) ); int a_random_integer = (int) ( Math.random() * 50 ) ; System.out.printf( "\n And here is a random integer in the range " + "from 0 to 49: " + a_random_integer + "\n\n" ) ; // THE REST OF THE LINES ARE NOT SHOWN IN THE BOOK! // Method Math.round() is able round numbers as humans do. // Otherwise, computers always round downwards. System.out.print( "\n ((int) 34.56 ) evaluates to " + ( (int) 34.56 ) + "\n Math.round( 34.56 ) evaluates to " + Math.round( 34.56 ) + "\n Math.rint( 34.56 ) evaluates to " + Math.rint( 34.56 ) + "\n" ) ; System.out.print( "\n ((int) 34.50 ) evaluates to " + ( (int) 34.50 ) + "\n Math.round( 34.50 ) evaluates to " + Math.round( 34.50 ) + "\n Math.rint( 34.50 ) evaluates to " + Math.rint( 34.50 ) + "\n" ) ; } }