1 Self-Referential Data
2 The First Methods
3 Working with lists
4 Working with self-referential data
5.3.5

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:

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.

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.