21
Math With Java Math With Java The Math Class The Math Class

Math With Java

  • Upload
    oceana

  • View
    36

  • Download
    0

Embed Size (px)

DESCRIPTION

Math With Java. The Math Class. First, A Quick Review of Math Operators in Java. Primitive Data type in Java that represent numbers: int – Integer numbers double – Numbers with a decimal Ex. double myValue = 10.5;//store 10.5 in the variable - PowerPoint PPT Presentation

Citation preview

Page 1: Math With Java

Math With JavaMath With Java

The Math ClassThe Math Class

Page 2: Math With Java

First, A Quick Review of First, A Quick Review of Math Operators in JavaMath Operators in Java

Primitive Data type in Java that represent numbers:Primitive Data type in Java that represent numbers: int – Integer numbersint – Integer numbers double – Numbers with a decimaldouble – Numbers with a decimal Ex.Ex.

double myValue = 10.5;//store 10.5 in the variabledouble myValue = 10.5;//store 10.5 in the variable int myInt = 7;//stores 7 in the variable myIntint 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)

Page 3: Math With Java

CastingCasting

When data types are converted, we use a technique called casting to When data types are converted, we use a technique called casting to tell java to do the conversion for ustell java to do the conversion for us

When conversion results in a loss of data, the casting must be told to When conversion results in a loss of data, the casting must be told to javajava

Ex.Ex.double dVal = 10.5;double dVal = 10.5;int iVal = dVal;//error, you will lose 0.5int iVal = dVal;//error, you will lose 0.5

To tell java that you are OK with losing the 0.5 you have to rewrite To tell java that you are OK with losing the 0.5 you have to rewrite the line as:the line as:int iVal = (int)dVal;//(int) tells java you are aware of the possible loss of dataint iVal = (int)dVal;//(int) tells java you are aware of the possible loss of data

Ex.Ex.int iVal = 7;int iVal = 7;double dVal = iVal;double dVal = iVal;

No problem this time, no information would be lost when storing an No problem this time, no information would be lost when storing an int in a double.int in a double.

Page 4: Math With Java

What is printedWhat is printed

int iVal1 = 6;int iVal1 = 6;

int iVal2 = 10;int iVal2 = 10;

double dVal1 = 4.0;double dVal1 = 4.0;

double dVal2 = 3.0;double dVal2 = 3.0; System.out.println( iVal2 / (int)dVal1); // 2System.out.println( iVal2 / (int)dVal1); // 2 System.out.println( iVal2 / (double)iVal1); System.out.println( iVal2 / (double)iVal1);

//1.6666667//1.6666667 System.out.println( (double)iVal2 / (int)dVal1); //2.5System.out.println( (double)iVal2 / (int)dVal1); //2.5 System.out.println( (int)dVal2 / (int)dVal1); //1System.out.println( (int)dVal2 / (int)dVal1); //1 System.out.println( (int)dVal1 / iVal1); //0System.out.println( (int)dVal1 / iVal1); //0

Page 5: Math With Java

Integer RemainderInteger Remainder

So what happens to the decimal So what happens to the decimal when dividing integers?when dividing integers?

It is possible to find out how much It is possible to find out how much was left over in the division, also was left over in the division, also known as the remainderknown as the remainder

For example, For example, 5 / 3 is 1 remainder 25 / 3 is 1 remainder 2 7 / 2 is 3 remainder 17 / 2 is 3 remainder 1

Page 6: Math With Java

(%) Mod Operator(%) Mod Operator

With integers, the % operator With integers, the % operator determines the remainder after determines the remainder after division has occurreddivision has occurred

Ex.Ex. 10 % 6 = 410 % 6 = 4 12 % 4 = 012 % 4 = 0 32 % 5 = 232 % 5 = 2 18 % 5 = 318 % 5 = 3

Page 7: Math With Java

Mod OperatorMod Operator

Mod Operator has many uses, one Mod Operator has many uses, one common one is telling timecommon one is telling time

If 350 minutes have past since If 350 minutes have past since midnight, what time is it?midnight, what time is it?

