 |
Class Design, Part III
|
Reading: Ch. 5.4.12 - 5.6
Using ints in a class/program (contd.)
Using the Shop class
-
Want a test driver that does
-
initial testing of the class
-
makes sure the interface is consistent
class TestShop {
public static void main (String [] args) {
Employee bSchmerl = new Employee ("Bradley Schmerl", 12, 36);
Employee aKaplan = new Employee ("Alan Kaplan", 15, 24);
Employee rLowe = new Employee ("Rose Lowe", 12, 34);
Shop cornerShop = newShop (bSchmerl, aKaplan, rLowe);
cornerShop.payEmployees ();
cornerShop.printSummary ();
}
}
Summary of ints
|
Reference variable |
Primitive data variable |
| Type defined by |
Class definition |
Language |
| Value created by |
new |
available through underlying hardware |
| Value initialized by |
constructor |
as above |
| Variable initialized by |
Assignment of reference value |
Assignment of primitive data value |
| Variable contains |
Reference to object |
Primitive value |
| Used with |
Messages |
Operator symbols |
| Can appear as a receiver |
yes |
no |
-
print and println
-
System.out.print (50);
-
System.out.print (50 - i);
-
System.out.println (counter);
-
reading integers
-
use BasicDataReader method, readInt
-
Literals
-
2, 15, 22, -52
-
x + 2
-
-52 + j
-
rate * 85;
-
avoid magic numbers. why?
-
use constants: define an instance variable using static final, so static
final static final int SPECIALCODE = 85; ... rate * SPECIALCODE;
-
Multiple update:
-
Compound assignment:
-
a += 1; // a = a + 1;
-
counter -= 5; // counter = counter - 5
-
foo *= bar; // foo = foo * bar
-
foo /= (a + b + c)/3; // foo = foo / (a + b + c)/3;
-
Strings:
-
+ operator
-
"foo" + "bar"
-
String s; s = "foo" + s
-
String s; s += "foo"
-
"foo" + 1
-
s.trim() + counter
The double type - overview
-
floating point up to 15 digits precision
-
double d;
-
3.4
-
12.0
-
-52.0
-
-1E10
-
operators: +, -, *, /
-
2.22507385072014x10-308 to 1.79769313486231570x10+308
-
print and println
-
BasicDataReader readDouble
Last modified: 2/15/99