Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Math Class Methods Utilizing the Important Math Operations of Java!

Similar presentations


Presentation on theme: "The Math Class Methods Utilizing the Important Math Operations of Java!"— Presentation transcript:

1 The Math Class Methods Utilizing the Important Math Operations of Java!

2 The Java Math Class Java provides a Math class to make it easier to perform certain arithmetic operations. No one should have to stop and write Math methods to calculate the square root of something or raise a base to a power or any of the myriad of arithmetic operations that a programmer might need. You don’t need to import anything to use Math class methods. It is a class in the java.lang package and is automatically available to any Java program.

3 The Math Class Subset of Methods There are many methods in the Math class if you look up its API on line. However, we only want to be familiar with a few methods of the Math class and the remainder of them you can look in the Math class API. The most used methods and what they do are listed on the next slide.

4 The AP Subset of Math Class Methods Math Class Method SignaturesWhat the Operation Does public static int abs ( int x)returns the absolute value of the int value x public static double abs ( double x)returns the absolute value of the double value x public static double pow ( double base, double exponent)returns a double that is the power of the base raised to an exponent. public static double sqrt ( double x)returns the square root value of the double value x public static double random ()returns a random double value in the range [0.0, 1.0) in other words 0.0 inclusive and 1.0 exclusive, or you can say returns a double value x in the range: 0.0 <= x < 1.0. It is also good to know that there is a constant of the Math class named PI and it is accurate to 15 decimal places. It is declared as public static final double PI = 3.141592653589793;

5 Math Class Methods are Static All methods of the Math class are public and static. There are no void methods. They all return a numeric value! Now you are ready for a greater understanding of static. When we use the Math class, we never construct a Math object. In fact, there is no constructor of the Math class and you couldn’t instantiate an object of type Math if you wanted to. So the methods of the Math class were made static. That means when we want to call a Math class method, we just precede the name of the method with the name of the class instead of an object as in … …. Math.abs(…) …. Math.pow(…) …. Math.sqrt(…) …. Math.random(…)

6 Absolute Value of an int - abs(int x) The following code finds the absolute value of an integer value stored in x. Scanner reader = new Scanner (System.in); int x = reader.nextInt(); int absoluteValueOfX = Math.abs(x); After this code, the variable absoluteValueOfX will contain the absolute value of whatever integer (negative or positive) that is entered from the keyboard. If x holds the value -39, then absoluteValueOfX holds 39. If x holds the value 39, then absoluteValueOfX holds 39.

7 Absolute Value of a double- abs(double x) The following code finds the absolute value of a double value stored in y. Scanner reader = new Scanner (System.in); double y = reader.nextDouble(); double absoluteValueOfY = Math.abs(y); After this code, the variable absoluteValueOfY will contain the absolute value of whatever double (negative or positive) that is entered from the keyboard. If y holds the value -2.456, then absoluteValueOfY holds 2.456. If y holds the value 3.18, then absoluteValueOfY holds 3.18.

8 Power of a Number - pow(double base, double exponent) The following code finds the power of a base raised to an exponent. Scanner reader = new Scanner (System.in); System.out.print(“Enter a number for the base: ”); double base = reader.nextDouble(); System.out.print(“Enter a number for the exponent: ”); double exponent = reader.nextDouble(); double result = Math.pow(base, exponent); After this code, if base holds 2.0 and exponent holds -5.0, then result holds the value -32.0. 2.0 to the -5th power is calculated. After this code, if base holds 3.0 and exponent holds 4.0, then result holds the value 81.0. 3.0 to the 4th power is calculated.

9 Square Root of a Number - sqrt(double x) The following code finds the square root of a value stored in z. Scanner reader = new Scanner (System.in); System.out.print(“Enter a number to find the square root of: ”); double z = reader.nextDouble(); double result = Math.sqrt(z); After this code, if z holds 2.0, then result holds the value 1.4142135623730951. After this code, if z holds 16.0, then result holds the value 4.0.

10 The PI Constant of the Math class The following code uses the square root method of the Math class and the Math class constant PI which represents the value 3.141592653589793 to 15 decimal places. double area = 10.0; double radius = Math.sqrt (area / Math.PI); If you knew the area of a circle, you could find the radius by dividing the area by pi and then taking the square root of that result. Since the sqrt() method needs one parameter of type double, it is ok to place the expression area / math.PI in the parenthesis. That expression will be evaluated first to one value and that one value is the one parameter that is passed to sqrt. Note: area / Math.PI is a double divided by a double where the result is a double so the conditions for calling the method are satisfied.

11 Generating Random double Values

12 Random double Values: 0 <= result < 1.0 The following code randomly chooses a random double value between 0.0 inclusive and 1.0 exclusive and stores it in result. double result = Math.random(); Therefore, result will hold a value in the range: 0 <= result < 1.0 or we could say 0 <= result < 0.99999999999999999999999999 this can also be stated as … [0.0, 1.0) Note: Math.random() will never return 1.0

13 Random double Values: 0 <= result < 6.0 The following code randomly chooses a random double value between 0.0 inclusive and 6.0 exclusive and stores it in result. double result = Math.random() * 6; Therefore, result will hold a value in the range: 0 <= result < 6.0 or we could say 0 <= result < 5.99999999999999999999999999 this can also be stated as … [0.0, 6.0) Note: Math.random() * 6 will never return 6.0