There are 60 minutes in an hourThere are 60 minutes in an hour 350 / 60 = 5350 / 60 = 5 350 % 60 = 50350 % 60 = 50 The time is 5:50amThe time is 5:50am

Page 8: Math With Java

New - Shortcut OperatorsNew - Shortcut Operators

Some operators are shortcuts to common expressions:Some operators are shortcuts to common expressions:

OperatorOperator ExpressionExpression Equivalent ToEquivalent To

+=+= x += y;x += y; x = x + y;x = x + y;

-=-= x -= y;x -= y; x = x – y;x = x – y;

*=*= x *= y;x *= y; x = x*y;x = x*y;

/=/= x /= y;x /= y; x = x / y;x = x / y;

++++ x++;x++; x = x+1;x = x+1;

---- x--;x--; x = x – 1;x = x – 1;

Page 9: Math With Java

The Math ClassThe Math Class

The Math class is a special class The Math class is a special class called a static class, there is no need called a static class, there is no need to create a Math object, instead we to create a Math object, instead we ask the class directlyask the class directly

Static classStatic class Math.PI//value of PIMath.PI//value of PI

Non static classNon static class String PI = “3.14”;//make a new stringString PI = “3.14”;//make a new string

Page 10: Math With Java

Math classMath class

All of the buttons you find on your All of the buttons you find on your calculator are in the Math classcalculator are in the Math class sin, cos, tansin, cos, tan sqrt, pow, etc.sqrt, pow, etc.

Of course you can find them all in the Of course you can find them all in the JavaDocJavaDoc

Another things you’ll need to use in Another things you’ll need to use in the Math class is random numbersthe Math class is random numbers

Page 11: Math With Java

Numeric OperationsNumeric Operations

The Math class can be used by putting the The Math class can be used by putting the following line under the package following line under the package statement:statement: import java.lang.Math;import java.lang.Math;

import – command tells java to get code import – command tells java to get code from another libraryfrom another library

Page 12: Math With Java

Math MethodsMath Methods

double Math.ceil( double value )double Math.ceil( double value ) This method This method rounds uprounds up a floating point a floating point

numbernumber It also returns a floating point so be sure to It also returns a floating point so be sure to

cast if you’re putting the result in an intcast if you’re putting the result in an int Ex.Ex.

double ceil = Math.ceil( 10.9 ); //returns 11.0double ceil = Math.ceil( 10.9 ); //returns 11.0 double ceil = Math.ceil( 3.1 ); //returns 4.0double ceil = Math.ceil( 3.1 ); //returns 4.0 double ceil = Math.ceil( -1.9 ); //returns -1.0double ceil = Math.ceil( -1.9 ); //returns -1.0 int ceil = (int)Math.ceil( -1.2 ); //returns -1.0int ceil = (int)Math.ceil( -1.2 ); //returns -1.0

Page 13: Math With Java

Math MethodsMath Methods

double Math.floor( double value )double Math.floor( double value ) This method This method rounds down rounds down a floating point a floating point

numbernumber It also returns a floating point so be sure It also returns a floating point so be sure

to cast if you’re putting the result in an intto cast if you’re putting the result in an int Ex.Ex.

double floor = Math.floor( 3.1 ); //returns 3.0double floor = Math.floor( 3.1 ); //returns 3.0 double floor = Math.floor( 10.9 ); //returns 10.0double floor = Math.floor( 10.9 ); //returns 10.0 double floor = Math.floor( -1.2 ); //returns -2.0double floor = Math.floor( -1.2 ); //returns -2.0 int floor = (int)Math.floor( -1.9 ); //returns -2.0int floor = (int)Math.floor( -1.9 ); //returns -2.0

Page 14: Math With Java

Math MethodsMath Methods double Math.pow( double base, double double Math.pow( double base, double

exponent )exponent ) This method This method returns the power of (base)returns the power of (base)exponentexponent

It also returns a floating point so be sure to It also returns a floating point so be sure to cast if you’re putting the result in an intcast if you’re putting the result in an int Ex.Ex.

