Download presentation

Presentation is loading. Please wait.

Published byErika Tardiff Modified over 2 years ago

1
Java Math Class

2
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

3
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.*;

4
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 to output something Calling the Math Class Calling the function power Arguments (will work out 2 8 )

5
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? 2 8 = 256 System.out.println(Math.pow(2,8));

6
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);

7
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.*;

8
… 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);

9
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

10
Math.pow() The Math.pow() works out the power of a certain number. For example if we wish to find the answer of 2 9 we would use the following code

11
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); }

12
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

13
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); }

14
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.

15
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); }

16
ACTIVITY Using the Math functions we have leant so far Create a program that will find; 1.7 5 2.Square root -90

17
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.

18
… 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;

19
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); }

20
ACTIVITY Using the Math functions we have leant so far Coin Toss Assume that; 1.Heads = 1 2.Tails = 2 Write a program that generates a random number between 1 and 2 to represent heads and tails

21
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

22
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); }

23
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

24
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); }

25
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

26
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); }

27
Formatting values Number of decimal places to be printed

28
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

29
Placeholders The following are the placeholders we will be using %f only PlaceholderUsed for %d int, byte, short, long %ffloat %sString %cchar

30
Decimal Places When 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);

31
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); }

32
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);

33
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); }

34
Padding with 0s From 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);

35
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); }

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google