java math class. what is the math class? the math class is another class that is prepared by java...
TRANSCRIPT
JavaMath 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;
To make use of the Math class, this is not needed.
It is automatically imported
Import Java.util.*;
How to use the Math ClassYou 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 to output something
Calling the Math Class
Calling the function power
Arguments (will work out 28)
What happens…
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
System.out.println(Math.pow(2,8));
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.
The Math keyword no longer needs to be used
double result = pow(2,8);
Math Class Functions1. Math.pow() – To the power of2. Math.sqrt() – The square root 3. Math.abs() – Outputs only positive numbers4. Math.random() – Outputs a random number5. Math.round() – Rounds up numbers 6. Math.ceil() – Outputs the smallest number7. 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);}
}
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);
}}
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);
}}
ACTIVITY Using the Math functions we have leant so far
Create a program that will find;1. 75
2. Square root -90
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);
}}
ACTIVITY Using the Math functions we have leant so far
Coin Toss Assume that;
1. Heads = 12. Tails = 2
Write a program that generates a random number between 1 and 2 to represent heads and tails
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);
}}
Math.ceil()The Math.ceil() also outputs decimal
numbers as whole numbers
This is done in a different way as it will return the smallest whole number which is not less than the number given
For example;◦13.3 would result in 14◦ -11.5 would result in -11
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);
}}
Math.floor()The Math.floor() does the exact
opposite to Math.ceil()
The whole number would be the next largest 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.1);double num2 = floor(-43.4);
System.out.println(num1 + "\n" + num2);
}}
Formatting values Number of decimal places to be printed
Output of Results So far we should know how to
use;1. print()2. println()
Now we will be using printf() which is used to determine how many decimal places we would like our result to have
PlaceholdersThe following are the
placeholders we will be using %f only
Placehold
er
Used for
%d int, byte, short, long
%f float
%s String
%c char
Decimal PlacesWhen using %f the number of
decimal places can be specified
What happens?1. %.2f = determines that you want 2
decimal places (replaces the actual answer)
2. \n is used to display the actual answer on the “next line”
double num1 = 234.8889;double num2 = 56.99058;double ans = num1+num2;
System.out.printf(“Formatted to 2 decimal places :%.2f\n”,ans);
import static java.lang.Math.*;class PrintFDemo {
public static void main (String args[]) {
double num1 = 234.8889;double num2 = 56.99058;double ans =
num1+num2;
System.out.printf("Formatted to 2 decimal places :%.2f\n",ans);
}}
Field Size A field can also be used to hold a
specific number. This means how many characters are to be held
For example we want to have a field size of 8 this mean only 20 numbers can be held
double num1 = 234.8889;double num2 = 56.99058;double ans = num1+num2;System.out.printf(“Formatted to field size 20:%20.3f\
n”,ans);
import static java.lang.Math.*;class feild {
public static void main (String args[]) {double num1 = 234.8889;double num2 = 56.99058;double ans = num1+num2;
System.out.printf("Formatted to 3 decimal places and a field size of 20 :%20.3f\n",ans);
}}
Padding with 0sFrom the output above we would
see that there are many empty spaces before the number in order to use the 20 field spaces
We might wish to pad (fill) these spaces with 0s all you have to do is add a 0 in front of your placeholder double num1 = 234.8889;
double num2 = 56.99058;double ans = num1+num2;System.out.printf(“Formatted to field size 20:%020.3f\
n”,ans);
import static java.lang.Math.*;class feild {
public static void main (String args[]) {double num1 = 234.8889;double num2 = 56.99058;double ans = num1+num2;
System.out.printf("Formatted to 3 decimal places and a field size of 20 :%020.3f\n",ans);
}}