CMSC 10200 Lab — Spring 2006
Lab Assignment 4

This lab is due 11:59pm on Sunday.

Cleveland's Diner II

This lab builds upon lab 3. You will add several new classes to your Diner project. This lab will introduce you to using the Java API 1.5.0. You will need to make sure Eclipse is set to Java 5.0 compliant.

Your grade for this lab depends on

  1. Fixing problems in your menu package from last week.
  2. Quality of your code.
    1. Method bodies must be short (anything more than 10 lines is too long.)
    2. Proper use of generic Java Collection classes (described below.)
  3. Thorough testing of your classes.
  4. Appropriate use of external comments for the user of your classes (see Javadoc). This is for all the classes in your Diner package. If you followed the style in lab3, your javadoc comments for last week's classes in menu are already satisfactory.
  5. Extra Credit: If you did the Extra Credit for lab3 last week, you should continue to write this week's lab using Exceptions where appropriate. I will give you a little extra for this. If you would like to add exception handling to your lab3 code, you may recieve extra credit this week for doing this.

Omlette and OmlettePlate Classes

Cleveland's Diner offers omlette's with a wide choice of toppings:

You will create two classes: Omlette and OmlettePlate in the menu package. The Omlette class must have
  1. public enum Topping{ ... }: a single enum class with all 21 possible toppings. (I hear you groaning, so see enum classes below for a hint how to avoid a big switch.)
  2. public Omlette(Topping[] ts): a constructor which accepts an Array of toppings. Here are some things that you must consider in constructing your Omlette object
    1. If ts is null you should create a plain Omlette (one with no toppings.)
    2. A topping only counts once on an omlette, although ts could include the same topping several times.
    3. ts could contain null references, but this should never lead to an exception. You should ignore null references on ts and not try to add them to the Omlette object.
    4. See the HashSet generic Java class for keeping track of the toppings.
The OmlettePlate class is similar to the EggPlate from lab 3. If you wrote your code using Exceptions, you should use Exceptions in this class as well.

Order Class

You will need to add an Order class to your Diner project. The Order class must not go into the menu package, but should be created outside this package. (Eclipse will create a default package for this class.) You will need to import the menu package into the Order class, by adding the line

import menu.*;
Without this, you would have to prefix all classes from the menu package with menu., as in
menu.Omlette.Topping.CHEDDAR

The Order class constructs a list of Items, which are added one-by-one using an add method. The class must have the following methods

You will find the LinkedList generic Java class useful for implementing a list of Items.

Strategies for Implementation

Collections

A Collection<E> is a generic Java interface class. You will find a summary of the methods that are part of all Collections in the Java API. There are two Collections which you will find useful for this lab (links are to the Java API)

  1. LinkedList<E>: implements a list of Objects.
  2. HashSet<E>: implements a set of Objects--each Object can only occur once in the collection.
Arrays are not Collections.

The Collection interface includes methods for performing some basic operations on collections of objects. Since "collection" is a very general concept, operations that can be applied to all collections are also very general. They are generic operations in the sense that they can be applied to various types of collections containing various types of objects. Suppose that coll is any object that implements the Collection interface. Here are some of the operations that are defined:

In addition, each specific Collection subclass may add new methods which are appropriate to the class. See the Java API for a given class to learn what methods it implements.

Enum Class

The Omlette class looks like a real pain with 21 different Topping types. Wouldn't it be nice if a Topping knew what its price were? It can. This section describes how.

An enum is a class, and can have constructors, data fields and methods. It does not look like a class because Java is following the C style of syntax. Suppose you would like an enum class for baseball's greatest natural sluggers, and you wanted each slugger to "know" how many home runs they hit. Here is how you write the class:

public enum Slugger {
   AARON(755),
   RUTH(714),
   MAYS(660),
   ROBINSION(586),
   KILLEBREW(573),
   JACKSON(563),
   SCHMIDT(548);

   private int homers;
   Slugger(int hr) { this.homers = hr; }
   public int homeruns() { return homers; }
}
Seven Slugger objects are created (the seven leading natural sluggers) using the new constructor Slugger. Besides the toString method which all Objects have, there is a homeruns method. For example
AARON.homeruns()
returns the integer 755.

Javadoc

javadoc is a tool for generating documentation in HTML. From now on you are responsible for providing documentation of all your public classes, fields, methods, and constructors, following the general guidelines put together for the Java community. I have put together a javadoc tutorial with further details.

Javadoc and Eclipse fit together nicely: you can use Eclipse to generate javadoc comments (that is, generate all the HTML pages to display the javadoc comments). Eclipse puts all these files in a directory called doc for you (this is standard Java practice.) You can also view javadoc comments while in Eclipse, by letting your mouse hover over a method. This will display the javadoc in a little window for you. For more information, see the Eclipse HelpDesk (and search under javadoc.)

Handin

Your lab is due Sunday at 11:59pm. You will submit your Diner project folder. You must be inside this directory to submit. See handin for details


Kenneth Harris