Lab 2
Goals: The goals of this lab is to review data definitions and practice designing self-referential classes and data, then design methods for a variety of class herarchies.
For this lab, there are no starter flies. For each problem, start a new project and build the files from scratch.
However, these problems require a lot ot typing that does not contribute to learning new concepts. To help you, we provide nearly complete solution to most of these problems. If you have made a number of data definitions and want to just continue, copy what you need from the solutions files and move on to the next part. Of course, if you rely on the solutions, you will not learn how to work out the problems yourself.
All the files you will need are in this one Lab2.zip file. You can download it once and extract from it the files you need later on.
1 Self-Referential Data
The youth soccer coach need to notify all players if the game is cancelled due to rain, and so she set up a phone chain. The coach calls the team captain who starts the phone chain by calling two other players on the team. Every player who receives the call then calls two other players (or one or none, if the player is at the end of the phone chain). This continues until all players have been called.
Here is the phone chain for the Astros team:
Jen(captain) |
/ \ |
May Bea |
/ \ / \ |
Kim Pat Ann Joy |
/ \ / \ / \ / \ |
Tay Zoe Meg Lou Cam Eve Tam EMPTY |
/ \ / \ / \ / \ / \ / \ / \ |
;; An Phone Chain (PC) is one of |
;; -- EMPTY |
;; -- (make-link Player PC PC) |
|
A Player is (make-player String String) |
(define-struct (player name phone-num)) |
Convert this data definition into Java classes and interfaces. Make examples of several phone chains including the representation of the phone chain shown above.
2 The First Methods
Our classes represents pets and pet owners as follows:
// to represent a pet owner |
class Person { |
String name; |
Pet pet; |
int age; |
|
Person(String name, Pet pet, int age) { |
this.name = name; |
this.pet = pet; |
this.age = age; |
} |
} |
// to represent a pet |
interface Pet { } |
|
// to represent a pet cat |
class Cat implements Pet { |
String name; |
String kind; |
boolean longhaired; |
|
Cat(String name, String kind, boolean longhaired) { |
this.name = name; |
this.kind = kind; |
this.longhaired = longhaired; |
} |
} |
|
// to represent a pet dog |
class Dog implements Pet { |
String name; |
String kind; |
boolean male; |
|
Dog(String name, String kind, boolean male) { |
this.name = name; |
this.kind = kind; |
this.male = male; |
} |
} |
Follow the design recipe to design the following methods for these classes:
Make examples fo at least four pet owners - two for each of the pets, as you need to make sure you cover the two boolean values.
Design the method with the following purpose and header:
// is this person older than the given person?
boolean isOlder(Person other)
Design the method sameNamePet that has the following purpose statement:
// does the name of this person's pet match the given name?
There are several things to watch out for in this problem.
When you make the template for the class Person you will see, that you do not know anything about the pet’s name.
You will need a helper method for the class hierarchy that represents Pets that will check whether pet’s name matches the given name.
But, to design such method, you first need to define the purpose statements and the header in the interface Pet. Java requires that this method be declared public - though at this time, for our simple programs, this designation is irrelevant.
You then need to define this method in every class that implements the interface. (We will soon learn how we can avoid the extra work.)
Some people do not have pets, and sometimes the pets perish. Add a new class to this class hierarchy so that we can represent persons who do not have a pet.
Make sure you add new examples!
Design the method perish in the class Person that produces a person whose pet has perished.
3 Working with lists
Start a new project and copy into it all the files you have already defined for the Persons and Pets. Add the necessary class and interface definitions so that a person may have more than one pet.
Draw the class diagram for the entire collection of classes and interfaces.
We need to make a small change. We need to change the method sameNamePet to hasPetNamed that determines wehther the person has a pet with the given name.
Well, it is not a small change. Make sure you change all that is needed, add templates wehrever necessary, change the test cases, etc.
You may notice that we no longer need the class NoPet as not having any pet means the person’s list of pets is an empty list.
For now, just eliminate the method perish.
To modify the method so it would work with the list of pets would require removing the pet with the given name from the list of pets this person owns.
If you have some time left, finish this task as well.
4 Working with self-referential data
Go back to the your examples of the soccer-player’s phone chain.
Design the method countPlayers that will count how many players rely on this person’s phone call (including self). If the person is not called a numebr of players on the team will not learn of the game’s cancellation.
Design the method willCall(String) that determines whether this player will call a player with the given name.
Answer yes, if this player’s name matches the given name.
Design the method longestChain that counts longest sequence of phone calls that have to be made to reach any of the players.
Note: Throughout this lab make sure you follow the rule one task – one method and design each method in the class that should be responsible for it.