• 0:00
    /
    0:00
    Loaded: 0%
    0:00
    Progress: 0%
    Stream TypeLIVE
    0:00
     
    1x
    Advertisement

Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.

Similar presentations


Presentation on theme: "Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static."— Presentation transcript:

1 Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static. This means there is only one copy. You cannot create an object of the class. You will use the class name to call the methods.

2 Math Class methods Tested on the AP Method Summary
abs(double x) Math.abs(36.0);           Returns the absolute value of a double value. abs(int x) Math.abs(36);           Returns the absolute value of an int value. pow(double base, double exponent) Math.pow(2,4);           Returns the value of baseexponent. sqrt(double x) Math.sqrt(4);           Returns the square root of a number.

3 Mathematical expressions to Java

4 Math.random() The Math.random() method returns random double numbers in the range >=0.0 to <1.0 . x= Math.random(); // assigns random number to x x = 0.0 to To get a desired number you multiply and cast to an int. int n = (int)(Math.random()* 10) This will produce a number starting at 0 inclusive and up to 10 exclusive. Number will be from 0 to 9

5 int n = (int)(Math.random()* 10)
Starts at 0 and goes to < Because 0-9 is 10 numbers. This will produce a number starting at 0 inclusive and up to 10 exclusive. Number will be from 0 to 9

6 Math.random To include the 10 You would add + 1
int n = (int)(Math.random()*10+1) This will produce a number starting at 1 inclusive and up to 10 exclusive. Number will be from 1 to 10

7 Math.random Create a number starting with 20 and up to 50 not including 50. The + will be the number you start with = 50 int n = (int)(Math.random()*30 )+20; Produce 20 to = 49 This will produce a number starting at 20 inclusive and up to 50 exclusive.

8 Generating Random Numbers
Generate a number from 65 inclusive and 90 exclusive  A random decimal between 0 inclusive & 5 exclusive A random integer between 10 inclusive & 45 exclusive A random integer between 3 inclusive & 12 inclusive int num = (int) (Math.random() * 25) = to 89 int num = (int)(Math.random()*5); int num = (int)(Math.random()*35); = to 44 int num = (int)(Math.random()*10+3) = to 12


Download ppt "Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static."

Similar presentations


Ads by Google