Math Methods

From CompSciWiki
Revision as of 15:39, 20 March 2007 by Umelazza (Talk | contribs)

Jump to: navigation, search

COMP 1010 Home > Calling Methods


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.

   

{{{Body}}}

You only need to call the method using the following general statement:

 Type variableName = Math.methodName(argumentList);
 
 Type         = double, float, or int.
 variableName = any name that suites your purpose.
 methodName   = round, min, sqrt, etc.
 argumentList = could be zero or more parameter(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.

Most used math methods (at least in COMP 1010):

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.

random()

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.

if you want to get a random number between 0.0 and 1.0:

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

If you need numbers between 1.0 and 11.0:

 00  double rand;
 01  rand = Math.random() * 10; // the number 10 increases the range to 10 numbers instead (0,1) range.

(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:

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

For example, you want the numbers from 22 to 32:

 00  int rand;
 01  rand = (int)(Math.random() * 10) + 22; // 

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.

The formula for getting a random number between L (low number) and H (high number) inclusive is:

(int)((Math.random() * (H - L + 1)) + L)

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.

max()

This method returns the maximum number of the two numbers passed to the min() 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 min1 is 3 and min2 is 3.5.

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.

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