Difference between revisions of "Math Methods"

From CompSciWiki
Jump to: navigation, search
 
(18 intermediate revisions by 4 users not shown)
Line 20: Line 20:
 
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.
 
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()===
+
 
 +
===<code>ceil</code>===
 
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.
 
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.
  
  00  double num1 = 6.3 ;
+
{{CodeBlock
  01  double num2 = 14.0 ;
+
|Code=
  02  double num1Ceil ;
+
public class Ceil
  03  double num1NegCeil ;
+
{
  04  double num2Ceil ;
+
public static void main(String args[])
  05
+
{
  06  num1Ceil = Math.ceil(num1) ;      <font color="green">// rounds up to 7.0 </font>
+
double num1 = 6.3 ;
  07  num1NegCeil = Math.ceil(-num1) ;  <font color="green">// rounds up to -6.0</font>
+
double num2 = 14.0 ;
  08  num2Ceil = Math.ceil(num2) ;      <font color="green">// stays at 14.0</font>
+
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
 +
 
 +
{{OutputBlock
 +
|Code=
 +
num1Ceil is 7.0
 +
num1NegCeil is -6.0
 +
num2Ceil is 14.0
 +
}}
 +
 
  
===floor()===
+
===<code>floor</code>===
 
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.
 
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.
  
  00  double num1 = 6.9 ;
+
{{CodeBlock
  01  double num2 = 14.0 ;
+
|Code=
  02  double num1Floor ;
+
public class Floor
  03  double num1NegFloor ;
+
{
  04  double num2Floor ;
+
public static void main(String args[])
  05
+
{
  06  num1Floor = Math.floor(num1) ;      <font color="green">// rounds down to 6.0 </font>
+
double num1 = 6.9 ;
  07  num1NegFloor = Math.floor(-num1) ;  <font color="green">// rounds down to -7.0</font>
+
double num2 = 14.0 ;
  08  num2Floor = Math.floor(num2) ;      <font color="green">// stays at 14.0</font>
+
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);
 +
}
 +
}
 +
}}
  
===max()===
+
Output
 +
 
 +
{{OutputBlock
 +
|Code=
 +
num1Floor is 6.0
 +
num1NegFloor is -7.0
 +
num2Floor is 14.0
 +
}}
 +
 
 +
===<code>max</code>===
 
This method returns the maximum number of the two numbers passed to the max() method.
 
This method returns the maximum number of the two numbers passed to the max() method.
  
  00  int max1;
+
{{CodeBlock
  01  double max2;
+
|Code=
  02
+
public class Max
  03  max1 = Math.max(3, 2);     <font color="green">// you can compare integers</font>
+
{
  04  max2 = Math.max(1.2, 3.5); <font color="green">// or you can compare doubles</font>
+
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
  
The result of '''max1''' is '''3''' and '''max2''' is '''3.5'''.
+
{{OutputBlock
 +
|Code=
 +
max1 is 3
 +
max2 is 3.5
 +
}}
  
===min()===
+
===<code>min</code>===
 
This method returns the smaller of the two numbers passed to the min() method.
 
This method returns the smaller of the two numbers passed to the min() method.
  
  00  int min1;
+
{{CodeBlock
  01  double min2;
+
|Code=
  02
+
public class Min
  03  min1 = Math.min(3, 2);     <font color="green">// you can compare integers</font>
+
{
  04  min2 = Math.min(1.2, 3.5); <font color="green">// or you can compare doubles</font>
+
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);
 +
}
 +
}
 +
}}
  
The result of '''min1''' is '''2''' and '''min2''' is '''1.2'''.
+
Output
  
===pow()===
+
{{OutputBlock
 +
|Code=
 +
min 1 is 2
 +
min2 is 1.2
 +
}}
 +
 
 +
===<code>pow</code>===
 
This method ('''math.pow(x,n)''') returns the result of x raised to the power of n.
 
This method ('''math.pow(x,n)''') returns the result of x raised to the power of n.
  
  00  double power;
+
{{CodeBlock
  01  power = Math.pow(2, 3);  <font color="green">// 2 to the power 3</font>
+
|Code=
 +
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);
 +
}
 +
}
 +
}}
  
The result of '''power''' is '''8'''.
+
Output
  
===random()===
+
{{OutputBlock
 +
|Code=
 +
Answer is 8.0
 +
}}
 +
 
 +
===<code>random</code>===
 
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.  
 
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:
 
If you want to get a random number greater than or equal to 0.0 and less than 1.0:
<pre>
+
{{CodeBlock
00  double rand;
+
|Code=
01  rand = Math.random();
+
double rand;
</pre>
+
rand = Math.random();
 +
}}
 +
 
 
If you need a number greater than or equal to 0.0 and less than 10.0:
 
If you need a number greater than or equal to 0.0 and less than 10.0:
  
00  double rand;
+
{{CodeBlock
01  rand = Math.random() * 10; <font color="green">// the number 10 increases the range to 0.0 - 10.0 instead of the normal 0.0 - 1.0 range.</font>
+
|Code=
 +
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.  
 
(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:
 
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:
<pre>
+
 
00  double rand;
+
{{CodeBlock
01  rand = Math.random() * 15;
+
|Code=
</pre>
+
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:
 
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:
<pre>
+
 
 +
{{CodeBlock
 +
|Code=
 
(int)(Math.random() * (H - L + 1)) + L
 
(int)(Math.random() * (H - L + 1)) + L
</pre>
+
}}
 +
 
 
L and H must be '''ints'''.
 
L and H must be '''ints'''.
  
 
If, for example, you want the numbers from 22 to 32:
 
If, for example, you want the numbers from 22 to 32:
<pre>
+
 
00  int rand;
+
{{CodeBlock
01  rand = (int)(Math.random() * 11) + 22;  
+
|Code=
</pre>
+
int rand;
 +
rand = (int)(Math.random() * 11) + 22;  
 +
}}
 +
 
 
Line 01 uses '''(int)''' to cast the double to an integer result.
 
Line 01 uses '''(int)''' to cast the double to an integer result.
  
Line 112: Line 209:
 
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.
 
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()===
+
===<code>round</code>===
 +
 
 
This method returns the argument passed (number) to the closest int.
 
This method returns the argument passed (number) to the closest int.
  
  00  int n;
+
{{CodeBlock
  01  n = Math.round(2.365);
+
|Code=
 +
public class Round
 +
{
 +
public static void main(String args[])
 +
{
 +
long n;
 +
  n = Math.round(2.365);
 +
  System.out.println("Rounded number is " + n);
 +
}
 +
}
 +
}}
 +
 
 +
Output
 +
 
 +
{{OutputBlock
 +
|Code=
 +
Rounded number is 2
 +
}}
  
The result of '''n''' is '''2'''.
+
===<code>sqrt</code>===
  
===sqrt()===
 
 
This method returns the square root of a positive number.
 
This method returns the square root of a positive number.
  
  00  double squareRoot;
+
{{CodeBlock
  01  squareRoot = Math.sqrt(25);
+
|Code=
 +
public class Sqrt
 +
{
 +
public static void main(String args[])
 +
{
 +
double squareRoot;
 +
  squareRoot = Math.sqrt(25);
 +
  System.out.println("Square root is "+squareRoot);
 +
}
 +
}
 +
}}
  
The result of '''squareRoot''' is '''5'''.
+
Output
 +
 
 +
{{OutputBlock
 +
|Code=
 +
Square root is 5.0
 +
}}
  
 
===More Math methods and examples===
 
===More Math methods and examples===
For a full list of all the math methods you can visit: [http://java.sun.com/javase/6/docs/api/java/lang/Math.html API Math Methods](and then scroll down to the Method Summary table)
+
For a full list of all the math methods you can visit: [http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html Math API] and then scroll down to the Method Summary table.
  
To see an example about some Math methods you can visit:
+
To see more examples of Math methods you can visit:
 
http://www.cafeaulait.org/course/week4/40.html
 
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: <code>ceil, floor, max, min, pow, random, round and sort</code>.
 
}}
 
}}

Latest revision as of 15:40, 18 January 2012

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