Lab 7a
Circularly Referential Data
Goals: The goals of this lab is to learn how to design and use circularly referential data.
In this part we’ll visit a familiar concept where circular data exists – namely, lists of friends , or buddy lists. These buddy lists could be IM buddy lists, ICQ buddy lists, or lists of friends on social networks. Intuitively a buddy list is just a username and a list of other buddies; the latter part is where we get circularity.
Download the files in Lab7-Buddies.zip. The folder contains the files:
Person.java
ILoBuddy.java
MTLoBuddy.java
ConsLoBuddy.java
ExamplesBuddies.java
Create a project Lab6-Buddies and import the five files listed above into the default package. Add the tester.jar library to the project as you have done before.
All errors should have disappeared and you should be able to run the project.
Before we can design any methods for the lists of buddies, we need to be able to make examples of buddy lists.
We would like to make examples of the following circle of buddies:
Ann’s buddies are Bob and Cole
Bob’s buddies are Ann and Ed and Hank
Cole’s buddy is Dan
Dan’s buddy is Cole
Ed’s buddy is Fay
Fay’s buddies are Ed and Gabi
Gabi’s buddies are Ed and Fay
Hank does not have any buddies
Jan’s buddies are Kim and Len
Kim’s buddies are Jan and Len
Len’s buddies are Jan and Kim
If you try to make such examples, you will fail. Try it an find out why you cannot make these examples.
Design the method addBuddy that modifies the person’s buddy list by adding the given person to one person’s buddy list.
This method should be defined in the class Person and have the following purpose/effect statement and header:
// EFFECT:
// Change this person's buddy list so that it includes the given person
void addBuddy(Person buddy){...}
Add any additional methods you may need to make sure you can represent the circle of buddies given above.
If someone wants to invite a lot of friends to a party, he or she calls all people on his or her list of buddies, and asks them to invite their friends (buddies) as well, and ask their friends to invite any of their friends as well (anyone that can be reached through this network of friends.
We call those on the person’s buddy list the direct buddies and the others that will also be invited to the party the distant buddies.
Now we would like to ask some pretty common questions. For each question design the method that will find the answer. The purpose/effect statements and the headers for the methods are already given:
Does this person have another person as a direct friend?
// returns true if this Person has that as a direct buddy
boolean hasDirectBuddy(Person that)
How many direct buddies do the two persons have in common?
// returns the number of people that are direct buddies
// of both this and that person
int countCommonBuddies(Person that)
- Will the given person be invited to a party organized by someone?
// will the given person be invited to a party
// organized by this person?
boolean hasDistantBuddy(Person that)
Homework Assignment
Design the method that provides the answer to the following question:
How many people will be at the party if all those a person invites (directly or indirectly) show up?
// returns the number of people who will show up at the party |
// given by this person |
int partyCount() |
Follow the Design Recipe!