Presentation is loading. Please wait.

Presentation is loading. Please wait.

Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.

Similar presentations


Presentation on theme: "Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type."— Presentation transcript:

1 Math With Java The Math Class

2 First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type in Java that represent numbers: int – Integer numbers int – Integer numbers double – Numbers with a decimal double – Numbers with a decimal Ex. Ex. double myValue = 10.5;//store 10.5 in the variable double myValue = 10.5;//store 10.5 in the variable int myInt = 7;//stores 7 in the variable myInt int myInt = 7;//stores 7 in the variable myInt The math operators for int and double are: The math operators for int and double are: * multiplication * multiplication / division / division + addition + addition - subtraction - subtraction % remainder (only for int) % remainder (only for int)

3 Casting When data types are converted, we use a technique called casting to tell java to do the conversion for us When data types are converted, we use a technique called casting to tell java to do the conversion for us When conversion results in a loss of data, the casting must be told to java When conversion results in a loss of data, the casting must be told to java Ex. Ex. double dVal = 10.5; int iVal = dVal;//error, you will lose 0.5 To tell java that you are OK with losing the 0.5 you have to rewrite the line as: To tell java that you are OK with losing the 0.5 you have to rewrite the line as: int iVal = (int)dVal;//(int) tells java you are aware of the possible loss of data Ex. Ex. int iVal = 7; double dVal = iVal; No problem this time, no information would be lost when storing an int in a double. No problem this time, no information would be lost when storing an int in a double.

4 What is printed int iVal1 = 6; int iVal2 = 10; double dVal1 = 4.0; double dVal2 = 3.0;  System.out.println( iVal2 / (int)dVal1); // 2  System.out.println( iVal2 / (double)iVal1); //1.6666667  System.out.println( (double)iVal2 / (int)dVal1); //2.5  System.out.println( (int)dVal2 / (int)dVal1); //1  System.out.println( (int)dVal1 / iVal1); //0

5 Integer Remainder So what happens to the decimal when dividing integers? So what happens to the decimal when dividing integers? It is possible to find out how much was left over in the division, also known as the remainder It is possible to find out how much was left over in the division, also known as the remainder For example, For example, 5 / 3 is 1 remainder 2 5 / 3 is 1 remainder 2 7 / 2 is 3 remainder 1 7 / 2 is 3 remainder 1

6 (%) Mod Operator With integers, the % operator determines the remainder after division has occurred With integers, the % operator determines the remainder after division has occurred Ex. Ex. 10 % 6 = 4 10 % 6 = 4 12 % 4 = 0 12 % 4 = 0 32 % 5 = 2 32 % 5 = 2 18 % 5 = 3 18 % 5 = 3

7 Mod Operator Mod Operator has many uses, one common one is telling time Mod Operator has many uses, one common one is telling time If 350 minutes have past since midnight, what time is it? If 350 minutes have past since midnight, what time is it? There are 60 minutes in an hour There are 60 minutes in an hour 350 / 60 = 5 350 / 60 = 5 350 % 60 = 50 350 % 60 = 50 The time is 5:50am The time is 5:50am

8 New - Shortcut Operators Some operators are shortcuts to common expressions: Some operators are shortcuts to common expressions: OperatorExpression Equivalent To += x += y; x = x + y; -= x -= y; x = x – y; *= x *= y; x = x*y; /= x /= y; x = x / y; ++x++; x = x+1; --x--; x = x – 1;

9 The Math Class The Math class is a special class called a static class, there is no need to create a Math object, instead we ask the class directly The Math class is a special class called a static class, there is no need to create a Math object, instead we ask the class directly Static class Static class Math.PI//value of PI Math.PI//value of PI Non static class Non static class String PI = “3.14”;//make a new string String PI = “3.14”;//make a new string

10 Math class All of the buttons you find on your calculator are in the Math class All of the buttons you find on your calculator are in the Math class sin, cos, tan sin, cos, tan sqrt, pow, etc. sqrt, pow, etc. Of course you can find them all in the JavaDoc Of course you can find them all in the JavaDoc Another things you’ll need to use in the Math class is random numbers Another things you’ll need to use in the Math class is random numbers

11 Numeric Operations The Math class can be used by putting the following line under the package statement: The Math class can be used by putting the following line under the package statement: import java.lang.Math; import java.lang.Math; import – command tells java to get code from another library import – command tells java to get code from another library

12 Math Methods double Math.ceil( double value ) double Math.ceil( double value ) This method rounds up a floating point number This method rounds up a floating point number It also returns a floating point so be sure to cast if you’re putting the result in an int It also returns a floating point so be sure to cast if you’re putting the result in an int Ex. Ex. double ceil = Math.ceil( 10.9 ); //returns 11.0 double ceil = Math.ceil( 10.9 ); //returns 11.0 double ceil = Math.ceil( 3.1 ); //returns 4.0 double ceil = Math.ceil( 3.1 ); //returns 4.0 double ceil = Math.ceil( -1.9 ); //returns -1.0 double ceil = Math.ceil( -1.9 ); //returns -1.0 int ceil = (int)Math.ceil( -1.2 ); //returns -1.0 int ceil = (int)Math.ceil( -1.2 ); //returns -1.0

