10

Click here to load reader

Chapter 07

Embed Size (px)

DESCRIPTION

chapter 07

Citation preview

Page 1: Chapter 07

AP Computer ScienceHomework Set 7

Inheritance

P7A. You’ve been hired to design the next generation electronic device. Write the class definition for a new type of object of your own created design that “extends” MyDevice. The class definition should:a. include a unique instance variable or variables and/or processor methods (to

justify it as a new subclass),b. include an upgraded multi-argument constructor to reflect the addition of the new

instance variable(s),c. add new getters and setters for each new instance variable(s), andd. include a toString() method that “overrides” the superclass’ toString()

method to include the values of all private instance variables in its output.

Include your new device in the ArrayList<MyDevice>. Traverse the ArrayList<MyDevice> using either a for or for-each loop and call each object’s toString() method.

The ArrayList below includes “seemingly” different objects. However, since they all “extend” MyDevice, they are all really considered “MyDevice” objects and can therefore be placed into the ArrayList<MyDevice>; that is, they can all “fit into” the ArrayList of MyDevices.

The for loop below “polymorphically” processes (prints out) the all of the devices in the ArrayList<MyDevice>. This means that the loop can treat all objects that are seemingly different (poly-morphism – “many shapes”) as MyDevice objects and call their respective toString() methods.

for( MyDevice device : inventory ) { System.out.println( device ); } // end for

/poly•mor•phism/ - n. 1. the act of referring to a subclass object using a superclass name, 2. the act of referring to objects in the general as opposed to the specific.

Page 1AP Computer Science – HW Set 7 Programs

http://thecubscientist.com

“polymorphism” in action!

We never refer to specific phones, pods, pads, etc. We only refer to them as a “device”

Page 2: Chapter 07

P7B. Consider the Television class on the next page. Write a program that will include the following:a. Write four new classes, “Plasma”, “DLP”, “LCD”, and “LED” that extend the

Television class shown below. b. For each of the above classes:

i. Write a zero argument constructor for each of the above classes. ii. Write a new two argument constructor for each of these four classes that

take a String argument for the “model” of the Television and a double for the “cost” of the television.

c. Write a driver program to create each one of these types of Televisions, including a “Television” object and store them into an ArrayList of type Television. Use a for-each loop to polymorphically call the “toString()” method for each Television in the ArrayList. Sample output is shown below:

Page 2AP Computer Science – HW Set 7 Programs

http://thecubscientist.com

Page 3: Chapter 07

