Math Methods

From CompSciWiki
Revision as of 21:13, 6 December 2011 by CharuC (Talk | contribs)

Jump to: navigation, search

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.

Code

 import java.lang.Math;
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);
	}
} 
 C:\Users\umchan99\assignment files\programs a6>java Ceil
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.

 import java.lang.Math;
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);
	}
} 
 C:\Users\umchan99\assignment files\programs a6>java Floor
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.

 00  int max1;
 01  double max2;
 02
 03  max1 = Math.max(3, 2);      // you can compare integers
 04  max2 = Math.max(1.2, 3.5);  // or you can compare doubles

The result of max1 is 3 and max2 is 3.5.

min()

This method returns the smaller of the two numbers passed to the min() method.

 00  int min1;
 01  double min2;
 02
 03  min1 = Math.min(3, 2);      // you can compare integers
 04  min2 = Math.min(1.2, 3.5);  // or you can compare doubles

The result of min1 is 2 and min2 is 1.2.

pow()

This method (math.pow(x,n)) returns the result of x raised to the power of n.

 00  double power;
 01  power = Math.pow(2, 3);   // 2 to the power 3

The result of power is 8.

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:

 00  double rand;
 01  rand = Math.random();

If you need a number greater than or equal to 0.0 and less than 10.0:

00  double rand;
01  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:

 00  double rand;
 01  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:

 00  int rand;
 01  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.

 00  int n;
 01  n = Math.round(2.365);

The result of n is 2.

sqrt()

This method returns the square root of a positive number.

 00  double squareRoot;
 01  squareRoot = Math.sqrt(25);

The result of squareRoot is 5.

More Math methods and examples

For a full list of all the math methods you can visit: API Math Methods(and then scroll down to the Method Summary table)

To see an example about some Math methods you can visit: http://www.cafeaulait.org/course/week4/40.html

Previous Page: String Methods Next Page: JOptionPane Methods