12.07.2015 Views

HW Set 7 - thecubscientist.com

HW Set 7 - thecubscientist.com

HW Set 7 - thecubscientist.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

P7B. Consider the Television class on the next page. Write a program that will includethe following:a. Write four new classes, “Plasma”, “DLP”, “LCD”, and “LED” that extend theTelevision 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 thattake a String argument for the “model” of the Television and a doublefor the “cost” of the television.c. Write a driver program to create each one of these types of Televisions, includinga “Television” object and store them into an ArrayList of type Television. Use afor-each loop to polymorphically call the “toString()” method for eachTelevision in the ArrayList. Sample output is shown below:Page 2AP Computer Science – <strong>HW</strong> <strong>Set</strong> 7 Programshttp://<strong>thecubscientist</strong>.<strong>com</strong>


public class Television{private String model;private double price;public Television(){this.model = new String();this.price = 0.0;} // end zero-arg constructor Televisionpublic Television( String initialModel, double initialPrice ){this.model = new String( initialModel );this.price = initialPrice;} // end two-arg constructor Televisionpublic void setModel( String newModel ){this.model = newModel;} // end method setModelpublic String getModel(){return this.model;} // end method getModelpublic void setPrice( double newPrice ){this.price = newPrice;} // end method setPricepublic double getPrice(){return this.price;} // end method getPricepublic 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 TelevisionPage 3AP Computer Science – <strong>HW</strong> <strong>Set</strong> 7 Programshttp://<strong>thecubscientist</strong>.<strong>com</strong>


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 SuperHeropublic void setSuitColor(String suitColor){this.suitColor = new String( suitColor );} // end method setSuitColorpublic String getSuitColor(){return suitColor;} // end method getSuitColorpublic void setCape(boolean cape){this.hasCape = cape;} // end method setCapepublic boolean isCaped(){return hasCape;} // end method isCapedpublic abstract String motto();} // end abstract class SuperHeroPage 4AP Computer Science – <strong>HW</strong> <strong>Set</strong> 7 Programshttp://<strong>thecubscientist</strong>.<strong>com</strong>


Part 1: Write class definitions for an “AsteroidMan”, “FriedEggMan”, and anothersuperhero of your choosing that extends the abstract class “SuperHero”. Each ofthese classes should implement the “motto()” method that returns a simple mottounique to their class (create your own custom and clever mottos!)Part 2: Write a “SuperHeroDriver” class that creates instances of anAsteroidMan, FriedEggMan, and YourHero and initializes each of them with anarbitrary suitColor and hasCape instance variable. Add each of these Superheroesto an ArrayList of type SuperHero called “heroes” and print each superhero’ssuitColor, hasCape instance variable, and motto using a for-each loop.Part 3: Create 3x3 2D array called “capedHeroes” that can hold objects of typeSuperHero. For every superhero in the “heroes” ArrayList that has a cape, addthat SuperHero to the “capedHeroes” 2D array starting at (0,0) AND remove thatSuperHero from the “Superheroes” ArrayList. Empty “spaces” in the 2Darray should be filled with the value of “null.” Use a separate nested for loop toperform this operation. Your algorithm should work for any number of heroes in theoriginal 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 <strong>com</strong>pletingPart 3 above.Sample output is shown below:Page 5AP Computer Science – <strong>HW</strong> <strong>Set</strong> 7 Programshttp://<strong>thecubscientist</strong>.<strong>com</strong>


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.” Therequirements for each of the classes are shown below:Part 1: Write the class definition for the abstract class “Student.” The class definitionshould include private instance variables of type String to hold the student’s firstname, 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 classdefinition. It should also include an abstract method “calculateTuition()” asshown below:abstract public int calculateTuition(int units);Part 2: Write a three-argument constructor in the Student class to initialize eachstudent’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.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 eachconcrete class, “UnderGrad”, “Graduate”, and “PostGraduate”. Add them to a3x2 2D array named “studentBody”. Use a for or for-each loop to print eachstudent’s name and major along with the student’s tuition based on the number of unitsthe student is taking. Organize the Students in the 2D array as shown below:P7E. AP 2005 #2 TicketPage 6AP Computer Science – <strong>HW</strong> <strong>Set</strong> 7 Programshttp://<strong>thecubscientist</strong>.<strong>com</strong>


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<strong>com</strong>mon class elements (instance variables and methods) into a superclass.c. Explain that only public data members and/or methods are inherited and thatprivate 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 <strong>com</strong>position(building class from existing classes) and know when to use each technique.f. Write Java code to polymorphically process objects that exist in an inheritancehierarchy.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 7AP Computer Science – <strong>HW</strong> <strong>Set</strong> 7 Programshttp://<strong>thecubscientist</strong>.<strong>com</strong>

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

Saved successfully!

Ooh no, something went wrong!