public class Television { private String model; private double price; public Television() { this.model = new String(); this.price = 0.0; } // end zero-arg constructor Television public Television( String initialModel, double initialPrice ) { this.model = new String( initialModel ); this.price = initialPrice; } // end two-arg constructor Television

public void setModel( String newModel ) { this.model = newModel; } // end method setModel public String getModel() { return this.model; } // end method getModel

public void setPrice( double newPrice ) { this.price = newPrice; } // end method setPrice public double getPrice() { return this.price; } // end method getPrice public String toString() { String output = new String(); output = "The Model of this tv is: " + this.model +"\tPrice: " + this.price; return output; } // end method toString } // end class Television

Page 3AP Computer Science – HW Set 7 Programs

http://thecubscientist.com

Page 4: Chapter 07

P7C. Below is a class definition for the abstract class “SuperHero”.

public abstract class SuperHero { private String suitColor; private boolean hasCape;

public SuperHero() { this.suitColor = new String(); this.hasCape = false; } // end zero-arg constructor SuperHero

public void setSuitColor(String suitColor) { this.suitColor = new String( suitColor ); } // end method setSuitColor public String getSuitColor() { return suitColor; } // end method getSuitColor public void setCape(boolean cape) { this.hasCape = cape; } // end method setCape public boolean isCaped() { return hasCape; } // end method isCaped public abstract String motto();

} // end abstract class SuperHero

Page 4AP Computer Science – HW Set 7 Programs

http://thecubscientist.com

Page 5: Chapter 07

Part 1: Write class definitions for an “AsteroidMan”, “FriedEggMan”, and another superhero of your choosing that extends the abstract class “SuperHero”. Each of these classes should implement the “motto()” method that returns a simple motto unique to their class (create your own custom and clever mottos!)

Part 2: Write a “SuperHeroDriver” class that creates instances of an AsteroidMan, FriedEggMan, and YourHero and initializes each of them with an arbitrary suitColor and hasCape instance variable. Add each of these Superheroes to an ArrayList of type SuperHero called “heroes” and print each superhero’s suitColor, hasCape instance variable, and motto using a for-each loop.

Part 3: Create 3x3 2D array called “capedHeroes” that can hold objects of type SuperHero. For every superhero in the “heroes” ArrayList that has a cape, add that SuperHero to the “capedHeroes” 2D array starting at (0,0) AND remove that SuperHero from the “Superheroes” ArrayList. Empty “spaces” in the 2D array should be filled with the value of “null.” Use a separate nested for loop to perform this operation. Your algorithm should work for any number of heroes in the original ArrayList of “SuperHero”.

Part 4: Print the suitColor, hasCape, and motto for each hero in the “hero” ArrayList and “capedHeroes” 2D array to verify their contents after completing Part 3 above.

Sample output is shown below:

Page 5AP Computer Science – HW Set 7 Programs

http://thecubscientist.com

Page 6: Chapter 07

P7D. College Tuition! In this program, you will write an abstract class “Student” and three concrete classes, “UnderGrad” and “Graduate” (that inherit from “Student”) and “PostGraduate” that inherits from “Graduate.” The requirements for each of the classes are shown below:

Part 1: Write the class definition for the abstract class “Student.” The class definition should include private instance variables of type String to hold the student’s first name, a String for his/her major and an int to hold the number of units taken. Getter and setter methods for each of the variables should be included in the class definition. It should also include an abstract method “calculateTuition()” as shown below:

abstract public int calculateTuition(int units);

Part 2: Write a three-argument constructor in the Student class to initialize each student’s name, major, and number of units taken.

Part 3: Write the class definitions for the “UnderGrad,” “Graduate,” and “PostGraduate” classes. These concrete classes should override the abstract method “calculateTuition()” according to the following rule:

UnderGrad tuition is calculated by multiplying the number of units by $250.Graduate tuition is calculated by multiplying the number of units by $500.

Page 6AP Computer Science – HW Set 7 Programs

http://thecubscientist.com

Page 7: Chapter 07

PostGraduate tuition is calculated by multiplying the number of units by $750.

Part 4: Write a “StudentDriver” class to create two instances of each type of each concrete class, “UnderGrad”, “Graduate”, and “PostGraduate”. Add them to a 3x2 2D array named “studentBody”. Use a for or for-each loop to print each student’s name and major along with the student’s tuition based on the number of units the student is taking. Organize the Students in the 2D array as shown below:

P7E. AP 2005 #2 Ticket

Page 7AP Computer Science – HW Set 7 Programs

http://thecubscientist.com

Page 8: Chapter 07

By the end of the lesson students should be able to:a. Write the Java code to allow class B inherit from class A.

public class B extends A {

}b. Given a Java inheritance hierarchy, write the Java code to properly abstract out

common class elements (instance variables and methods) into a superclass.c. Explain that only public data members and/or methods are inherited and that

private data members and/or methods are not inherited.d. Describe the method call order in a Java inheritance hierarchy.e. Describe that class extension (inheritance) is different from class composition

(building class from existing classes) and know when to use each technique.f. Write Java code to polymorphically process objects that exist in an inheritance

hierarchy.g. Write Java methods that have polymorphic arguments and return types.h. Write the Java code to override an existing method.i. Write the Java code to overload an existing method.

Page 8AP Computer Science – HW Set 7 Programs

http://thecubscientist.com