Page 325 - Introduction to Programming with Java: A Problem Solving Approach
P. 325

                }
// end Time class
Exercises 291 //**********************************************************
<insert setHours method definition here> public Time setMinutes(int minutes)
{
this.minutes = minutes;
return this;
// end setMinutes
public Time setSeconds(int seconds)
}
{
this.seconds = seconds;
return this;
// end setSeconds
//************************************************************
}
public void printTime()
{
System.out.printf("%02d:%02d:%02d\n", hours, minutes, seconds);
} // end printTime
public class TAimpeDarigveor PDF Enhancer {
public static void main(String[] args)
{
Time time = new Time();
<insert chained-method-calls statement here> } // end TimeDriver class
}
8. [after §7.7]
a) Modify the two-parameter setHeight method in Figure 7.10 to make it test its units parameter to
see if units is equal to one of the following allowable symbols: “m,” “cm,” “mm,” “in,” or “ft.” If it is equal to one of these, set the instance variables and return true. If it is not equal to one of these, return false.
b) Does this modification require any change to any program that calls the two-argument setHeight method? Why or why not?
c) Write a statement that calls the modified method and utilizes the returned information to print an error message “Error: units not recognized” if the units argument is not one of the allowed values.
9. [after §7.8] Provide a standard three-parameter constructor for a class named JewelryItem. The class contains three instance variables—description, price, and qtyOnHand. The constructor simply assigns its three parameters to the three instance variables.
10. [after §7.9] Overloaded Constructors:
a) Add a pair of constructors to the Height class that implement the initializations provided by the two
setHeight operations in Figure 7.11. Minimize the total number of statements by having the one-parameter constructor call the one-parameter setHeight method and having the two-parameter constructor call the two-parameter setHeight method.
































































   323   324   325   326   327