Follow by instruction
Assignment #6
CS 180: Programming Fundamentals – Fall 2017
1
Programming Assignment 6
Getting Started
This assignment will focus on basic class definition. It uses concepts introduced in Sections 4.1
and 4.2 as well as previous assignments and lectures.
Programming Assignment (20 points)
Your class should be called Sundae with an additional test program called SundaeTest.
This is the beginning of an application for managing customer orders at a sundae bar. In this
assignment, you will create a class representing a sundae.
Define the class Sundae as follows:
Private instance variables: (1 point)
scoops - an integer for storing the number of scoops of ice cream in the sundae. There
can be 1, 2, or 3 scoops of ice cream. The value should default to 1 scoop.
flavor - a char representing the ice cream flavor: ‘V’ for vanilla, ‘C’ for chocolate, and ‘S’
for strawberry. The value should default to ‘V’.
toppings - an array of strings (String[]) specifying the toppings on the sundae. The
default value should be an empty string array (not null!)
NOTE: There should be no other instance variables in the class
Public instance methods
setScoops() - takes a parameter of type int which is the number of scoops in the sundae
and has a boolean return value. The method should check that the value is 1, 2, or 3. If
the value is valid, it should assign the value to the scoops instance variable and return
true. Otherwise, the method should return false. (2 points)
setFlavor() - takes a parameter of type char representing the flavor of the ice cream and
has a boolean return value. The method should check that the parameter is equal to ‘V’,
‘C’, or ‘S’ and if so assign the value to the flavor instance variable and return true.
Otherwise, the method should return false. (2 points)
Assignment #6
CS 180: Programming Fundamentals – Fall 2017
2
setToppings() - takes a parameter of type String[] (string array) representing the sundae
toppings. The method should assign the parameter value to the toppings instance
variable. The return value of setToppings() should be void. (1 point)
getScoops() - takes no parameters and has a return value of int. Returns the number of
scoops. (1 point)
getFlavor() - takes no parameters and has a return value of char. Returns the char
representing the flavor. (1 point)
getToppings() - takes no parameters and has a string array return value. Returns the
toppings array. (1 point)
numToppings() - takes no parameters and has an int return value. Returns the length of
the topping array. Be careful to check for null. For null toppings, the method should
return 0. (1 point)
calcPrice() - takes no parameters and has a double return value. This method should
calculate and return the price of the sundae. The price is the base price plus an
additional price for each topping. The base price is $3 for one scoop, $4 for two scoops,
and $5 for three scoops. Each topping costs $0.10 for a one-scoop sundae, $0.20 for a
two-scoop sundae, and $0.30 for a three-scoop sundae. (3 points)
Define the class SundaeTest as follows in the same folder as your Sundae class: (5 points)
The SundaeTest program should:
Create an instance of class Sundae
Prompt for and read the number of scoops
Prompt for and read a character representing the flavor
Prompt for and read the number of toppings.
a. Create an array to store the specified number of toppings
b. Write a loop to read the toppings and store them in the array
5. Call the appropriate Sundae methods to set the values entered by the user for scoops,
flavor, and toppings.
6. Print a message that includes the price information by using the appropriate methods of
the Sundae class. The price should be formatted to two decimals.
1.
2.
3.
4.
Assignment #6
CS 180: Programming Fundamentals – Fall 2017
Sample Interaction
Please enter the size of sundae (1, 2 or 3 scoops): 3
Please enter the flavor (V, C, or S): C
How many toppings would you like to add to the sundae? 2
Please enter topping number 1: whipped cream
Please enter topping number 2: cherry
************************************************
Created a sundae
3 scoops
flavor: C
2 toppings:
1. whipped cream
2. cherry
This sundae costs $5.60
************************************************
Another interaction that shows invalid values for scoops and flavor. Also shows how zero
toppings should be reported:
Please enter the size of sundae (1, 2 or 3 scoops): 4
Invalid number of scoops. Setting to 1 scoop.
Please enter the flavor (V, C, or S): X
Invalid flavor. Setting to vanilla.
How many toppings would you like to add to the sundae? 0
************************************************
Created a sundae
1 scoops
flavor: V
0 toppings:
This sundae costs $3.00
************************************************
Graphical User Interface
You don’t need to write any additional code for this part. I am supplying a graphical interface that uses
Sundae class to demonstrate how your class can be used with another class. The supplied interface
includes two files: SundaeGUI.java and sundae.jpg (an image file used by SundaeGUI). These two files
should be placed in the project folder, where you keep the Sundae and SundaeTest classes. If your
project has src and bin folders, place SundaeGUI.java in src and sundae.jpg in the project folder.
When you are finished writing and testing your Sundae class, you can run the SundaeGUI, and it should
3
Assignment #6
CS 180: Programming Fundamentals – Fall 2017
work by allowing you to select parameters and displaying the order information, upon clicking the
“Build” button. The following image illustrates the SundaeGUI application running.
If your Sundae class is not defined according to the provided above specification, The SundaeGUI will
generate an exception when you click on the “Build” button. This may happen if you are missing a
method, or have defined it with a different set of parameters, a different type of return value or
misspelled its name.
You will need to fix your code before you submit it to get full credit.
4
Assignment #6
CS 180: Programming Fundamentals – Fall 2017
Submit Sundae.java and SundaeTest.java.
Note that using good programming style counts for 2 points in this assignment. In addition to the rules
of good style defined before, each method should have an introductory comment, describing its
purpose, parameters and return value.
5