Numeric Operations

Download Report

Transcript Numeric Operations

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
int myInt = 7;//stores 7 in the variable myInt
The math operators for int and double are:





* multiplication
/ division
+ addition
- subtraction
% remainder (only for int)
Casting



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
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:
int iVal = (int)dVal;//(int) tells java you are aware of the possible loss of data

Ex.
int iVal = 7;
double dVal = iVal;

No problem this time, no information would be lost when storing an int in a
double.
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
Integer Remainder



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
For example,


5 / 3 is 1 remainder 2
7 / 2 is 3 remainder 1
(%) Mod Operator


With integers, the % operator determines
the remainder after division has occurred
Ex.




10
12
32
18
%
%
%
%
6
4
5
5
=
=
=
=
4
0
2
3
Mod Operator






Mod Operator has many uses, one
common one is telling time
If 350 minutes have past since midnight,
what time is it?
There are 60 minutes in an hour
350 / 60 = 5
350 % 60 = 50
The time is 5:50am
New - Shortcut Operators

Some operators are shortcuts to common expressions:
Operator
Expression
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;
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
Static class


Math.PI//value of PI
Non static class

String PI = “3.14”;//make a new string
Math class

All of the buttons you find on your
calculator are in the Math class




sin, cos, tan
sqrt, pow, etc.
Of course you can find them all in the
JavaDoc
Another things you’ll need to use in the
Math class is random numbers
Numeric Operations

The Math class can be used by putting the
following line under the package statement:


import java.lang.Math;
import – command tells java to get code from
another library
Math Methods



double Math.ceil( double value )
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

Ex.




double ceil = Math.ceil( 10.9 ); //returns 11.0
double ceil = Math.ceil( 3.1 ); //returns 4.0
double ceil = Math.ceil( -1.9 ); //returns -1.0
int ceil = (int)Math.ceil( -1.2 ); //returns -1.0
Math Methods



double Math.floor( double value )
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

Ex.




double floor = Math.floor( 3.1 ); //returns 3.0
double floor = Math.floor( 10.9 ); //returns 10.0
double floor = Math.floor( -1.2 ); //returns -2.0
int floor = (int)Math.floor( -1.9 ); //returns -2.0
Math Methods



double Math.pow( double base, double 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

Ex.




double power = Math.pow( 2, 3 ); //returns 8.0
double power = Math.pow( 3, 4 ); //returns 81.0
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)
Math Methods



double Math.abs( double 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

Ex.




double absolute = Math.abs( 100 ); //returns 100.0
double absolute = Math.abs( -4.5); //returns 4.5
double absolute = Math.abs( 0.57 ); //returns 0.57
int absolute = (int)Math.abs( -1234 ); //returns 1234
Math Methods



double Math.sqrt( double 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

Ex.




double root = Math.sqrt( 100 ); //returns 10.0
double root = Math.sqrt( 2 ); //returns 1.4142
double root = Math.sqrt( 0.25 ); //returns 0.5
int root = (int)Math.sqrt( 16.0 ); //returns 4
Making a Formula in Java


The quadratic formula:
Using the order of operations we get,
b  b2  4ac
2a
x1 =
( –1*b + Math.sqrt( Math.pow( b, 2 ) – 4*a*c )) / 2*a
Can you rewrite the other solution?
x2 = (–1*b – Math.sqrt( Math.pow( b, 2 ) – 4*a*c )) / 2*a
Math.random()

Random numbers can be generated from
the random method of the Math Class



static double random();
returns a random number between 0.0
(inclusive) and 1.0 (exclusive)
Ex.


double randomNumber = Math.random() *
100;
This is a random number between 0.0 and
99.999999
Using Math.random

Ex.











double randomNumber = Math.random() * 35;
Between 0.0 and 34.9999999999
double randomNumber = Math.random() * 50 + 10;
Between 10.0 and 59.999999999
int randomNumber = (int)(Math.random() * 20) + 5;
Between 5 and 24
don’t forget to cast with ints
Notice the range is 5 to 24!
Math.random() * 20 returns 0 to 19.999999999999…
(int)(Math.random() * 20) chops off all decimals
Returns 0 to 19
Be Careful When Casting!

(int)Math.random() * 20 + 5;



Generate a random value, say 0.774312
Cast it to an it



returns 5 every time your program is run:
Now it has the value 0
0 * 20 = 0
0+5=5
Using Math.random

If you want to generate a number in the
range [a,b)  not including b (as doubles)


Math.random()*(b – a) + a
If you want to generate a number in the
range [a,b] (as ints)

(int)(Math.random()*(b – a+1)) + a