Você está na página 1de 3

CO116 Chapter 4 Assignment A

Tip Top Bakery wants a program to build a sandwich and provide them the
number of calories for a combination of bread and filling.

Building the Framework


Create a public class named SandwichBuilder with a main method. Then, in the
same file, create the following, non-public classes: Bread, Filling, Sandwich.
Save the file as SandwichBuilder.java.

The Bread Class


Design the class according to the following specifications:

Create a field to hold the bread type, e.g. rye.


Create a corresponding getter method.
Create a field to hold the number of calories per slice.
Create a corresponding getter method.
Create a constructor that takes parameters for each field and assigns them.

The Filling Class


Design the class according to the following specifications:

Create a field to hold a filling type, such as egg salad.


Create a corresponding getter method.
Create another field to hold calories per serving.
Create a corresponding getter method.
Create a constructor that takes parameters for each field and assigns them.

The Sandwich Class


Design the class according to the following specifications:

Create a field to hold the name of the sandwich and its price. Create get and
set methods for both fields.

Create a field to hold a Bread object. When creating the field, declare and
then initialize it by creating a new Bread object. Create get and set methods.

Create a field to hold a Filling object. When creating the field, declare and
then initialize it by creating a new Filling object. Create get and set
methods.

Create a constructor that accepts the name, price, bread attributes, and
filling attributes of the Sandwich object and sets them all in the object.

Create a method that returns the total number of calories of the sandwich.
Assume that a Sandwich has two slices of Bread and one serving of Filling.

The SandwichBuilder Class


This class should contain the main method from which the program runs. In that
method, implement the following functionality:

Ask the user for all the attributes of a Sandwich needed for the constructor
(see Sample Output section below)

Create a Sandwich object, passing all of the data obtained from the user into
the object via its constructor.

Write the code necessary to display the attributes of the Sandwich,


including the total number of calories (see Sample Output section below).

Sample Output
----jGRASP exec: java SandwichBuilder
What is the name of the sandwich? HappyBrat
What is the price? 4.99
What type of bread? white
How many calories per slice? 300
What kind of filling: jelly
How many calories per serving? 250
The HappyBrat comes on two slices of white
bread with jelly for $4.99 (660 calories)
----jGRASP: operation complete.

Submitting Your Work


After your program runs correctly and displays the information the way you like,
copy the output to bottom of the file like you do in your assignments. Make sure
you have a comment at the top that includes your name, date, course, section, and
the name of the program. When youve done all of this, you are ready to submit
your assignment. Follow the instructions within Blackboard to submit your work.

Você também pode gostar