Homework 3
Home Up

 

HOMEWORK #3

Given: March 1, 2001

Due: March 22, 2001

 

 

PROBLEM #1:

PURPOSE: To gain experience in developing interfaces and class hierarchies that work together as a system.

Develop a system for keeping track of kinds of animals. The following should be the top-level class for the animals themselves:

public abstract class Animals //This class is abstract because you will never see anything that is just "an

              //animal" in nature.

{

public Animal (String name)

{

      d_name = name;

      }

      public String getName()

      {

      return d_name;

      }

      public abstract String getType (); // return type of animal: eg, mammal, bird, fish

      public abstract String getAnimalSpecies(); // return species of animal: e.g., dog, cat

      private String d_name;

}

You should include classes for the following animal types: dog, bat, parakeet, ducks, catfish.

Let's assume for the sake of consistency that:

Dogs run and swim.

Bats fly only.

Parakeets fly and run.

Ducks fly, run, and swim.

Catfish swim only.

You should also include abstract classes for types of animals such as mammal, bird, and fish.

These classes are abstract too because you will never see just a plain bird, fish, or mammal in nature.

You will have to produce an inheritance hierarchy involving the Animal class and your classes for animal types.

Produce interfaces for FlyingAnimal, SwimmingAnimal, and RunningAnimal. Make the appropriate animal classes implement these interfaces.

Finally, develop test scaffolding via a separate class ( for example, TestCase.java) that creates instances of the following classes and displays the type, name, and species of those instances: dog, bat, parakeet, duck, and catfish.

There are a number of ways to do this assignment. To help you get started, I have written a .java file for the duck to give you an idea of what you should have.

public class duck extends Bird implements SwimmingAnimal, RunningAnimal, FlyingAnimal

{

public duck(String name)

{

super(name);

}

public String getAnimalSpecies()

{

return "species somateria fischeri known as duck.";

}

public String swim()

{

return "on the surface of the water";

}

public String run()

{

return "waddling on webbed feet.";

}

public String fly()

{

return "swiftly during hunting season.";

}

 

public void showinfo()

{

System.out.println("My animal is of "+this.getAnimalSpecies());

System.out.println("My animal's type is "+this.getType());

System.out.println("My animal's method of swimming is "+this.swim());

System.out.println("My animal's method of running is "+this.run());

System.out.println("My animal's method of flying is "+this.fly());

System.out.println("My animal is "+this.getName());

}

}

PROBLEM #2

PURPOSE: Deal with more complex objects and also deal with data structures in Java.

Suppose we wish to keep track of the birthdays of students in a class as follows:

We are going to keep an ordered linked list of Birthday objects, where the first Birthday object in the list is the earliest birthday of the year, and the last Birthday object is the latest birthday of the year.

Each Birthday object should contain the following private variables:

Day thisDay; //Object of type Day indicating which birthday this is.

Integer howmay //Integer indicates how many students have this birthday.

The code for the Birthday and Day classes shall be given to you. The java class that you design shall create the linked list and provide the test scaffolding that runs in a loop and provides the following options:

  1. Add a birthday to the linked list. If the date is already represented by a Birthday object in the list, then you just need to increment the "howmany" variable associated with it.
  2. Delete a birthday from the linked list (if it is in the list).
  3. Print all birthdays in the list and how many students have that birthday.

Use the java.util.LinkedList class to form your linked list of Birthday objects.

The minimum and maximum number of students is indeterminate.

You can modify/add/delete the methods of both the Birthday and Day classes if you like. The reason they are being furnished is to help you with your assignment. The main point is that each Birthday object contains private variables of type Day (which day is this?) and integer (how many students have this birthday?). If you modify either the Birthday or Day classes, send me a readme.txt indicating what you did, and also send your customized Day and Birthday classes along with the java class that provides the test scaffolding.

 

NOTE: I know that these two assignments can be done many different ways, and I will therefore not be as strict in enforcing a certain implementation as I have been in the past two assignments. If you have questions about whether or not you are attacking the problem correctly, please ask!


Copyright © 2000-2001 First Principles. All rights reserved.
For problems or questions regarding this web contact webmaster.
Last updated: 07 June 2001 23:06:27 -0400.