13 Math Methods double Math.floor( double value ) double Math.floor( double value ) This method rounds down a floating point number This method rounds down a floating point number It also returns a floating point so be sure to cast if you’re putting the result in an int It also returns a floating point so be sure to cast if you’re putting the result in an int Ex. Ex. double floor = Math.floor( 3.1 ); //returns 3.0 double floor = Math.floor( 3.1 ); //returns 3.0 double floor = Math.floor( 10.9 ); //returns 10.0 double floor = Math.floor( 10.9 ); //returns 10.0 double floor = Math.floor( -1.2 ); //returns -2.0 double floor = Math.floor( -1.2 ); //returns -2.0 int floor = (int)Math.floor( -1.9 ); //returns -2.0 int floor = (int)Math.floor( -1.9 ); //returns -2.0

14 Math Methods double Math.pow( double base, double exponent ) double Math.pow( double base, double exponent ) This method returns the power of (base) exponent This method returns the power of (base) exponent It also returns a floating point so be sure to cast if you’re putting the result in an int It also returns a floating point so be sure to cast if you’re putting the result in an int Ex. Ex. double power = Math.pow( 2, 3 ); //returns 8.0 double power = Math.pow( 2, 3 ); //returns 8.0 double power = Math.pow( 3, 4 ); //returns 81.0 double power = Math.pow( 3, 4 ); //returns 81.0 double power = Math.pow( 0.1, 5 ); //returns 0.00001 double power = Math.pow( 0.1, 5 ); //returns 0.00001 int power = (int)Math.pow( 1.5, 2 );//returns 2.25 (it is cast to 2) int power = (int)Math.pow( 1.5, 2 );//returns 2.25 (it is cast to 2)

15 Math Methods double Math.abs( double value ) double Math.abs( double value ) This method returns the absolute value of value This method returns the absolute value of value It also returns a floating point so be sure to cast if you’re putting the result in an int It also returns a floating point so be sure to cast if you’re putting the result in an int Ex. Ex. double absolute = Math.abs( 100 ); //returns 100.0 double absolute = Math.abs( 100 ); //returns 100.0 double absolute = Math.abs( -4.5); //returns 4.5 double absolute = Math.abs( -4.5); //returns 4.5 double absolute = Math.abs( 0.57 ); //returns 0.57 double absolute = Math.abs( 0.57 ); //returns 0.57 int absolute = (int)Math.abs( -1234 ); //returns 1234 int absolute = (int)Math.abs( -1234 ); //returns 1234

16 Math Methods double Math.sqrt( double value ) double Math.sqrt( double value ) This method returns the square root of value This method returns the square root of value It also returns a floating point so be sure to cast if you’re putting the result in an int It also returns a floating point so be sure to cast if you’re putting the result in an int Ex. Ex. double root = Math.sqrt( 100 ); //returns 10.0 double root = Math.sqrt( 100 ); //returns 10.0 double root = Math.sqrt( 2 ); //returns 1.4142 double root = Math.sqrt( 2 ); //returns 1.4142 double root = Math.sqrt( 0.25 ); //returns 0.5 double root = Math.sqrt( 0.25 ); //returns 0.5 int root = (int)Math.sqrt( 16.0 ); //returns 4 int root = (int)Math.sqrt( 16.0 ); //returns 4

17 Making a Formula in Java The quadratic formula: The quadratic formula: Using the order of operations we get, Using the order of operations we get, Math.pow( b, 2 ))Math.sqrt( – 4*a*c–1*b+()/ 2*ax1 = x2 = (–1*b – Math.sqrt( Math.pow( b, 2 ) – 4*a*c )) / 2*a Can you rewrite the other solution?

18 Math.random() Random numbers can be generated from the random method of the Math Class Random numbers can be generated from the random method of the Math Class static double random(); static double random(); returns a random number between 0.0 (inclusive) and 1.0 (exclusive) returns a random number between 0.0 (inclusive) and 1.0 (exclusive) Ex. Ex. double randomNumber = Math.random() * 100; double randomNumber = Math.random() * 100; This is a random number between 0.0 and 99.999999 This is a random number between 0.0 and 99.999999

19 Using Math.random Ex. Ex. double randomNumber = Math.random() * 35; double randomNumber = Math.random() * 35; Between 0.0 and 34.9999999999 Between 0.0 and 34.9999999999 double randomNumber = Math.random() * 50 + 10; double randomNumber = Math.random() * 50 + 10; Between 10.0 and 59.999999999 Between 10.0 and 59.999999999 int randomNumber = (int)(Math.random() * 20) + 5; int randomNumber = (int)(Math.random() * 20) + 5; Between 5 and 24 Between 5 and 24 don’t forget to cast with ints don’t forget to cast with ints Notice the range is 5 to 24! Notice the range is 5 to 24! Math.random() * 20 returns 0 to 19.999999999999… Math.random() * 20 returns 0 to 19.999999999999… (int)(Math.random() * 20) chops off all decimals (int)(Math.random() * 20) chops off all decimals Returns 0 to 19 Returns 0 to 19

20 Be Careful When Casting! (int)Math.random() * 20 + 5; (int)Math.random() * 20 + 5; returns 5 every time your program is run: returns 5 every time your program is run: Generate a random value, say 0.774312 Generate a random value, say 0.774312 Cast it to an it Cast it to an it Now it has the value 0 Now it has the value 0 0 * 20 = 0 0 * 20 = 0 0 + 5 = 5 0 + 5 = 5

21 Using Math.random If you want to generate a number in the range [a,b)  not including b (as doubles) If you want to generate a number in the range [a,b)  not including b (as doubles) Math.random()*(b – a) + a Math.random()*(b – a) + a If you want to generate a number in the range [a,b] (as ints) If you want to generate a number in the range [a,b] (as ints) (int)(Math.random()*(b – a+1)) + a (int)(Math.random()*(b – a+1)) + a


Download ppt "Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type."

Similar presentations


Ads by Google