UW Home     CSE Home   Announcements    Message Board    Contact Info 

 
 

Homework #5

Due: Friday, March 5. The written part of this assignment is due by 5:00 p.m. Hand it in during lecture on Friday or in the CSE main office in the Allen Center by 5:00 p.m. The programming part is due online by 9:00 p.m. No late asssignments will be accepted.

This homework is meant to give you a bit of practice thinking about inheritance while you get started with the final project. We also hope to clear up some of the lingering confusion about return statements vs. printed output.

1. (written exercise only) (a) Suppose we are writing the following class to model a playing card.

/** Model of a playing card*/
public class Card {
   private String suit;   // card suit; one of "spade", "heart", 
                          // "club", or "diamond"
   ...
}

Write a getSuit() method to return the suit of the card (assuming it has already been initialized in the constructor), and write a printSuit() method that prints the suit of the card on System.out.

(b) Now suppose we're writing a different class, say CardPlayer, and we want to discover whether a card is red or black. Write the code for a method isRed(Card c) that returns true if c is a heart or a diamond and returns false otherwise. This method only has access to public methods defined in Card. Which of the two methods defined in part (a) did you use in your implementation of isRed and why did you pick that one?

2. (written exercise only) Suppose you are designing classes to represent vehicles for a traffic simulation system. In particular, we want classes to represent cars, trucks, and busses. For this problem we will explore the design of these classes only. There is no Java coding involved.

(a) For each of the individual classes Car, Truck, and Bus, list at least 4 properties that would be appropriate for the class, and at least 4 responsibilities (methods) that it can perform. There should be some differences between the properties and responsibilities in the different classes, i.e., they should not all be the same, but there should be some that are common to all of the classes. You should design these classes independently of each other for this part of the problem.

(b) Now take your answer to part (a) and expand on your design using inheritance to factor out common properties and responsibilities from the different classes and put them in a parent class. You should design a class Vehicle to contain the common information. The classes Car, Truck, and Bus should extend Vehicle, inheriting the information declared there and containing additional properties and responsibilities as needed to include all of the design from part (a). Be sure to make it clear which methods in Vehicle are abstract and which are actually implemented in Vehicle.

3. (to be implemented on the computer) For this problem, design and implement a set of small classes to model some characters from the movie The Wizard of Oz, including Dorothy, the Scarecrow, and the Cowardly Lion. All of these characters can report who they are, sing, skip down the Yellow Brick Road, tell you how many times they have skipped, and they all have a favorite saying. When asked who they are, they reply with their character's name. When asked to sing, all of them reply with "Ding! Dong! The witch is dead!". When asked to skip a certain number of times, they all reply with "skip skip skip skip", where the word "skip" appears as many times as they were asked to skip. They can all report the total number of times they have skipped. Finally, they all have a favorite saying, such as "There's no place like home" for Dorothy, "If I only had a brain" for the Scarecrow, and "If I only had courage" for the Cowardly Lion.

More specifically, each of the Java objects that model a character should implement the following methods

/** Return the name of this character */
public String getName()
		
/** Return words from the song that this character sings */
public String sing()

/** Return a string with "skip" appearing n times
 * @param n  the number of times to skip */
public String skip(int n)
 
/** Return the total number of skips this character has done
 * @return sum of all of the times this character has skipped */
public int getSkips()

/** Return this character's favorite saying */
public String getSaying()

Notice that none of these methods actually print anything on System.out. Instead they return a String which, if desired, could be printed by the caller.

(a) Implement a base class CastMember and classes Dorothy, Scarecrow, and Lion that extend CastMember in appropriate ways so they have the behavoir described above. In particular, be sure that items common to all of the characters are included in CastMember and inherited by the extended classes. Methods in CastMember should be either implemented there, or should be abstract and implemented in the extended classes - part of your job is to decide which to do. You may add additional extended classes to represent additional characters from the movie if you wish.

(b) Create a class Oz that contains a main method. When executed, this method should create instances of each of the different character objects and store them in an ArrayList. After the list has been filled, the main method should go through the list and have each of the characters skip a few times (the number of times should be different for each character, say 1, 2, 3, ..., or can be chosen randomly; and each character's skip() method can be called once or several times). After all of the characters have skipped at least once, the main method should go through the list one last time and print out each character's name, song, saying, and the total number of times the character has skipped.

What to Turn in

Turn in written answers to the first two questions either in lecture or in the CSE main office on the due date given at the top of the page by 5 pm. Turn in your Java code for the last question using this online turnin form by 9 pm that day.