double power = Math.pow( 2, 3 ); //returns 8.0double power = Math.pow( 2, 3 ); //returns 8.0 double power = Math.pow( 3, 4 ); //returns 81.0double power = Math.pow( 3, 4 ); //returns 81.0 double power = Math.pow( 0.1, 5 ); //returns 0.00001double power = Math.pow( 0.1, 5 ); //returns 0.00001 int power = (int)Math.pow( 1.5, 2 );//returns 2.25 (it is int power = (int)Math.pow( 1.5, 2 );//returns 2.25 (it is

cast to 2)cast to 2)

Page 15: Math With Java

Math MethodsMath Methods

double Math.abs( double value )double Math.abs( double value ) This method This method returns the absolute valuereturns the absolute value of of

valuevalue It also returns a floating point so be sure to It also returns a floating point so be sure to

cast if you’re putting the result in an intcast if you’re putting the result in an int Ex.Ex.

double absolute = Math.abs( 100 ); //returns 100.0double absolute = Math.abs( 100 ); //returns 100.0 double absolute = Math.abs( -4.5); //returns 4.5double absolute = Math.abs( -4.5); //returns 4.5 double absolute = Math.abs( 0.57 ); //returns 0.57double absolute = Math.abs( 0.57 ); //returns 0.57 int absolute = (int)Math.abs( -1234 ); //returns 1234int absolute = (int)Math.abs( -1234 ); //returns 1234

Page 16: Math With Java

Math MethodsMath Methods double Math.sqrt( double value )double Math.sqrt( double value ) This method This method returns the square rootreturns the square root of of

valuevalue It also returns a floating point so be sure It also returns a floating point so be sure

to cast if you’re putting the result in an intto cast if you’re putting the result in an int Ex.Ex.

double root = Math.sqrt( 100 ); //returns 10.0double root = Math.sqrt( 100 ); //returns 10.0 double root = Math.sqrt( 2 ); //returns 1.4142double root = Math.sqrt( 2 ); //returns 1.4142 double root = Math.sqrt( 0.25 ); //returns 0.5double root = Math.sqrt( 0.25 ); //returns 0.5 int root = (int)Math.sqrt( 16.0 ); //returns 4int root = (int)Math.sqrt( 16.0 ); //returns 4

Page 17: Math With Java

2 4

2

b b ac

a

Making a Formula in JavaMaking 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?

Page 18: Math With Java

Math.random()Math.random()

Random numbers can be generated from Random numbers can be generated from the random method of the Math Classthe random method of the Math Class static double random();static double random(); returns a random number between 0.0 returns a random number between 0.0

(inclusive) and 1.0 (exclusive)(inclusive) and 1.0 (exclusive) Ex.Ex.

double randomNumber = Math.random() * double randomNumber = Math.random() * 100;100;

This is a random number between 0.0 and This is a random number between 0.0 and 99.99999999.999999

Page 19: Math With Java

Using Math.randomUsing Math.random Ex.Ex.

double randomNumber = Math.random() * 35;double randomNumber = Math.random() * 35; Between 0.0 and 34.9999999999Between 0.0 and 34.9999999999 double randomNumber = Math.random() * 50 + 10;double randomNumber = Math.random() * 50 + 10; Between 10.0 and 59.999999999Between 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 intsdon’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 Math.random() * 20 returns 0 to

19.999999999999…19.999999999999… (int)(Math.random() * 20) chops off all decimals(int)(Math.random() * 20) chops off all decimals Returns 0 to 19Returns 0 to 19

Page 20: Math With Java

Be Careful When Casting!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.774312Generate a random value, say 0.774312 Cast it to an itCast it to an it

Now it has the value 0Now it has the value 0 0 * 20 = 00 * 20 = 0 0 + 5 = 50 + 5 = 5

Page 21: Math With Java

Using Math.randomUsing Math.random

If you want to generate a number in If you want to generate a number in the range [a,b) the range [a,b) not including not including bb (as (as doubles) doubles) Math.random()*(b – a) + aMath.random()*(b – a) + a

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