The Math Class Using various math class methods. Formatting the values. The Math class is used for mathematical operations; in our case some of its functions will be used. In order to use the Math class, there is no need to import the class because it is automatically imported (by the java.lang package). The functions covered are the following: Method Description static double pow (double y, double x) Returns y raised to the x static double sqrt (double arg) Returns the square root of arg static int abs (int arg) Returns the absolute value of arg static double ceil (double arg) Returns the smallest whole number greater than or equal to arg static double floor (double arg) Returns the largest whole number less than or equal to arg static int round (float arg) Returns arg rounded up to the nearest int static long round (double arg) Returns arg rounded up to the nearest long static double random() Returns a random number between 0 and 1 The class name Math must be included each time the method is used. For instance: System.out.println(Math.pow(2,8)); The Math class Function Name Arguments Ms.P.Sullivan Page 1 of 6
In the previous example the Math class is being called and the pow method is called with the arguments of 2 and 8. This means that 2 to the power of 8 is worked out which will result in 256. If the result must be stored in a variable the statement can be changed to double result = Math.pow(2,8); Having to use the class name Math every time a function is required might be tedious, so it can be avoided by using the static import statement at the beginning (before the class declaration) import static.java.lang.math.*; and then the statement can be coded as double result = pow(2,8); So, you can choose whichever method you prefer, either use the import at the beginning or add the class name Math to each method you re using. Math.pow() import static java.lang.math.*; class Power { public static void main (String args[]){ int a = 5; int b = 6; System.out.println(pow(a,b)); This program will work out 5 to the power of 6 and it will display 15625. Math.sqrt() import static java.lang.math.*; class SquareRoot { public static void main (String args[]){ int a = 100; System.out.println(sqrt(a)); The above program will work out the square root of 100 and so it will result in 10. Ms.P.Sullivan Page 2 of 6
Maths.abs() The absolute value of a number is equal to the same number without the sign. It can be useful on calculations which require positive numbers only; for instance finding the square root of a negative number cannot be done, so first you work out the absolute value. import static java.lang.math.*; class SquareRoot { public static void main (String args[]){ double a = -5.4; System.out.println(abs(a)); This program will return the value of 5.4 since the negative symbol is dropped. Math.random() A random number is a computer generated number picked up from a set of numbers. random() returns a double number ranging from.00000 0 to.99999 9. This can be ideal if randomly generated numbers are required, such as rolling a dice or picking a number for the lotto. For instance, to generate a random number from 0 to 9 (inclusive) the following code can be used: int num = (int)(math.random() * 10); if the number required must be from 1 to 10, +1 must be added at the end. int num = (int)(math.random() * 10)+1; The Math.random() method returns a double number so the it must be type casted into an integer number by using (int). Randomly generated numbers start from 0, so if these must be started from 1, a +1 must be added at the end. For instance to generate a random number from 1 to 6, like a dice this code is required: int die = (int)(math.random()*6)+1; This will generate a random number from 1 to 6. Ms.P.Sullivan Page 3 of 6
Math.round() This method returns the closest value to the integer value of the number. If the fraction value is equal to or more than 0.5, it will add 1 to the original value. Math.round() will remove the fraction and just return the rounded whole number. It can accept a double value and return a long, or accept a float value and return an integer. For example double num1 = Math.round(1234.54543423232); will return 1235, whilst double num1 = Math.round(1234.44543423232); will return 1234. Math.ceil() This will return the smallest integer number which is not less than the supplied number. Method Result Math.ceil(11); 11 Math.ceil(11.3); 12 Math.ceil(-13.4); -13 Math.floor() This will return the largest integer number which is not greater than the supplied number. Method Result Math.floor(11); 11 Math.floor(11.3); 11 Math.floor(-13.4); -14 Ms.P.Sullivan Page 4 of 6
Formatting values printf() To format text a specified number of decimal places printf() can be used instead of println(). When using printf(), the placeholders must be specified inside the message (inside the quotes), followed by the variable. For instance: int num = 5; System.out.printf( %d\n,num); In this case what happens is 5 will be displayed on a line, then the cursor will move to the next line. This happens because the %d is replaced with the value of num and then the escape character \n is applied. There are different placeholders for different data types: Placeholder Used for %d int, byte, short, long %f float %s String %c char When using %f the number of decimal places can be specified as well. For instance: System.out.printf( Formatted to 2 decimal places :%.2f\n,ans); A field can also be used to hold a number. In this example a field of size 10 is created to display the number: System.out.printf( Formatted to 2 decimal places :%10.2f\n,ans); Finally, if the empty spaces are to filled in with zeros it can be coded as: System.out.printf( Formatted to 2 decimal places :%010.2f\n,ans); Ms.P.Sullivan Page 5 of 6
Activities 1. Develop an application naming it PowerOf to ask the user to input a number between 2 and 10 and then output: a. The number inputted to the power of 3 b. The number inputted to the power of 5 2. Write an application named FindSquareRt to output the square root of +144 and also the square root of -25. (Note: you have to use abs() in the case of -25. Why?) 3. Create an application to simulate the Super Five lottery. That is you need to generate one number between 1 and 43. 4. Lotto draws can generate a number between 1 and 90. Create an application that generates one number for the lotto draw. 5. Create a class naming it MyRound to round and output the following values to integers: a. 24.244 b. 24.566 c. 24.999 *** Ms.P.Sullivan Page 6 of 6