©2005 Felleisen, Proulx, et. al.

3.7  Problem (12.4.2)

A software house that is working with a grocery chain receives this problem statement:

Develop a program that keeps track of the items in the grocery store. For now, assume that the store deals only with ice cream, coffee, and juice. Each of the items is specified by its brand name, weight (grams) and price (cents). Each coffee is also labeled as either regular or decaffeinated. Juice items come in different flavors, and can be packaged as frozen, fresh, bottled, or canned. Each package of ice cream specifies its flavor.
Design and implement the following methods:
  1. unitPrice, which computes the unit price (cents per gram) of some grocery item;

  2. lowerPrice, which determines whether the unit price of some grocery item is lower than some given amount;

  3. cheaperPerUnit, which determines whether one grocery item is cheaper than some other, given in terms of the unit cost.

3.8  Problem

Recall Homework 2, problem 2 that dealt with reading lists. Design and implement the following methods for these classes:

  1. hasOldBooks which determines whether there are any old books in the list of books (books published before 1950).

  2. allNewBooks which check if all books in the list are new books.

  3. aCount which computes how many books in the list were written by a given author.

3.9  Problem

Recall Homework 2, problem 3 that dealt with a maze game. We revise the game a little bit, so that the girl starts the game with no fortune in a room with elf. We also add the requirement that each wizard has a unique name. The wizards are parameterized by two magic numbers a and b and compute the girl's final fortune as a function of her incoming fortune x, using the formula ax + b. As before, the game ends when a monster leaves the girl with no fortune, or when the girl lands in a room containing a wizard.

Each move in the game consists of the following steps:

First, modify your existing class hierarchy to match the updated design specification.

Second, design and implement the following methods for the class hierarchy:

  1. reward which determines the girl's new fortune, based on her current fortune and the fact that she is currently in 'this' room.

  2. gameOver which checks if the game is over - again, it needs to know the girl's current fortune and the room.

  3. countRooms which counts how many rooms are in the maze.

  4. countElfRooms, countMonsterRooms and countWizardRooms which count how many rooms of each kind are there in the maze.