CS 2010 Homework 12   - Due November 18

Alternate Assignment

Instead of the normal HW 12 below, you may elect to implement in Java the execute function of HW 11 — no GUI, and no need to parse strings. Your execute should be a method of a DefOrAssignOrExpr class, and you'll certainly need an Expr class with a evaluate method. To test your evaluator, instantiate DefOrAssignOrExpr directly instead of parsing parenthesized input.

Students who implement a Mini Scheme interpreter in Java before the HW 12 due date will be excused from all further homework. For this alternate project, you may use whatever Java environment you prefer.

Setup

profj1.plt
Install the ProfessorJ update. To install, download profj.plt, open profj.plt using DrScheme's Open menu item, and allow DrScheme to install the package. Then restart DrScheme.

Assignment

Exercise 12.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).

Exercise 12.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.

Exercise 12.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.

Exercise 12.4, Oh My!

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

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

Exercise 12.5, Erp

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

Exercise 12.6, Rescue

Define the OzAnimal method rescue that takes no arguments and returns an oz-animal that hasn't recently eaten anyone.

Hint: the answer should be especially short, and it should use the name "no one".

Exercise 12.7, Oz Scenes

An Oz scene consists of one person and one OzAnimal. 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.

Exercise 12.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: Thursday, November 13th, 2003
mflatt@cs.utah.edu