Math Methods
COMP 1010 Home > Calling Methods
Introduction
Math methods are interesting. You can use them in many applications that deal with numbers (e.g. banking system, lottery, cashier application, etc.). This section will help you understand how the built-in Java math methods can be used in your code.
Methods
These methods are called using this general style:
Type variableName = Math.methodName(argumentList); Type = double, float, or int. variableName = any name that suits your purpose. methodName = round, min, sqrt, etc. argumentList = could be zero or more argument(s).
Practice using each method as they are introduced to improve your skills. The more you practice each method, the easier it will be to use.
ceil
This method rounds a double up to the nearest integer value. The result is still of type double, and would need to be cast to an int if an int is needed.
public class Ceil { public static void main(String args[]) { double num1 = 6.3 ; double num2 = 14.0 ; double num1Ceil ; double num1NegCeil ; double num2Ceil ; num1Ceil = Math.ceil(num1) ; // rounds up to 7.0 </font> num1NegCeil = Math.ceil(-num1) ; // rounds up to -6.0</font> num2Ceil = Math.ceil(num2) ; // stays at 14.0</font> System.out.println("num1Ceil is " + num1Ceil + "\nnum1NegCeil is " + num1NegCeil + "\nnum2Ceil is " + num2Ceil); } }
Output
num1Ceil is 7.0 num1NegCeil is -6.0 num2Ceil is 14.0
floor
This method rounds a double down to the nearest integer value. The result is still of type double, and would need to be cast to an int if an int is needed.
public class Floor { public static void main(String args[]) { double num1 = 6.9 ; double num2 = 14.0 ; double num1Floor ; double num1NegFloor ; double num2Floor ; num1Floor = Math.floor(num1) ; // rounds down to 6.0 </font> num1NegFloor = Math.floor(-num1) ; // rounds down to -7.0</font> num2Floor = Math.floor(num2) ; // stays at 14.0</font> System.out.println("num1Floor is " + num1Floor + "\nnum1NegFloor is " + num1NegFloor + "\nnum2Floor is "+ num2Floor); } }
Output
num1Floor is 6.0 num1NegFloor is -7.0 num2Floor is 14.0
max
This method returns the maximum number of the two numbers passed to the max() method.
public class Max { public static void main(String args[]) { int max1; double max2; max1 = Math.max(3, 2); // you can compare integers max2 = Math.max(1.2, 3.5); // or you can compare doubles System.out.println("max 1 is " + max1 + "\nmax2 is "+ max2); } }
Output
max1 is 3 max2 is 3.5
min
This method returns the smaller of the two numbers passed to the min() method.
public class Min { public static void main(String args[]) { int min1; double min2; min1 = Math.min(3, 2); // you can compare integers min2 = Math.min(1.2, 3.5); // or you can compare doubles System.out.println("min 1 is " + min1 + "\nmin2 is "+ min2); } }
Output
min 1 is 2 min2 is 1.2
pow
This method (math.pow(x,n)) returns the result of x raised to the power of n.
public class Pow { public static void main(String args[]) { double power; power = Math.pow(2, 3); // 2 to the power 3 System.out.println("Answer is "+power); } }
Output
Answer is 8.0
random
This method returns a random double number which is greater than 0.0 and less than 1.0. It can be used to specify a range of numbers that you want your program to work with.
If you want to get a random number greater than or equal to 0.0 and less than 1.0:
double rand; rand = Math.random();
If you need a number greater than or equal to 0.0 and less than 10.0:
double rand; rand = Math.random() * 10; // the number 10 increases the range to 0.0 - 10.0 instead of the normal 0.0 - 1.0 range.
(Math.random() * n): n depends on the amount of numbers you require for the program. For example, if you choose n to be 15, then your range will be greater than or equal to 0.0 and less than 15.0:
double rand; rand = Math.random() * 15;
Most of the time, you will be using a randomly generated int instead of the double which is returned by Math.random(). The formula for getting a random int (integer) between L (low number) and H (high number) inclusive is:
(int)(Math.random() * (H - L + 1)) + L
L and H must be ints.
If, for example, you want the numbers from 22 to 32:
int rand; rand = (int)(Math.random() * 11) + 22;
Line 01 uses (int) to cast the double to an integer result.
Notice that 11 is the result of 32 - 22 + 1.
Random has many applications. You could use it in a dice game, or you can use it to pick a lucky number for a lottery application.
round
This method returns the argument passed (number) to the closest int.
public class Round { public static void main(String args[]) { long n; n = Math.round(2.365); System.out.println("Rounded number is " + n); } }
Output
Rounded number is 2
sqrt
This method returns the square root of a positive number.
public class Sqrt { public static void main(String args[]) { double squareRoot; squareRoot = Math.sqrt(25); System.out.println("Square root is "+squareRoot); } }
Output
Square root is 5.0
More Math methods and examples
For a full list of all the math methods you can visit: Math API and then scroll down to the Method Summary table.
To see more examples of Math methods you can visit: http://www.cafeaulait.org/course/week4/40.html
Summary
In this section, we learnt how to use the various Math methods in Java. Using these Math methods can help you perform arithmetical and other mathematical operations on numbers in Java. Particularly, we went through the Math methods: ceil, floor, max, min, pow, random, round and sort
.
Previous Page: String Methods | Next Page: JOptionPane Methods |