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
- Fixing problems in your
menupackage from last week. - Quality of your code.
- Method bodies must be short (anything more than 10 lines is too long.)
- Proper use of generic Java Collection classes (described below.)
- Thorough testing of your classes.
- 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.
- Extra Credit: If you did the Extra
Credit for lab3 last week, you should continue to write this
week's lab using
Exceptionswhere 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:
- Omlette base price (a plain omlette): $2.00
- Choice of any combination of toppings (charge is per topping in
addition to the base price)
- Cheddar, American, Swiss and Feta Cheeses (+ $0.75)
- Artichoke Hearts, Avocado, Green Chilis, Mushrooms, Onions, Olives, Peppers, Spinach, Tomato, Zucchini (+ $0.50)
- Ham, Bacon, Sausage (+ $1.00)
- Lox, Shrimp (+1.50)
- Chili Sauce, Rarebit Sauce (+ $0.60)
- Omlette Plate: Omlette, Meat, Side items and choice of White or Wheat toast. Cost: sum of costs of Omlette, Meat and Side items.
Omlette and
OmlettePlate in the menu package.
The Omlette class must have
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 bigswitch.)public Omlette(Topping[] ts): a constructor which accepts anArrayof toppings. Here are some things that you must consider in constructing your Omlette object- If
tsisnullyou should create a plain Omlette (one with no toppings.) - A topping only counts once on an omlette,
although
tscould include the same topping several times. tscould containnullreferences, but this should never lead to an exception. You should ignorenullreferences ontsand not try to add them to the Omlette object.- See the HashSet generic Java class for keeping track of the toppings.
- If
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
public Order(): A constructor which creates a new Order object whose list ofItems is empty.public double price(): compute the price of the order, by summing the price of theItems. The price is in dollars. You should convert from pennies to dollars only after you compute the price in pennies.public String toString(): convert anOrderto aString. Descriptions of eachItemshould be placed on separate lines.\nis the newline character and adds a newline when the string is printed.public boolean add(Item i): addsItem ito the list stored by the Order object. Ifiisnullthen you should returnfalsewhich indicatesiwas not added to the Order. Returntrueonly ifiwas added to the order.
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)
- LinkedList<E>: implements a list of Objects.
- 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:
- coll.size() -- returns an int that gives the number of objects in the collection.
- coll.isEmpty() -- returns a boolean value which is true if the size of the collection is 0.
- coll.clear() -- removes all objects from the collection.
- coll.contains(object) -- returns a boolean value that is true if object is in the collection.
- coll.add(object) -- adds object to the collection. The parameter can be any Object. This method returns a boolean value which tells you whether the operation actually modified the collection. For example, adding an object to a HashSet has no effect if that object was already in the set.
- coll.remove(object) -- removes object from the collection, if it occurs in the collection, and returns a boolean value that tells you whether the object was found.
- coll.containsAll(coll2) -- returns a boolean value that is true if every object in coll2 is also in the coll. The parameter can be any Collection.
- coll.addAll(coll2) -- adds all the objects in the collection coll2 to coll.
- coll.removeAll(coll2) -- removes every object from coll that also occurs in the collection coll2.
- coll.retainAll(coll2) -- removes every object from coll that does not occur in the collection coll2. It "retains" only the objects that do occur in coll2.
- coll.toArray() -- returns an array of type Object[] that contains all the items in the collection. The return value can be type-cast to another array type, if appropriate. For example, if you know that all the items in coll are of type String, then (String[])coll.toArray() gives you an array of Strings containing all the strings in the collection.
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 a> for details