Chapter 9 – Life and Death of an Object

advertisement
AP Computer Science
Homework Set 9
Classes and Constructors
Song
Clock
Card
P9A. Upgrade the Alligator program in Chapter 7 (P7A) to supply a constructor for
an Alligator that initializes its “isAwake” boolean instance variable. Modify the
AlligatorDriver to instantiate Alligator objects using both a no-argument constructor
and a one argument constructor, creating the same output as in program P7A. Does
creating a constructor make a programmer’s life easier?
P9B. Modify the Clock class from Chapter 7 (P7D), to include a three argument
constructor for Clock, BedroomClock, and KitchenClock classes that initializes its
three private instance variables (hours, minutes, seconds) to integer values. Modify
the ClockDriver program to create a BedroomClock and KitchenClock using the
three argument constructors, creating the same output as in program P7D.
P9C. Modify the SuperHero class (P8A) to include a two-argument constructor for a
SuperHero. The arguments for the constructor should be a String representing the
color of the SuperHero’s suit and a boolean representing whether or not the
SuperHero has a cape. Also modify the PantherMan, AsteroidMan, and FriedEggMan
classes so that they may use the two-argument constructor defined in SuperHero.
Finally, modify the SuperHeroDriver so that instances of an AsteroidMan,
FriedEggMan, and PantherMan objects are instantiated using the two-argument
constructor. The output should be the same as in program P8A.
P9D. Modify the Student class (P8B) to include a two-argument constructor for a
Student. The arguments for the constructor should be a String representing the
Student’s name and a String representing the student’s major. Make the proper
modifications in the UnderGrad, Graduate, and PostGraduate classes so that may use
the two-argument constructor defined in Student. Finally, modify the StudentDriver
Page 1
AP Computer Science – HW Set 9 Programs
http://thecubscientist.com
so that instances of an UnderGrad, Graduate, and PostGraduate objects are
instantiated using the two-argument constructor. The output should be the same as in
program P8B.
P9E. Modify the TV class (P8C) to include a one-argument constructor for a TV.
The argument for the constructor should be a Boolean representing whether or not the
TV is on. Make the proper modifications in the HDTV and BWTV classes so that
may use the one-argument constructor defined in TV. Finally, modify the TVDriver
so that instances of an HDTV and BWTW objects are instantiated using the oneargument constructor. The output should be the same as in program P8C.
By the end of the lesson students should be able to:
a.
State that method invocations and local variables are stored on the stack and that objects and
its instance variables are stored on the heap.
b.
Explain why the heap is also known as the “Garbage Collectible Heap”.
c.
Write the Java code to construct an object using the keyword “new” (i.e. write a zero
argument AND one or more argument constructor for an object.)
d.
Write the Java code for overloaded object constructors.
e.
Explain that a programmer needs to explicitly write a zero argument constructor if a one or
more argument constructor is written by the programmer.
f.
Explain that every constructor up an inheritance hierarchy is run at the time an object of a
subclass is created.
g.
Write the Java code to invoke an object’s superclass constructor (with no arguments or with
one or more arguments.)
h.
Write the Java code to invoke an overloaded constructor from another constructor using the
keyword “this”.
i.
Explain the difference between life and scope for local variables.
j.
Explain the three ways to eliminate an object’s reference (i.e. make it eligible for GC.)
Page 2
AP Computer Science – HW Set 9 Programs
http://thecubscientist.com
Download