Difference between revisions of "Math Methods"

From CompSciWiki
Jump to: navigation, search
 
(12 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.
====Code====
+
 
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
import java.lang.Math;
 
 
public class Ceil
 
public class Ceil
 
{
 
{
Line 42: Line 42:
 
}
 
}
 
}}
 
}}
 +
 +
Output
  
 
{{OutputBlock
 
{{OutputBlock
|Code=C:\Users\umchan99\assignment files\programs a6>java Ceil
+
|Code=
 
num1Ceil is 7.0
 
num1Ceil is 7.0
 
num1NegCeil is -6.0
 
num1NegCeil is -6.0
 
num2Ceil is 14.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.
 +
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
import java.lang.Math;
 
 
public class Floor
 
public class Floor
 
{
 
{
Line 70: Line 74:
 
}
 
}
 
}}
 
}}
 +
 +
Output
 +
 
{{OutputBlock
 
{{OutputBlock
|Code=C:\Users\umchan99\assignment files\programs a6>java Floor
+
|Code=
 
num1Floor is 6.0
 
num1Floor is 6.0
 
num1NegFloor is -7.0
 
num1NegFloor is -7.0
 
num2Floor is 14.0
 
num2Floor is 14.0
 
}}
 
}}
===max()===
+
 
 +
===<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.
 +
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
import java.lang.Math;
 
 
public class Max
 
public class Max
 
{
 
{
Line 93: Line 101:
 
}
 
}
 
}}
 
}}
 +
 +
Output
 +
 
{{OutputBlock
 
{{OutputBlock
|Code=C:\Users\umchan99\assignment files\programs a6>java Max
+
|Code=
 
max1 is 3
 
max1 is 3
 
max2 is 3.5
 
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.
 +
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
import java.lang.Math;
 
 
public class Min
 
public class Min
 
{
 
{
 
public static void main(String args[])
 
public static void main(String args[])
 
{
 
{
int min1;
+
                int min1;
 
double min2;
 
double min2;
max1 = Math.min(3, 2); // you can compare integers
+
min1 = Math.min(3, 2); // you can compare integers
max2 = Math.min(1.2, 3.5); // or you can compare doubles
+
min2 = Math.min(1.2, 3.5); // or you can compare doubles
 
System.out.println("min 1 is " + min1 + "\nmin2 is "+ min2);
 
System.out.println("min 1 is " + min1 + "\nmin2 is "+ min2);
 
}
 
}
 
}
 
}
 
}}
 
}}
 +
 +
Output
 +
 
{{OutputBlock
 
{{OutputBlock
 
|Code=
 
|Code=
C:\Users\umchan99\assignment files\programs a6>java Min
+
min 1 is 2
max1 is 2
+
min2 is 1.2
max2 is 1.2
+
 
}}
 
}}
===pow()===
+
 
 +
===<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.
 +
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
import java.lang.Math;
 
 
public class Pow
 
public class Pow
 
{
 
{
Line 136: Line 151:
 
}
 
}
 
}}
 
}}
 +
 +
Output
 +
 
{{OutputBlock
 
{{OutputBlock
 
|Code=
 
|Code=
C:\Users\umchan99\assignment files\programs a6>java Pow
 
 
Answer is 8.0
 
Answer is 8.0
 
}}
 
}}
  
===random()===
+
===<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 178: 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.
 +
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
import java.lang.Math;
 
 
public class Round
 
public class Round
 
{
 
{
Line 193: Line 225:
 
}
 
}
 
}}
 
}}
 +
 +
Output
 +
 
{{OutputBlock
 
{{OutputBlock
 
|Code=
 
|Code=
C:\Users\umchan99\assignment files\programs a6>java Round
 
 
Rounded number is 2
 
Rounded number is 2
 
}}
 
}}
===sqrt()===
+
 
 +
===<code>sqrt</code>===
 +
 
 
This method returns the square root of a positive number.
 
This method returns the square root of a positive number.
 +
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
import java.lang.Math;
 
 
public class Sqrt
 
public class Sqrt
 
{
 
{
Line 213: Line 249:
 
}
 
}
 
}}
 
}}
 +
 +
Output
 +
 
{{OutputBlock
 
{{OutputBlock
 
|Code=
 
|Code=
C:\Users\umchan99\assignment files\programs a6>java Sqrt
 
 
Square root is 5.0
 
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