©2008 Felleisen, Proulx, et. al.

6.1  The World

Our projects that extended the World contained three import statements, indicating that we need to use classes defined in three different libraries written by someone else.

To run a project that extends the World in Eclipse (or any other Java 1.5 IDE) we need to save a copy of the relevant library files in a folder, and specify in the project properties the file path to these libraries.

Managing the Libraries

Configuring a Project with the World Library

Start a new project BlobWorld. Import the .java files from the BlobWorld folder. Notice that the files are marked with a number of errors. You need the World library.

To work with the libraries you need to add the three Variables you defined earlier to this project. The process is similar to what you did earlier:

You can now run your BlobWorld project. The key controls the movement of the ball, but the timer also moves the ball randomly on each tick. The world ends when the ball moves out of bounds.

Make sure you can run the project. Read the code, to see the design. It is nearly the same as what you have done in ProfessorJ. The method runWorld is invoked as a part of the test suite.

Making a Better World

Change the class TimmerWorld so that the world now consists of two Blobs, one that moves randomly on tick, and another that is controlled by the user through the arrow key events. Add the code that ends the world (using the endOfWorld method) when the two Blobs are overlapping (the distance between their centers is smaller than the sum of their radii). Follow the Design Recipe.

6.2  Quiz

6.3  Circular Data

We will now see in practice how to deal with circularly referential data similar to what we have seen in lectures.

In this part we’ll visit a familiar concept where circular data exists – namely, buddy lists. These buddy lists could be IM buddy lists, ICQ ubuddy lists, or lists of friends on social networks. Inuitively a buddy list is just a username and a list of other buddies; the latter part is where we get circularity. So, start by working with the following files:

  1. Create a project LabBuddies and import the four 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.

  2. Before we can design any methods for the lists of buddies, we need to ba able to make examples of buddy lists.

    Design the method add that adds a buddy to one person’s buddy list. Add any additional methods you may need to make sure you can represent the following circle of buddies:

    • Tom’s buddies are Jan and Tim

    • Tim’s buddies are Dan and Jan and Tom

    • Jan’s buddies are Tom and Tim

    • Dan’s buddy (only one) is Tim

  3. Now we would like to ask some pretty common questions, e.g

    • Does this person have this other person as a direct friend?

    • How many buddies do two buddies have in common?

    • Does this person have this other person as a "friend-of-a-friend"?

    The purpose statements and the mehtod headers for the three methods are already given:

    // returns true if this has that as a direct buddy
    boolean hasDirectBuddy(Buddy that)
    // returns the number of buddies this and that have in common
    int countCommonBuddies(Buddy that) 
    // returns true if this has that as a direct or distant buddy
    boolean hasDistantBuddy(Buddy that)

    Start by implementing hasDirectBuddy. Follow the Design Recipe!

    If you have time remaining, design the other two methods as well.

Last modified: Monday, February 18th, 2008 11:30:23pm