// integer division, named constants public class MathOps /** This class demonstrates using the Math class * We discussed pow, sqrt, round, random **/ { public static void main(String[] args) { System.out.println("2^120 is " + Math.pow(2,120)); // used a formula and got that your room fits 37.3 people // you want to put that into an integer variable // double gets TRUNCATED int numPeople = (int)37.99999; // type cast (forced conversion) System.out.println("numPeople is: " + numPeople); // if we want to round a number, call Math.round double grade = 88.5; System.out.println("Grade is: " + Math.round(grade)); // int num = "Hello World"; incompatible types double y=0; // widening conversion is OK in Java /* This example source code demonstrates the use of * random() method of Math class * Generating random numbers from 1 to 6 (ex: spinning a die) */ // define the range int max = 6; int min = 1; int range = max - min + 1; // generate random number from 1 to 10 int rand = (int) (Math.random()* range) + min; System.out.println("You spun: " + rand); // this is an example of losing precision with doubles double sum=0.0; sum = .1+.1+.1+.1+.1+.1+.1+.1+.1+.1; System.out.println("sum="+sum); // integer division double answer = sum/5; // division of doubles answer = 1/2; System.out.println("answer of 1/2 is: " + answer); } }