14 Random double Values: 10.3 <= result < 40.8 The following code randomly chooses a random double value between 10.3 inclusive and 40.8 exclusive and stores it in result. double result = Math.random() * 30.5 + 10.3; First, Math.random() * 30.5 produces a random double in the range of 0.0 <= random value < 30.5 (not including 30.5) or [0.0, 30.5) or we could say between 0 and 30.499999999999999999999999 when we add the 10.3 then we get the range: 10.3 <= random value < 40.8 (not including 40.8) or [10.3, 40.8) So how did we get the 30.5?

15 Random double Values: 10.3 <= result < 40.8 So if you were beginning here: Write the code to generate a random double value between 10.3 inclusive and 40.8 exclusive?. You would subtract 40.8 - 10.3 and get 30.5. It’s the 30.5 that you take to multiply times Math.random() to get Math.random() * 30.5 You then simply add back on the 10.3 that you initially subtracted to get … Math.random() * 30.5 + 10.3; No additional ( ) are needed because of order of operations.

16 Random double Values: -200.0 <= result < 200.0 Write the code to generate a random double value between -200.0 inclusive and 200.0 exclusive? You would add 200 to the range numbers to get the new range of 0.0 to 400.0. Since we know the upper end is exclusive, we just use 400.0 to multiply times Math.random() to get Math.random() * 400.0 We then subtract off the 200.0 we added to get … Math.random() * 400.0 - 200.0; No additional ( ) are needed since we are generating a double value and order of operations evaluates everything correctly.

17 Generating Random int Values

18 Random int Values: 0 <= result < 5 To generate random integers between 0 and 5 inclusive, we need to use casting since there are not separate methods to generate random integers and random doubles with the Math class. The following code finds a random int value between 0 and 5 inclusive and stores it in result. int result = (int) (Math.random() * 6); This should make since, because Math.random() * 6 returns a double between 0.0 and 5.99999999999… Casting truncates double values, so the resulting range is 0 to 5, because (int) (5.999999999999..) is 5 due to truncation. Note: (int) (Math.random() * 6) will never return 6!

19 Random int Values Bug Be careful in designing your mathematical expression to generate random integers. What range of values are possible to be stored in result? int result = (int) (Math.random());

20 Random int Values Bug Revealed Be careful in designing your mathematical expression to generate random integers. What range of values are possible to be stored in result? int result = (int) (Math.random()); Zero would always be stored in result!! Why? Because Math.random() returns a double in the range from 0 up to but not including 1. So if you cast any value less than 1 to an (int) the truncation makes it zero.

21 Random int Values: 1 <= result < 6 To generate random integers between 1 and 6 inclusive, we need to add 1 outside of the expression being casted. The following code finds a random int value between 1 and 6 inclusive and stores it in result. int result = (int) (Math.random() * 6) + 1; Note: casting has a very high priority, so Java will cast the expression (Math.random() * 6) before it adds the 1. It would be incorrect to use: int result = (int) (Math.random() * 6+ 1); because this code would generate random integers between 0 and 6 instead of 1 and 6. Note where the red ( ) are.

22 Random int Values: 101 <= result <= 1000 To generate random integers in the range 101 to 1000 inclusive, we first subtract 101 to the range to get a new range of 0 to 899. Since the range is inclusive and we want both 101 and 1000 to possibly be generated, then we add 1 to 899 and get 900. This is the number that we mutliply Math.random() by: (int) (Math.random() * 900) generates integers from 0 to 899. We then add back the 101 that was initially subtracted to get: int result = (int) (Math.random() * 900) + 101; It would be incorrect to use: int result = (int) (Math.random() * 900 + 101); because this code would generate random integers between 100 and 1000 instead of 101 and 1000.

23 Random int Values: -500 <= result <= 500 To generate random integers in the range -500 to 500 inclusive, we first add 500 to the range to get a new range of 0 to 1000. Since the range is inclusive and we want both -500 and 500 to possibly be generated, then we add 1 to 1000 and get 1001. This is the number that we mutliply Math.random() by: (int) (Math.random() * 1001) generates integers from 0 to 1000. We then subtract off the 500 we originally added to get: int result = (int) (Math.random() * 1001) - 500;

24 Random int Values: 1 <= result < n The following code finds a random int value between 1 and n inclusive and stores it in result. int result = (int) (Math.random() * n) + 1; The following code finds a random int value between 0 and n - 1 inclusive and stores it in result. int result = (int) (Math.random() * n); This last line of code may be the one to be the most familiar with, because you may have to randomly select the index of an array or ArrayList of n items that has indices between 0 and n - 1.

25 Overloading Methods in a Class Sometimes classes contain multiple versions of the same method. This is called Overloading. An example, is the Math class method abs. There are four versions of this method in the Math class API. You saw two of them on the previous slide: static int abs ( int x) static double abs ( double x) additional ones are: static long abs ( long x) static float abs ( float x) How can a class have four different versions of a method? Because when one of these methods is called, Java inspects the parameter and identifies its type so it knows which one to go and execute.

26 Overloading Methods in a Class Overloading is using the same method name but a different number, type, or order of parameters in the same class. Here is another example of overloaded methods in a class: public double calculateSomething (int x) public double calculateSomething (int x, int y) public double calculateSomething (int x, double y) public double calculateSomething (double x, int y) public double calculateSomething (double x, double y) public double calculateSomething (int x, double y, int z) All of the methods have either a different number of parameters, different type, or different order.


Download ppt "The Math Class Methods Utilizing the Important Math Operations of Java!"

Similar presentations


Ads by Google