Lab 2: Interfaces
- Translate UML class diagrams into source code
- Construct software which uses an interface
- Employ aggregation and composition in a software system
- Use the
java.util.ArrayListcollection class to hold class data
In this lab, you will explore using interfaces to make deserts.
By using interfaces, you can treat prepared ingredients the same as the basic (simple) ones. There are three preparation techniques provided in this lab:
Every ingredient, simple or prepared, provides basic information about itself:
- The ingredient's name
- The energy content in Calories (kcal)
- The volume of the ingredient (cups)
- A recipe for preparing the ingredient
Preparation techniques use existing ingredients to create new ones.
A class diagram for the program is shown in the figure below:
Implement all of the classes shown in the diagram above based on the
discussion that follows. The arrows with black diamonds simply indicate
that one class has a reference to another. These references are also shown
as member variables in the class diagrams for the three prepared ingredients
Simple ingredients print a simple recipe listing the name of the ingredients
in the title (between two lines of equal signs) and the basic information
about the ingredient. For example, the
SimpleIngredient milk prints this
==================================================== Milk ==================================================== Cups: 1 Cups Energy: 103 Calories
Energy is rounded to the nearest Calorie, and volume is rounded to the hundredth of a Cup. If fractional digits are zero, they are ommited as shown here.
A baked ingredient keeps the same number of calories, but becomes dry and
increases its volume by a user-provided expansion factor. (The new volume
volume is the volume of the
A measured ingredient maintains the energy density of the original ingredient,
but measures a different quantity of it. The new quantity becomes part
of the ingredient's name. The new volume is specified as
denominator cups, where the denominator defaults to 1 if it
is not provided. The calories are given by
volume where the
volume describe the original ingredient. When printing its ingredient
list, the measured ingredient lists the quantity both as a fraction and then,
in parenthesis, as a decimal number to the nearest hundredth. See the cake
output at the end for examples.
A mixed ingredient is a combination of several existing ingredients. The
volume and energy of the mix are simply the sum of the volume and energy
of each of the ingredients in the mix, respectively. (This is a bit of an
approximation, especially for volume!) A mix is wet if any of the ingredients
is wet. A mixed ingredient's recipe lists the dry and wet ingredients
separately, as shown in the
ice cream example a little later on.
Each prepared ingredient (baked, measured, and mixed) prints its own recipe
and the recipe for each ingredient it is prepared from. For example, the
dry milk option of the Kitchen program prints the recipe for baked milk,
which in turn prints the recipe for the milk it is baked from:
==================================================== Baked Milk ==================================================== Ingredient to be baked: Milk Cups: 0.2 Cups Energy: 103 Calories ==================================================== Milk ==================================================== Cups: 1 Cups Energy: 103 Calories
ice cream prints first the main recipe, and then the recipe
for each ingredient it is made from:
==================================================== Ice Cream ==================================================== Dry Ingredients: Sugar Wet Ingredients: Cream Milk Cups: 2.12 Cups Energy: 980 Calories ==================================================== Cream ==================================================== Cups: 0.12 Cups Energy: 104 Calories ==================================================== Milk ==================================================== Cups: 1 Cups Energy: 103 Calories ==================================================== Sugar ==================================================== Cups: 1 Cups Energy: 773 Calories
A prepared ingredient may be used to prepare further ingredients. For example, in the cookie recipe, each ingredient in the mix is itself a measured ingredient:
The final recipe is for a cake. The frosting and batter are prepared separately, and then combined:
You may add new options to the example program, but do not edit the options
provided in the example. Let the single
printRecipe() call print everything.
Appendix: cookie and cake examples.
Here is the output for the two remaining options of Kitchen.java
All of the classes must be in a package with your username. (You will need
to modify the package declaration for the
FIXME to your MSOE
This laboratory assignment was developed by Dr. Josiah Yoder.