11.07.2015 Views

AP Computer Science Homework Set 9 Classes and Constructors

AP Computer Science Homework Set 9 Classes and Constructors

AP Computer Science Homework Set 9 Classes and Constructors

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>AP</strong> <strong>Computer</strong> <strong>Science</strong><strong>Homework</strong> <strong>Set</strong> 9<strong>Classes</strong> <strong>and</strong> <strong>Constructors</strong>P9A. Upgrade the Alligator program in Chapter 7 (P7A) to supply a constructor foran Alligator that initializes its “isAwake” boolean instance variable. Modify theAlligatorDriver to instantiate Alligator objects using both a no-argument constructor<strong>and</strong> a one argument constructor, creating the same output as in program P7A. Doescreating a constructor make a programmer’s life easier?P9B. Modify the Clock class from Chapter 7 (P7D), to include a three argumentconstructor for Clock, BedroomClock, <strong>and</strong> KitchenClock classes that initializes itsthree private instance variables (hours, minutes, seconds) to integer values. Modifythe ClockDriver program to create a BedroomClock <strong>and</strong> KitchenClock using thethree argument constructors, creating the same output as in program P7D.P9C. Modify the SuperHero class (P8A) to include a two-argument constructor for aSuperHero. The arguments for the constructor should be a String representing thecolor of the SuperHero’s suit <strong>and</strong> a boolean representing whether or not theSuperHero has a cape. Also modify the PantherMan, AsteroidMan, <strong>and</strong> FriedEggManclasses so that they may use the two-argument constructor defined in SuperHero.Finally, modify the SuperHeroDriver so that instances of an AsteroidMan,FriedEggMan, <strong>and</strong> PantherMan objects are instantiated using the two-argumentconstructor. The output should be the same as in program P8A.P9D. Modify the Student class (P8B) to include a two-argument constructor for aStudent. The arguments for the constructor should be a String representing theStudent’s name <strong>and</strong> a String representing the student’s major. Make the propermodifications in the UnderGrad, Graduate, <strong>and</strong> PostGraduate classes so that may usethe two-argument constructor defined in Student. Finally, modify the StudentDriverso that instances of an UnderGrad, Graduate, <strong>and</strong> PostGraduate objects areinstantiated using the two-argument constructor. The output should be the same as inprogram P8B.Page 1<strong>AP</strong> <strong>Computer</strong> <strong>Science</strong> – HW <strong>Set</strong> 9 Programshttp://thecubscientist.com


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 theTV is on. Make the proper modifications in the HDTV <strong>and</strong> BWTV classes so thatmay use the one-argument constructor defined in TV. Finally, modify the TVDriverso that instances of an HDTV <strong>and</strong> BWTW objects are instantiated using the oneargumentconstructor. 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 <strong>and</strong> local variables are stored on the stack <strong>and</strong> that objects <strong>and</strong>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 zeroargument 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 ormore argument constructor is written by the programmer.f. Explain that every constructor up an inheritance hierarchy is run at the time an object of asubclass is created.g. Write the Java code to invoke an object’s superclass constructor (with no arguments or withone or more arguments.)h. Write the Java code to invoke an overloaded constructor from another constructor using thekeyword “this”.i. Explain the difference between life <strong>and</strong> scope for local variables.j. Explain the three ways to eliminate an object’s reference (i.e. make it eligible for GC.)Page 2<strong>AP</strong> <strong>Computer</strong> <strong>Science</strong> – HW <strong>Set</strong> 9 Programshttp://thecubscientist.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!