1 / 32

The Math Class Methods

The Math Class Methods. Utilizing the Important Math Operations of Java!. Section 1 - An Introduction Section 2 - The abs method Section 3 - The pow method Secton 4 - The sqrt method & Math.PI Section 5 - Generating Random Doubles Section 6 - Generating Random Integers

evelia
Download Presentation

The Math Class Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Math Class Methods Utilizing the Important Math Operations of Java! Section 1 - An Introduction Section 2 - The abs method Section 3 - The pow method Secton 4 - The sqrt method & Math.PI Section 5 - Generating Random Doubles Section 6 - Generating Random Integers Section 7 - Overloading Go Go Go Go Go Go Go

  2. Section 1An Introduction tothe Math Class 2

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

  4. 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 work with a few methods of the Math class that you are expected to know for the AP Exam. You are most likely to encounter them on the Multiple Choice part of the AP Exam, but it is quite possible that you might need to use them in code on Free Response questions. You might not receive full credit on a free response coding question if you try to use methods other than the ones specified by the College Board. The specified methods and what they do are listed on the next slide. 4

  5. The AP Subset of Math Class Methods 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

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

  7. Section 2The Math Classabs Method 7

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

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

  10. Section 3The Math Classpow Method 10

  11. 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 0.03125. 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. 11

  12. Section 4The Math Classsqrt Methodand Math.PI 12

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

  14. 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. 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. double area = 10.0; double radius = Math.sqrt (area / Math.PI); 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. 14

  15. Section 5The Math Classrandom MethodGenerating Random Doubles 15

  16. Random double Values: 0 <= result < 1.0 The following code randomly chooses a random doublevaluebetween 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 16

  17. Random double Values: 0 <= result < 6.0 The following code randomly chooses a random doublevaluebetween 0.0 inclusive and 6.0 exclusive and stores it in result. double result = Math.random() * 6.0; 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 There is no casting when generating random doubles. 17

  18. Random double Values: 0 <= result < 50.0 The following code randomly chooses a random doublevaluebetween 0.0 inclusive and 50.0 exclusive and stores it in result. double result = Math.random() * 50.0; Therefore, result will hold a value in the range: 0 <= result < 50.0 or we could say 0 <= result <= 49.9999999999999999999999999 this can also be stated as … [0.0, 50.0) Note: Math.random() * 50.0 will never return 50.0 There is no casting when generating random doubles. 18

  19. Random double Values: 10.3 <= result < 40.8 The following code randomly chooses a random doublevaluebetween 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? 19

  20. Random double Values: 10.3 <= result < 40.8 So if you were beginning with … “write the code to generate a random doublevaluebetween 10.3 inclusive and 40.8exclusive?” You would subtract 10.3 from both 10.3 and 40.8 to get the range of 0.0 to 30.5. We know how to generate values for this range … we just take the 30.5 and multiply it 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. 20

  21. Random double Values: -200.0 <= result < 200.0 Write the code to generate a random doublevaluebetween -200.0 inclusive and 200.0exclusive? You would add 200.0 to the range numbers to get the new range of 0.0 to 400.0. So we multiply 400.0 times Math.random() to get … Math.random() * 400.0 We then subtract off the 200.0 we added initially 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. 21

  22. Section 6The Math Classrandom MethodGenerating Random Ints 22

  23. 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 intvaluebetween 0 and 5 inclusive and stores it in result. To get the correct range, we pick 6, the integer 1 more than the upper range value of 5 to multiply by … int result = (int) (Math.random() * 6); This should make since, because Math.random() * 6 returns a double between 0.0 and 5.99999999999… Casting to int truncates the double values 0.0 and 5.99999…, so the resulting range is 0 to 5, because (int) (5.9999999999..) is 5 due to truncation. Note: (int) (Math.random() * 6) will never return 6! 23

  24. 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()); 24

  25. 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. Never use this code. 25

  26. 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 intvaluebetween 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. We know (int) (Math.random() * 6) produces integers from 0 to 5. So adding the 1 outside the ( ) makes the range 1 to 6. It bumps up the low & high ends. 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. 26

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

  28. Random int Values: -500 <= result <= 500 To generate random integers in the range -500 to 500 inclusive, we first add500 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; 28

  29. Random int Values: 1 <= result <= n The following code finds a random intvaluebetween 1 and n inclusive and stores it in result. int result = (int) (Math.random() * n) + 1; The following code finds a random intvaluebetween 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. 29

  30. Section 7Overloaded Methods ofThe Math Class 30

  31. 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 slide 5: 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 typeso it knows which one to go and execute. 31

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

More Related