CS 1410-20 Homework 9

Due: Friday, November 5th, 2010 9:40am

Although you implement this assignment in Java with Eclipse, handin your work using DrRacket. The handin server will not try to run any tests. The handin server will call your submitted file hw.java; unfortuately, however, the file will be saved in DrRacket’s non-text format.

Use the subset of Java that we have covered in class. In particular, use the = assignment operator only in constructors.

Part 1 – Lions

The relevant properties of a lion are, in order

  1. The number of teeth it has
  2. The name of the person it most recently ate (maybe "no one")

Here is an initial class definition:

  class Lion {
    int teeth;
    String ate;
    Lion(int teeth, String ate) {
       this.teeth = teeth;
       this.ate = ate;
    }
  }

Implement the Lion method moreScary, which produces a lion with two additional teeth (but the same stomach contents).

Part 2 – and Tigers

Design a representation for tigers, where the relevant properties of a tiger are

  1. The number of stripes it has (an integer)
  2. The length of its claws in centimeters (a double)
  3. The name of the person it most recently ate (maybe "no one")

Use Tiger as the name of your class, and put the constructor arguments in the above order.

Implement the Tiger method moreScary, which produces a tiger (with the same fur pattern and stomach contents) whose claws are one centimeter longer.

Part 3 – and Bears

Design a representation for bears, where the relevant properties of a bear are

  1. Its fur color, as a string
  2. The name of the person it most recently ate (maybe "no one")

Use Bear as the name of your class, and put the constructor arguments in the above order.

Implement the Bear method moreScary, which produces a bear (with the same fur color) that hasn’t eaten recently.

Part 4 – Oh My!

Define the interface IOzAnimal and adjust your earlier class declarations so that an Oz animal is a lion, tiger, or bear.

Add the IOzAnimal method moreScary, which produces a scarier animal (as above). Adjust the return types of previoulsy implemented methods if necessary.

Part 5 – Erp

Define the IOzAnimal method feed that takes a string for a person’s name. The result should be an IOzAnimal that just ate the person whose name is given.

Part 6 – Rescue

Define the IOzAnimal method rescue that takes no arguments and returns an IOzAnimal that hasn’t recently eaten anyone.

Part 7 – Oz Scenes

An Oz scene consists of one person and one IOzAnimal. It is represented as an OzScene:

  class OzScene {
    String person;
    OzAnimal animal;
    OzScene(String person, OzAnimal animal) {
      this.person = person;
      this.animal = animal;
    }

Implement the OzScene method moreScary, which takes no arguments and produces one with a scarier animal and the same person.

Part 8 – A Wizard of Oz

An Oz scene moves the next scene as follows:

Implement the OzScene method next, which takes no arguments returns the next OzScene according to the rules above.

Don’t forget to break up the problem into smaller pieces. For example, you might find an animalAte method of OzAnimal useful (that takes no arguments returns a string for the person that the animal recently ate).


Last update: Monday, November 1st, 2010
mflatt@cs.utah.edu