Difference between revisions of "Math Methods"

From CompSciWiki
Jump to: navigation, search
(random())
 
(36 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{1010Topic|Introduction=Math methods are very interesting. You can use them to help you in many applications that deal with numbers (e.g. banking system, lottery, cashier application, etc.). This section is introduced to help you understand how built-in math methods (in API) can be used within your code. These methods are very simple to use so there is no need to panic.|Overview=You will learn how to use simple API Math Methods|Chapter_TOC=[[Calling Methods]]}}
+
{{Template:1010Topic
 +
|Chapter_TOC=[[Calling Methods]]
 +
|Previous=[[String Methods]]
 +
|Next=[[JOptionPane Methods]]
 +
|Body=
  
You only need to call the method using the following general statement:
+
==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 variableName = Math.methodName(argumentList);
 
    
 
    
 
   '''Type'''        = double, float, or int.
 
   '''Type'''        = double, float, or int.
   '''variableName''' = any name that suites your purpose.
+
   '''variableName''' = any name that suits your purpose.
 
   '''methodName'''  = round, min, sqrt, etc.
 
   '''methodName'''  = round, min, sqrt, etc.
   '''argumentList''' = could be zero or more parameter(s).
+
   '''argumentList''' = could be zero or more argument(s).
  
Practice after each method introduced to improve your skills. You just need to know that the more you practice the method, the easier they will be.
+
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.
  
Most used math methods (at least in COMP 1010):
 
  
===round()===
+
===<code>ceil</code>===
This method returns the argument passed (number) to the closest int.
+
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  int n;
+
{{CodeBlock
  01 n = Math.round(2.365);
+
|Code=
 +
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);
 +
}
 +
}
 +
}}
  
The result of '''n''' is '''2'''.
+
Output
  
===random()===
+
{{OutputBlock
This method returns a random double number between 0 and 1. It could be used to specify a range of numbers that you want your program to work with.  
+
|Code=
 +
num1Ceil is 7.0
 +
num1NegCeil is -6.0
 +
num2Ceil is 14.0
 +
}}
  
if you want to get a random number between 0.0 and 1.0:
 
<pre>
 
00  double rand;
 
01  rand = Math.random();
 
</pre>
 
If you need numbers between 1.0 and 11.0:
 
<pre>
 
00  double rand;
 
01  rand = Math.random() * 10; // the number 10 increases the range to 10 numbers instead (0,1) range.
 
</pre>
 
(Math.random() * n): n depends on the amount of numbers you require for the program.
 
if you use n to be 15, then your range will be between 1.0 and 16.0:
 
<pre>
 
00  double rand;
 
01  rand = Math.random() * 15;
 
</pre>
 
For example, you want the numbers from 22 to 32:
 
<pre>
 
00  int rand;
 
01  rand = (int)(Math.random() * 10) + 22; //
 
</pre>
 
Line 01 uses '''(int)''' to cast the double to int (be able to get an integer result).
 
  
you can use this method to pick a lucky number for a lottory application.
+
===<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.
  
The formula for getting a random number between L (low number) and H (high number) inclusive is:
+
{{CodeBlock
<pre>
+
|Code=
(int)((Math.random() * (H - L + 1)) + L)
+
public class Floor
</pre>
+
{
 +
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);
 +
}
 +
}
 +
}}
  
===min()===
+
Output
This method returns the smaller of the two numbers passed to the min() method.
+
  
  00  int min1;
+
{{OutputBlock
  01  double min2;
+
|Code=
  02
+
num1Floor is 6.0
  03  min1 = Math.min(3, 2);      <font color="green">// you can compare integers</font>
+
num1NegFloor is -7.0
  04  min2 = Math.min(1.2, 3.5);  <font color="green">// or you can compare doubles</font>
+
num2Floor is 14.0
 +
}}
  
The result of '''min1''' is '''2''' and '''min2''' is '''1.2'''.
+
===<code>max</code>===
 +
This method returns the maximum number of the two numbers passed to the max() method.
  
===max()===
+
{{CodeBlock
This method returns the maximum number of the two numbers passed to the min() method.
+
|Code=
 +
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);
 +
}
 +
}
 +
}}
  
  00  int max1;
+
Output
  01  double max2;
+
  02
+
  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>
+
  
The result of '''min1''' is '''3''' and '''min2''' is '''3.5'''.
+
{{OutputBlock
 +
|Code=
 +
max1 is 3
 +
max2 is 3.5
 +
}}
  
===pow()===
+
===<code>min</code>===
 +
This method returns the smaller of the two numbers passed to the min() method.
 +
 
 +
{{CodeBlock
 +
|Code=
 +
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
 +
 
 +
{{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
 +
 
 +
{{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.
 +
 
 +
If you want to get a random number greater than or equal to 0.0 and less than 1.0:
 +
{{CodeBlock
 +
|Code=
 +
double rand;
 +
rand = Math.random();
 +
}}
 +
 
 +
If you need a number greater than or equal to 0.0 and less than 10.0:
 +
 
 +
{{CodeBlock
 +
|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.
 +
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:
 +
 
 +
{{CodeBlock
 +
|Code=
 +
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:
 +
 
 +
{{CodeBlock
 +
|Code=
 +
(int)(Math.random() * (H - L + 1)) + L
 +
}}
 +
 
 +
L and H must be '''ints'''.
 +
 
 +
If, for example, you want the numbers from 22 to 32:
 +
 
 +
{{CodeBlock
 +
|Code=
 +
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.
 +
 
 +
===<code>round</code>===
 +
 
 +
This method returns the argument passed (number) to the closest int.
 +
 
 +
{{CodeBlock
 +
|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
 +
}}
 +
 
 +
===<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