Page 326 - Introduction to Programming with Java: A Problem Solving Approach
P. 326
292
Chapter 7 Object-Oriented Programming—Additional Details
11.
b) Provide a complete, rewritten main method for the HeightDriver class such that the new main method uses one of the new constructors from part a) to generate this output:
6.0 ft
[after §7.9] Overloaded Constructors:
Assume that the Height class of Figure 7.10 contains only one setHeight method—the two-parameter version. Write two constructors for the Height class, one with one argument (double height), andtheotherwithtwoarguments(double heightandString units).Fortheone-argument constructor, use the default of “m” for units.
Do not duplicate any internal code. That is, have the one-parameter constructor transfer control to the two-parameter constructor, and have the two-parameter constructor transfer control to the two-parameter setHeight method.
12. [after §7.9]: Assume that the following two classes are compiled and run. What is their output? public class SillyClassDriver
{
public static void main(String[] args)
{
}
SillyClass sc = new SillyClass();
sc.display();
Apago PDF Enhancer
public class SillyClass
} // end SillyClassDriver class
{
private int x = 10;
public SillyClass()
{
}
}
this(20);
System.out.println(this.x);
public SillyClass(int x)
{
System.out.println(this.x);
System.out.println(x);
this.x = 30;
x = 40;
public void display()
{
}
int x = 50;
display(x);
System.out.println(x);