Page 271 - Introduction to Programming with Java: A Problem Solving Approach
P. 271
a) Write i.
ii. iii. b) Write i. ii. iii. iv.
the following methods for class Town:
An initialize method that establishes initial values of instance variables. Assume that initialize gathers all the data it needs by prompting for and inputting values from a user. A simulateBirth method that simulates the birth of one child.
A printStatistics method that prints out the current vital statistics.
a main method for a separate driver class that does the following:
Creates a town named newHome
Calls initialize to establish initial values of instance variables for newHome. Simulates the birth of a pair of twins.
Prints out newHome’s vital statistics.
6
********************************************************/
7
8
public class PcDesignDriver
9{
}
System.out.println("object == object? " + (xW == yW));
System.out.println("value == value? " +
(xW.doubleValue() == yW.doubleValue()));
System.out.println(
"object.equals(object)? " + xW.equals(yW));
System.out.println("object.compareTo(object)? " +
xW.compareTo(yW));
yW = new Double(y + 3.0);
System.out.println("object.compareTo(largerObject)? " +
xW.compareTo(yW));
yW = new Double(Double.NEGATIVE_INFINITY);
System.out.println("-infinity isInfinite()? " +
yW.isInfinite());
// end main
Double xW = new Double(x);
Double yW = new Double(y);
Exercises 237 // the object: wrapped x
// the object: wrapped y
} // end Wrapper class
Compile and run this program, and display the output. Read about the Double class in Sun’s documentation, and explain why each of the outputs comes out the way it does.
5. [after §6.4] Suppose you have a Town class that describes the demographics of small towns. The vital statistics described by Athispclassgaroe numPbeDrFOfAdEulntshanad numcberrOfChildren. These vital statistics are encapsulated and not directly accessible from outside the class.
6. [after §6.7] Given this PcDesign program:
1 /********************************************************
2 * PcDesignDriver.java
3 * Dean & Dean
4*
5
* This exercises the PcDesign class.
10 public static void main(String[] args)
11 {
12 PcDesign myPc = new PcDesign();