EXPRESSIONS AND ARITHMETIC. Working With Base (or BuiltIn) Types Arithmetic Constants. Number Crunching (crunch, crunch). Why do we have to learn about arithmetic? We’ve done fine without it so far... With numbers we can…
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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
We’ve done fine without it so far...
(e.g. loop counters — stay tuned)
Imagine trying to measure something without numbers!
(0, 0)
(MAX_X, 0)
(0, MAX_Y)
(MAX_X, MAX_Y)
128 to 127 (27 to 27  1)
32,768 to 32,767 ( 215 to 215  1)
2,147,483,648 to 2,147,483,647
(231 to 231  1)
9,223,372,...,808 to 9,223,372,...,807
(263 to 263 1)
public class HockeyPlayer{
private int _numTeeth;
}
public class HockeyPlayer {
private int _numTeeth;
public HockeyPlayer() {
_numTeeth = 8;
}
public void playHockey() {
// we can reassign it!
_numTeeth = 4;
}
}
Operator Meaning
value of numOne (assuming numOne is a
numeric variable type)
multiplication
division
remainder
*
/
%
+

addition
subtraction
val = val; // negates val
a += 5; a = a + 5;
a = 5; a = a  5;
a *= 5; a = a * 5;
a /= 5; a = a / 5;
a %= 5; a = a % 5;
a++; a = a + 1; a += 1;
a; a = a  1; a = 1;
variable
inti = 10;
int j = i++; // j is 10, i becomes 11
inti = 10;
int j = ++i; // j is 11, so is i
except that:
first, starting at the innermost level, and
precedence
2
2 * 3
2 + 4 * 3  7
4 / 2
10 % 3
(2 + 3) * (11 / 12)
(6 + 4) * 3 + (2  (6 / 3))
0 % 9
9 % 9
numbers?
point is assumed to be (an internal implementation
detail you don’t have to worry about)
they a have larger range and are more precise
double myRadius = 2.427;
r
public class MyMathClass {
public void reassign() {
intmyInt = 4;
double myDouble = 2.64;
myInt = myDouble;
// can’t assign a double to an int
}
}
myDouble = myInt;
// myDouble is now 4.0
myInt = (int)myDouble; // myInt is now 2
Math.sin(x);
Math.cos(x);
Math.tan(x);
Math.pow(x,y); — raises x to the power of y
Math.log(x);
Math.sqrt(x); — square root of x
... and more!
E = 2.71...
PI = 3.14...
public static double sin(double a) {
...
}
_side = Math.sin(x);
public class SimpleExample {
// We initialize outside of constructor
// because the variable is static
private staticint _numberOfInstances = 0;
public SimpleExample() {
// We want to keep a tally of all the
// SimpleExamples that we create
_numberOfInstances++;
}
public getNumInstances() {
return _numberOfInstances;
}
// Anyone can call getNumInstances to see
// how many SimpleExamples have been created
}
GREEN, etc. values of the java.awt.Color class
public class Physics {
// Speed of Light
// (hundredmillion meters per second)
public static final double LIGHT_SPEED =
2.998;
// constructor elided
public double getDistanceTraveled(
double numSeconds) {
// distance = velocity * time
return (LIGHT_SPEED * numSeconds);
}
}
public abstract class PhysicsConstants {
// speed of light (hundredmillion meters
// per second)
double static final LIGHT_SPEED = 2.998;
// more constants, if we want . . .
}
public double massToEnergy(double mass) {
return mass*PhysicsConstants.LIGHT_SPEED*
PhysicsConstants.LIGHT_SPEED;
}
public abstract class CowConstants {
// initial weight of cow in pounds
double static final COW_START_WEIGHT = 587.45;
// weight of one blade of grass in pounds
double static final BLADE_WEIGHT = 0.0118;
// cow must be under this weight to fly
double static final MAX_FLYING_WEIGHT = 603.76;
} // end of CowConstants
public class Cow extends FarmAnimal {
private double _weight;
// constructor elided
/**
* This method changes the cow’s weight
* based on how many blades of grass it ate.
*/
public void eatGrass(intnumBlades) {
double grassWeight = numBlades *
CowConstants.BLADE_WEIGHT;
// What can we use to calculate square
// root?
}
}
public void eatGrass(intnumBlades) {
double grassWeight = numBlades *
CowConstants.BLADE_WEIGHT;
_weight += 5 * Math.sqrt(grassWeight);
}
Inheritance and Interfaces
22 of 29