|
float and double data types in Java support 4 arithmetic operations: +, -, *, /
public class FloatArithm
{
public static void main(String[] args)
{
double a, b, c;
a = 4.2; // Put breakpoint here to demo
b = 2.0;
c = a + b; // 4.2 + 2.0 = 6.2
c = a - b; // 4.2 - 2.0 = 2.2
c = a * b; // 4.2 * 2.0 = 8.4
c = a / b; // 4.2 / 2.0 = 2.1
}
}
|
DEMO: demo/02-elem-prog/06-arith-ops/FloatArithm.java
int, short, byte and long data types has 5 arithmetic operations: +, -, *, /, %
public class IntegerArithm
{
public static void main(String[] args)
{
int a, b, c;
a = 7; // Put breakpoint here to demo
b = 3;
c = a + b; // 7 + 3 = 10
c = a - b; // 7 - 3 = 4
c = a * b; // 7 * 3 = 21
c = a / b; // 7 / 3 = 2 (computes the quotient of 7/3)
c = a % b; // 7 % 3 = 1 (computes the remainder of 7/3)
}
}
|
DEMO: demo/02-elem-prog/06-arith-ops/IntegerArithm.java
|
DEMO: demo/02-elem-prog/06-arith-ops/OperPrio.java
|
DEMO: demo/02-elem-prog/06-arith-ops/OddEven.java
A common use of the remainder % operation is to convert length in # inches to # feet + # inches:
public class InchesToFeetPlusInches
{
public static void main(String[] args)
{
int lengthInInches = 58;
int feet, inches;
feet = lengthInInches / 12; // # feet
inches = lengthInInches % 12; // # inches
}
}
|
DEMO: demo/02-elem-prog/06-arith-ops/InchesToFeetPlusInches.java
The remainder % operation can also be used to convert time in # seconds to # minutes + # seconds:
public class SecondsToMS
{
public static void main(String[] args)
{
int timeInSeconds = 76;
int minutes, seconds;
minutes = timeInSeconds / 60;
seconds = timeInSeconds % 60;
}
}
|
DEMO: demo/02-elem-prog/06-arith-ops/SecondsToMS.java
Question: what if we use timeInSeconds = 3667 --- convert to hours, minutes and seconds
"Staged" conversion: (1) # seconds ⇒ # Hrs + # RemSeconds and then (2) # RemSeconds ⇒ # min + # sec
public class SecondsToHMS
{
public static void main(String[] args)
{
int timeInSeconds = 3667;
int hours, minutes, seconds;
int remainingSeconds; // Help variable
// Stage 1
hours = timeInSeconds / 3600;
remainingSeconds = timeInSeconds % 3600;
// Stage 2
minutes = remainingSeconds / 60;
seconds = remainingSeconds % 60;
}
}
|
DEMO: demo/02-elem-prog/06-arith-ops/SecondsToHMS.java
I have provide "homeworks" for you to practice the material learned in this course.
The homeworks can be found at this website:
http://www.cs.emory.edu/~cheung/OutSchool/apcs/HW/
This website also contains the answers to the homeworks.
You should only look at the answer AFTER doing the homework to check you work.
|
import java.lang.Math; // Optional --- see comment public class Exponentiation { public static void main(String[] args) { double a, b, c; a = 3.0; b = 2.0; c = Math.pow( a, b ); // ab } } |
DEMO: demo/02-elem-prog/06-arith-ops/Exponentiation.java
Comment: the Java compiler will automatically import all classes in java.lang.*
// x += y ≡ x = x + y
// x -= y ≡ x = x - y
// x *= y ≡ x = x * y, and so on
// x++ ≡ x = x + 1
// x-- ≡ x = x - 1
public class ShortHandOp
{
public static void main(String[] args)
{
int a, b;
a = 4;
b = 2;
a += b; // Same as: a = a + b -- Try: -=, *=, /=
a++;
}
}
|
DEMO: demo/02-elem-prog/06-arith-ops/ShortHandOp.java
Most common: +=, -=, ++ and --