CSE 142 Spring 2002

Homework #2

Due: Electronically, 11:00pm, Sunday, April 14;
written report due in lecture, Monday, April 15.


Purpose

The purpose of this assignment is to gain experience creating and implementing new classes.  There are two sections to this assignment, each of which require implementing two new classes.  You are only required to implement the classes in one section or the other, however we highly suggest doing both sections to gain additional experience - the only way to really learn how to program.  If you turn in the classes for both sections, we will grade the first files in your list (your choice).

This writeup describes the kinds of objects (classes) you need to implement and the messages those objects should understand (i.e., methods that need to be included in the classes).  Part of your job is to decide what state information (instance variables) each object should contain, and how that state should be updated when the object's methods are called.  Each class should include an appropriate toString() method that returns a nicely formatted String representation of the object and its state.

As you design your classes you should think about how you can test them to verify that they are working properly.  You should also think about the order in which you implement and test the classes - what can you do first before implementing anything else?  How will you add to the initial part to create the final version(s) of the classes?

Your solution will be graded both on whether it works properly, and on how well it is written.  Be sure to include appropriate JavaDoc comments to document public methods, include comments to describe the instance variables and chose good names for them, and indent the code neatly.

Section A

(This section or section B is required, not both, but doing both would be a good way to gain experience.)

For this section, implement classes of objects to keep track of the cumulative times of a racer for an event like skiing, where the total time over several races determines the racer's final standing.  There are two classes that you need to define.  An instance of class Timer represents a time interval.  An instance of class Racer represents a racer.  The state of each Racer object includes a Timer object, which records the total time associated with that Racer.

Class Timer

When a Timer object is created, its state should be initialized to an accumulated time of 0.  A Timer object should respond to messages to increase the time by a specified amount.  The class should include the following constructor and methods:

/** Construct a new Timer with an accumulated time of 0 */
public Timer()

/** Increase this timer by the given number of hours, minutes, and seconds */
public void increaseBy(int hours, int minutes, int seconds)

/** Return a String representation of this Timer */
public String toString()

Class Racer

A Racer object has a name, country, id number, and should contain a Timer that records the total time associated with this Racer.  It should respond to messages to add a new time to the Racer's total time, and return the Timer object representing the Racer's total time when requested.  The Racer object should not do the actual arithmetic to calculate the total time; that should be done by the associated Timer object.  The String representation returned by toString() should include the Racer's total time as well as other state information.

/** Construct a new Racer with the given name, country, and idNumber and
 *  an total time of 0. */
public Racer(String name, String country, int idNumber)

/** Add the time represented by Timer c to this Racer's total time */
public void addRaceResult(Timer c);

/** Return this Racer's total time as a Timer object */
pubic Timer getTotalTime()

/** Return a String representation of this Racer, including the total time */
public String toString()

Section B

(This section or section A is required, not both, but doing both would be a good way to gain experience.)

In this section, implement a pair of classes that could be used to keep track of information in a kitchen.  Class Recipe represents the ingredients needed for a recipe; class Kitchen represents the ingredients on hand in a kitchen, can answer whether it has enough of the ingredients needed for a particular recipe, and can update the quantities on hand after a recipe has been prepared.

Class Recipe

A recipe object has a name and information about the ingredients needed to prepare it.  Everything is measured in ounces.  Its toString() method should return a readable description of the recipe, including the ingredients needed.

/** Construct a new Recipe with the given name and ingredients */
public Recipe(String name, double flour, double sugar, double butter)

/** Return the amount of the named ingredient required by this recipe */
public double getFlourNeeded()
public double getSugarNeeded()
public double getButterNeeded()

/** Return a String representation of this recipe */
public String toString()

Class Kitchen

A Kitchen is initially empty.  It responds to messages to buy flour, sugar, and butter, which increases the amount of these ingredients on hand.  It can report (true or false) whether it has sufficient ingredients available to make a particular Recipe, and it can update the ingredient amounts to reflect the amount consumed when a Recipe is prepared.

/** Construct a new Kitchen with the given name and no flour, sugar, or butter */
public Kitchen(String name)

/** Increase the amount of flour, sugar, or butter on hand by the given amount */
public void buyFlour(double amount)
public void buySugar(double amount)
public void buyButter(double amount)

/** Return true if there are enough ingredients on hand to make the given Recipe */
public boolean canMake(Recipe r)

/** Decrease the amount of flour, sugar, and butter by the amounts needed to make
 *  the given Recipe */
public void make(Recipe r)

/** Return a String representation of this Kitchen */
public String toString()

Turning in the Project

Use the link at the end of this section to turn in your Java source files online.  If  you decide to do both parts of the assignment, turn in all of the files for both parts at the same time, with the ones you want graded listed first.  Files must be turned in by 11:00 pm, Sunday, April 14.

Electronically turn in your .java file(s).  Turn-in form.

Written Report

In lecture on Monday, April 15, you should turn in a short (no more than one page) written report about your project.  Include your name, section, homework number and date at the top.  Briefly describe the classes you defined for your project.  Explain what worked and what didn't work in your code.  Describe the most important things you learned from this project, which could be new ideas that you have never seen before, or misconceptions that you had that the project helped you straighten out.