Lab 11: Interfaces
Objectives
- 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.ArrayList
collection class to hold class data
Resources
- Accept the GitHub Classroom assignment invitation in Canvas and then, in IntelliJ, create a new project from Version Control using the repository URL.
- Sample output for the
Kitchen
class is available insampleOutput.txt
.
Introduction
In this lab, you will explore using interfaces to make desserts.
By using interfaces, you can treat prepared ingredients the same as the basic (simple) ones. There are three preparation techniques provided in this lab:
- Baking
- Measuring
- Mixing
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.
Assignment Details
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
(Measure
, BakedIngredient
, and Mix
).
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
recipe:
====================================================
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
is expansionFactor
x volume
where volume
is the volume of the
baseIngredient
.)
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
numerator
/denominator
cups, where the denominator defaults to 1 if it
is not provided. The calories are given by
numerator
/denominator
x energy
/ volume
where the energy
and
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 in the sampleOutput
folder 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
Similarly, 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.
Acknowledgment
This laboratory assignment was developed by Dr. Josiah Yoder.