Вы находитесь на странице: 1из 28

Java

Math Class
What is the Math Class?
 The Math Class is another class that is
prepared by Java for us to use

 We use this class for mathematical


operations

 The Math Class contains many different


complex mathematical functions
Do you remember?
 When we used the external class
Scanner we had to use;

Import Java.util.*;

 To make use of the Math class, this is not


needed.

 It is automatically imported
How to use the Math Class
 You must include the class name Math
each time you wish to use the Math Class

 Example;

System.out.println(Math.pow(2,8));

Indicating you wish Calling the Math Calling the Arguments


to output Class function (will work
something power out 28)
What happens…
System.out.println(Math.pow(2,8));

 In the code above the function power is


being used

 The arguments being used are 2 and 8

 The program should output 256

 Why? 28 = 256
System.out.println(Math.pow(2,8));

Problem?
 By using the code above the result of 256
is not being saved anywhere as it is just
simply an output

 To save the result we need to use the


following code

double result = Math.pow(2,8);

System.out.println(result);
Fed up of writing Math …
 Programmers are lazy and do not enjoy
writing the same things a number of times

 We could use a java statement to avoid


this, this is known as a static import
statement (always placed before we
create a class)

import static.java.lang.Math.*;

 Once we use the static import
statement the code to use the power
function would be much shorter.

double result = pow(2,8);

 The Math keyword no longer needs to


be used
Math Class Functions
1. Math.pow() – To the power of
2. Math.sqrt() – The square root
3. Math.abs() – Outputs only positive numbers
4. Math.random() – Outputs a random number
5. Math.round() – Rounds up numbers
6. Math.ceil() – Outputs the smallest number
7. Math.floor() – Outputs the largest number
Math.pow()
 The Math.pow() works out the power of
a certain number.

 For example if we wish to find the answer


of 29 we would use the following code
import static java.lang.Math.*;
class Power {
public static void main (String args[]){
int a = 2;
int b = 9;

double p = pow(a,b);

System.out.println(p);
}} //output: 512.0
Math.sqrt()
 The Math.sqrt() function is used when we
want to find the square root of a number

 For example we want to find the square


root of 100 and 10000
import static java.lang.Math.*;
class SquareRoot {
public static void main (String args[]){
int a = 100;
int b = 10000;

double sr1 = sqrt(a);


double sr2 = sqrt(b);

System.out.println("The square root of 100


is " + sr1 + "\nThe square root of 10000 is " + sr2);
}
}
Output
The square root of 100 is 10.0
The square root of 10000 is 100.0
Math.abs()
 The Math.abs() function gives the absolute value
of the number

 The absolute value of a number is equal to the


same number without the sign.

 It is useful on calculations which require positive


numbers only

 We would use Math.abs() to find the square root


of a negative number (which cannot be done), so
first we find out the absolute value.
import static java.lang.Math.*;
class SquareRoot {
public static void main (String args[]){
double a = -20.2;
double positive = abs(a);

System.out.println(positive);

}
} // Output:20.2
Math.random()
 This functions outputs a random number
from a given group of numbers

 This could be used in many games as a


dice

 The random function works with double


data type only hence we would need to
typecast this into a int not to get decimal
numbers.

 The random function also outputs 0 as a
random number, if you wouldn’t like this
to happen you must use the +1 function

 For example you want to represent a dice


so you only want numbers from 1 to 6

int dice = (int)(Math.random()*6)+1;


import static java.lang.Math.*;
class RandomDice{
public static void main(String args[]){

int dice = (int)(random()*6)+1;

System.out.println("Player one roll "+ dice);

}
}//Output: Player one roll 3
Math.round()
 The Math.round() function results in the
closest value to the integer

 If the fraction value is 1.7, it will add 1 to


7 and output 8

 Basically the Math.round() function would


output the whole number with no
decimal
import static java.lang.Math.*;
class round{
public static void main(String args[]){

double num1 = round(1223.444);


double num2 = round(34.88);

System.out.println(num1 + "\n" +
num2);

}
}//Output: 1223.0
Math.ceil()
 The Math.ceil() also outputs decimal
numbers as whole numbers

 This is done in a different way as it will


return the largest whole number which is
not less than the number given
Math.ceil()
1. public class Guru99 {
2. public static void main(String args[]) {
3. double d1 = 84.6;
4. double d2 = 0.45;
5. System.out.println("Ceiling of '" + d1 + "' =
" + Math.ceil(d1));
6.
7. System.out.println("Floor of '" + d1 + "' = "
+ Math.floor(d1));
8.
9. System.out.println("Ceiling of '" + d2 + "' =
" + Math.ceil(d2));
10.
11.System.out.println("Floor of '" + d2 + "' =
" + Math.floor(d2));
12.
13.}
14.}
Output:
Ceiling of '84.6' = 85.0
Floor of '84.6' = 84.0
Ceiling of '0.45' = 1.0
Floor of '0.45' = 0.0
import static java.lang.Math.*;
class ceil{
public static void main(String args[]){

double num1 = ceil(10.1);


double num2 = ceil(-43.4);

System.out.println(num1 + "\n" + num2);

}
} //Output : 11.0
-43.0
Math.floor()
 The Math.floor() does the exact opposite
to Math.ceil()

 The whole number would be the next


smallest number possible

 For example;
◦ 13.3 would result in 13
◦ -11.5 would result in -12
import static java.lang.Math.*;
class floor{
public static void main(String args[]){

double num1 = floor(10.6);


double num2 = floor(-43.4);

System.out.println(num1 + "\n" + num2);

}
}// 10.0
-46.0

Вам также может понравиться