In this lab, you will be writing a complete class from scratch,
the IceCreamCone class. You will also write a driver to interact
with a user and to test all of the IceCreamCone methods.
Lab:
• Cone • Ice Cream o Flavor o Topping • Ice Cream Cone •
Driver
Part I: Cone
An ice cream cone has two main components, the flavor of ice cream
and the type of cone. Write a Cone class that takes an integer in
its constructor. Let a 1 represent a sugar cone, let a 2 represent
a waffle cone, and let a 3 represent a cup. Make sure to set an
invalid entry to a default cone (the choice of default is up to
you). Write a method to return the price of the cone. The sugar
cone is $0.59, the waffle cone is $0.79, and the cup is free. Write
a toString() method to return a String indicating the type of cone
that was selected.
Note: If you need a carriage return in a String, use "\r\n"
Part II: Ice Cream
Download the following files:
• Topping.java o public double price() //toppings are $0.15 ("no
topping" is free) • Toppings.java //this file has the following
public interface: o public static Toppings getToppings()
//singleton design pattern o public int numToppings() //the number
of different toppings o public String listToppings() //list (and
number) available toppings o public Topping getTopping(int index)
//1-based • Flavor.java • Flavors.java //this file has the
following public interface: o public static Flavors getFlavors()
//singleton design pattern o public int numFlavors() //the number
of different flavors o public String listFlavors() //list (and
number) available flavors o public Flavor getFlavor(int index)
//1-based
Ice cream has a number of scoops, a flavor, and a topping. Write an
IceCream constructor that accepts the number of scoops (up to 3), a
Flavor object, and a Topping object. Set default values (up to you)
if the input is invalid (make sure to protect against null). Write
a method to return the price (each scoop over 1 adds $0.75 to the
cost of the ice cream) and a method to return a String listing the
ice cream parameters selected.
Part III: Ice Cream Cone
Download the following file:
• Currency.class //this file has the following public interface: o
public static String formatCurrency(double val) //formats the
double as a String representing US currency
The IceCreamCone is composed of the Cone and the IceCream. Write a
constructor that accepts an IceCream and a Cone (protect against
null), a method to return the price (the base price is $1.99 plus
any costs associated with the components), and a method to return a
String display of the ice cream cone ordered by the user. Use
Currency.class to format the total price of the Ice Cream Cone.
Does this class use proper encapsulation techniques?
Note: Without using the Currency class, it is possible to get final
prices that are not quite correct, such as
3.5300000000000002.
Part IV: Ice Cream Driver
Download the following file:
• Keyboard.class //this file has the following public interface: o
public static Keyboard getKeyboard() //singleton design pattern o
public int readInt(String prompt) o public double readDouble(String
prompt) o public String readString(String prompt)
Write a driver to obtain the ice cream cone order from the user.
Allow multiple ice cream cones to be ordered, and keep a running
total of the price of the ice cream cones. Of course, wait (loop)
for the user to input valid entries. Thus, you are checking for
valid entries in the driver and in the constructor of your objects.
This is defensive programming, although it results in some code
duplication.
In addition to main, include the following methods in your
driver:
1. public static Flavor getFlavorChoice() //look at the methods
available in the Flavors class 2. public static Topping
getToppingChoice() //look at the methods available in the Toppings
class 3. public static int getScoopsChoice() 4. public static Cone
getConeChoice()
All of these methods take input from the user (using Keyb
Example output included as a jpg in Lab04 file
IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE THERE TO HELP YOU
ANSWER:
CODE:
import java.text.DecimalFormat;
// Class Currency definition
class Currency
{
// Declares an object of class DecimalFormat for 2 decimal places
private final static DecimalFormat fmt = new DecimalFormat("#,##0.00");
// Method to accept a number as parameter and returns it in 2 decimal format
public static String formatCurrency(double val)
{
String temp = "$" + fmt.format(val);
return temp;
}// End of method
}// End of class Currency
// Defines a enumeration for Cone
enum Cone
{
// Defines price for for each cone
sugar (0.59), waffle(0.79), cup(0.0);
// To store cone price
private double conePrice;
// Default constructor to set price
private Cone(double price)
{
conePrice = price;
}// End of default constructor
// Method to return price
public double price()
{
return conePrice;
}// End of method
}// End of enumeration Cone
// Defines a class Cones
class Cones
{
// Creates a Cones class object using default constructor
private static Cones cones = new Cones();
// Declares an array of object of Cone enumeration
private Cone[] all_cones;
// Default constructor to store Cone names
private Cones()
{
all_cones = Cone.values();
}// End of default constructor
// Method to return Cones class object
public static Cones getCones()
{
return cones;
}// End of method
// Method to return number of cones
public int numCones()
{
return all_cones.length;
}// End of method
// Method to return a string contains the cone names
public String listCons()
{
int index = 1;
// To store cone names
String cone_str = "";
// Loop through the enumeration, listing the cones
for (Cone top : all_cones)
{
// Displays cone names
cone_str += "Would you like a " + index + ": " + top.toString() + "\r\n";
// Increase the index counter by one
index++;
}// End of for loop
// Returns the cone names with index
return cone_str;
}// End of method
// Method to accept an index number and returns the Cone object
public Cone getCone(int index)
{
// Calls the method to store number of Cones
int num_cones = numCones();
// Checks if index is less than 1 or greater than total number of cones
if (index < 1 || index > num_cones)
{
// Returns 1 as default
index = 1;
}// End of if condition
// Otherwise returns the Cone object at index - 1 position
return all_cones[index - 1];
}// End of method
}// End of class Cones
// Defines enumeration for Topping
enum Topping
{
// Defines the amount for each topping
nuts(0.15), m_and_ms(0.45), hot_fudge(0.15), oreo_cookies(0.15), no_topping(0.00);
// To store topping price
private double toppingPrice;
// Default constructor to assign price
private Topping(double price)
{
toppingPrice = price;
}// End of default constructor
// Method to return topping price
public double price()
{
return toppingPrice;
}// End of method
}// End of enumeration Topping
// Class Toppings definition
class Toppings
{
// Creates an static object of the class Toppings using default constructor
private static Toppings toppings = new Toppings();
// Declares an array of Topping enumeration objects
private Topping[] all_toppings;
// Constructor to assign values of all toppings
private Toppings()
{
all_toppings = Topping.values();
}// End of method
// Static method to return Toppings object
public static Toppings getToppings()
{
return toppings;
}// End of method
// Method to return number of toppings
public int numToppings()
{
return all_toppings.length;
}// End of method
// Method to return a string having topping names
public String listToppings()
{
int index = 1;
// To store topping names
String topping_str = "";
// Loop through the enumeration, listing the flavors
for (Topping top : all_toppings)
{
topping_str += index + ". " + top.toString() + "\r\n";
// Increase the index by one
index++;
}// End of for loop
// Returns the topping names
return topping_str;
}// End of method
// Method to return Topping object based on the index given as parameter
public Topping getTopping(int index)
{
// Calls the method to store number of toppings
int num_toppings = numToppings();
// Checks if parameter index is less than 1 or greater than number of toppings
if (index < 1 || index > num_toppings)
{
// Sets the default topping index as 1
index = 1;
}// End of if condition
// Returns the topping index stored at index -1 position
return all_toppings[index - 1];
}// End of method
}// End of class Toppings
// Defines an enumeration Flavor
enum Flavor
{
chocolate, vanilla, coffee, mint_chocolate_chip, almond_fudge, pistachio_almond,
pralines_and_cream, cherries_jubilee, oreo_cookies_and_cream, peanut_butter_and_chocolate,
chocolate_chip, very_berry_strawberry, rocky_road, butter_pecan, chocolate_fudge, french_vanilla,
nutty_coconut, truffle_in_paradise;
}// End of enumeration Flavor
// Defines a class Flavors
class Flavors
{
// Declares an object of the class Flavors using default constructor
private static Flavors flavors = new Flavors();
// Declares an array of object of enumeration Flavor
private Flavor[] all_flavors;
// Default constructor to store enumeration Flavor values
private Flavors()
{
all_flavors = Flavor.values();
}// End of default constructor
// Static method to return Flavors class object
public static Flavors getFlavors()
{
return flavors;
}// End of method
// Method to return number of flavors
public int numFlavors()
{
return all_flavors.length;
}// End of method
// Method to return the flavor names with index
public String listFlavors()
{
int index = 1;
// To store flavor names
String flavor_str = "";
// Loop through the enumeration, listing the flavors
for (Flavor flavor : all_flavors)
{
flavor_str += index + ". " + flavor.toString() + "\r\n";
// Increase the index counter by one
index++;
}// End of for loop
// Returns the string contains flavor name with index
return flavor_str;
}// End of method
// Method to return Flavor object based on the index passed as parameter
public Flavor getFlavor(int index)
{
// Calls the method to store number of flavors
int num_flavors = numFlavors();
// Checks if parameter index is less than 1 or greater than number of flavors
if (index < 1 || index > num_flavors)
{
// Sets the default index as 1
index = 1;
}// End of if condition
// Returns the flavor index stored at index -1 position
return all_flavors[index - 1];
}// End of method
}// End of Flavors
// Defines a class Keyboard
class Keyboard
{
// Creates an object of Keyboard class object using default constructor
private static Keyboard kb = new Keyboard();
// Declares a scanner class object
private java.util.Scanner scan;
// Default constructor to create a Scanner class object
private Keyboard()
{
scan = new java.util.Scanner(System.in);
}// End of default constructor
// Static method to return Keyboard class object
public static Keyboard getKeyboard()
{
return kb;
}// End of method
// Method to read an integer from the keyboard and returns it.
// Uses the parameter provided prompt to request an integer from the user.
public int readInt(String prompt)
{
// Displays the message
System.out.print(prompt);
// To store inputed number
int num = 0;
// Try block begins
try
{
// Accepts a integer number
num = scan.nextInt();
// Calls the method to clear the buffer
readString("");
}// End of try block
// Catch block to handle wrong type data inputed
catch (java.util.InputMismatchException ime)
{
// Calls the method to clear the buffer
readString("");
// Resets the number to zero
num = 0;
}// End of catch block
// Catch block to handle no data
catch (java.util.NoSuchElementException nsee)
{
// Calls the method to clear the buffer
readString("");
// Resets the number to zero
num = 0;
}// End of catch block
// Returns the number
return num;
}// End of method
// Method to read a double from the keyboard and returns it.
// Uses the provided prompt to request a double from the user.
public double readDouble(String prompt)
{
// Displays the message
System.out.print(prompt);
// To store inputed number
double num = 0.0;
// Try block begins
try
{
// Accepts a double number
num = scan.nextDouble();
// Calls the method to clear the buffer
readString("");
}// End of try block
// Catch block to handle wrong type data inputed
catch (java.util.InputMismatchException ime)
{
// Calls the method to clear the buffer
readString("");
// Resets the number to zero
num = 0;
}// End of catch block
// Catch block to handle no data
catch (java.util.NoSuchElementException nsee)
{
// Calls the method to clear the buffer
readString("");
// Resets the number to zero
num = 0;
}// End of catch block
// Returns the number
return num;
}// End of method
// Method to read a line of text from the keyboard and returns it as a String.
// Uses the provided prompt to request a line of text from the user.
public String readString(String prompt)
{
// Displays the message
System.out.print(prompt);
// To store inputed string
String str = "";
// Try block begins
try
{
// Accepts a string including space
str = scan.nextLine();
}// End of try block
// Catch block to handle no data
catch (java.util.NoSuchElementException nsee)
{
// Calls the method to clear the buffer
readString("");
// Resets the string to null
str = "";
}// End of catch block
// Returns the string
return str;
}// End of method
}// End of Keyboard
// Defines a class IceCreamCone
public class IceCreamCone
{
// Static method to accept a Flavor index and return Flavor object
public static Flavor getFlavorChoice()
{
// To store Flavor index
int flavorNum;
// Creates a Keyboard object
Keyboard keyboard = Keyboard.getKeyboard();
// Creates a Flavors object
Flavors flavor = Flavors.getFlavors();
// Loops till valid Flavor index entered by the user
do
{
// Calls the method to display flavor names
System.out.println(flavor.listFlavors());
// Calls the method to display message and accept a number
flavorNum = keyboard.readInt("Enter your desired flavor: ");
// Checks if entered number is between 1 and number of flavors then come out of the loop
if(flavorNum >= 1 && flavorNum <= flavor.numFlavors())
break;
// Otherwise display error message
else
System.out.println("Please select between 1 - " + flavor.numFlavors() + " inclusive.");
}while(true);// End of do - while loop
// Calls the method to store the Flavor object based on the number inputed by the user
Flavor fla = flavor.getFlavor(flavorNum);
// Returns the Flavor object
return fla;
}// End of method
// Static method to accept a Topping index and return Topping object
public static Topping getToppingChoice()
{
// To store Topping index
int toppingNum;
// Creates a Keyboard object
Keyboard keyboard = Keyboard.getKeyboard();
// Creates a Toppings object
Toppings topping = Toppings.getToppings();
// Loops till valid Topping index entered by the user
do
{
// Calls the method to display topping names
System.out.println(topping.listToppings());
// Calls the method to display message and accept a number
toppingNum = keyboard.readInt("Enter your desired topping: ");
// Checks if entered number is between 1 and number of toppings then come out of the loop
if(toppingNum >= 1 && toppingNum <= topping.numToppings())
break;
// Otherwise display error message
else
System.out.println("Please select between 1 - " + topping.numToppings() + " inclusive.");
}while(true);// End of do - while loop
// Calls the method to store the Topping object based on the number inputed by the user
Topping top = topping.getTopping(toppingNum);
// Returns the Topping object
return top;
}// End of method
// Static method to accept a Cone index and return Cone object
public static Cone getConeChoice()
{
// To store Cone index
int coneNum;
// Creates a Keyboard object
Keyboard keyboard = Keyboard.getKeyboard();
// Creates a Cones object
Cones cone = Cones.getCones();
// Loops till valid Cone index entered by the user
do
{
// Calls the method to display message and accept a number
coneNum = keyboard.readInt("Would you like a 1: sugar cone, 2: waffle cone, or 3: cup? ");
// Checks if entered number is between 1 and 3 then come out of the loop
if(coneNum >= 1 && coneNum <= 3)
break;
// Otherwise display error message
else
System.out.println("Please select between 1, 2, or 3 for number of cones.");
}while(true);// End of do - while loop
// Calls the method to store the Cone object based on the number inputed by the user
Cone co = cone.getCone(coneNum);
// Returns the Cone object
return co;
}// End of method
// Static method to accept a Scoops choice and returns it
public static int getScoopsChoice()
{
// To store scoop index
int scoop;
// Creates a Keyboard object
Keyboard keyboard = Keyboard.getKeyboard();
// Loops till valid scoop index entered by the user
do
{
// Calls the method to display message and accept a number
scoop = keyboard.readInt("How many scoops (1, 2, or 3) would you like? ");
// Checks if entered number is between 1 and 3 then come out of the loop
if(scoop >= 1 && scoop <= 3)
break;
// Otherwise display error message
else
System.out.println("Please select 1, 2, or 3 for number of scoops.");
}while(true);// End of do - while loop
// Returns the scoop index
return scoop;
}// End of method
// Method to create an order list string and returns it
public static String order(Cone cone, Flavor flavor, Topping topping, int scoops, double price)
{
String orderList = "\n Your Order:";
// Concatenates cone name, flavor name, topping name, and number of scoops
orderList += "\n Cone: " + cone + "\n Flovor: " + flavor + "\n Topping: "
+ topping + "\n Number of scoops: " + scoops;
// Concatenates price with 2 decimal places by calling formatCurrency() method
orderList += "\n Price: " + Currency.formatCurrency(price);
// Returns the string format order
return orderList;
}// End of method
// Method to calculate price and returns it
public static double getOrderPrice(Cone cone, Flavor flavor, Topping topping, int scoops)
{
// Calculates price by calling price() method to get the price
double price = (topping.price() * scoops) + (cone.price() * scoops) + 1.99;
// Return the price
return price;
}// End of method
// main method definition
public static void main(String[] args)
{
// To store user choice
char ch;
// To store number of ice creams
int counter = 0;
// Topping, Cone, Flavor object declared
Topping topping;
Cone cone;
Flavor flavor;
// To store number of scoops
int scoops;
// Creates a Keyboard object
Keyboard keyboard = Keyboard.getKeyboard();
// To store each purchased price
double productPrice = 0;
// To store total purchased price
double totalPrice = 0;
// Loops till user choice is 'y'
do
{
// Calls the method to display message and accept user choice
ch = keyboard.readString("Would you like to order an ice cream cone? (y / n)").charAt(0);
// Checks if choice is 'y' or 'Y'
if(ch == 'Y' || ch == 'y')
{
// Calls the method to display Flavors and accept user choice
flavor = getFlavorChoice();
// Calls the method to display Toppings and accept user choice
topping = getToppingChoice();
// Calls the method to display scoops and accept user choice
scoops = getScoopsChoice();
// Calls the method to display Cones and accept user choice
cone = getConeChoice();
// Calculates each ice cream price
productPrice = getOrderPrice(cone, flavor, topping, scoops);
// Display the order list
System.out.println(order(cone, flavor, topping, scoops, productPrice));
// Calculates total purchase price
totalPrice += productPrice;
// Increase the purchase counter by one
counter++;
}// End of if condition
// Otherwise
else
{
// Displays the total amount to pay
System.out.println("Your total order for " + counter + " orders of ice cream is "
+ Currency.formatCurrency(totalPrice));
// Come out of the loop
break;
}// End of else
}while(true); // End of do while loop
}// End of main method
}// End of class IceCreamCone
Sample Output:
Would you like to order an ice cream cone? (y /
n)y
1. chocolate
2. vanilla
3. coffee
4. mint_chocolate_chip
5. almond_fudge
6. pistachio_almond
7. pralines_and_cream
8. cherries_jubilee
9. oreo_cookies_and_cream
10. peanut_butter_and_chocolate
11. chocolate_chip
12. very_berry_strawberry
13. rocky_road
14. butter_pecan
15. chocolate_fudge
16. french_vanilla
17. nutty_coconut
18. truffle_in_paradise
Enter your desired flavor: 22
Please select between 1 - 18 inclusive.
1. chocolate
2. vanilla
3. coffee
4. mint_chocolate_chip
5. almond_fudge
6. pistachio_almond
7. pralines_and_cream
8. cherries_jubilee
9. oreo_cookies_and_cream
10. peanut_butter_and_chocolate
11. chocolate_chip
12. very_berry_strawberry
13. rocky_road
14. butter_pecan
15. chocolate_fudge
16. french_vanilla
17. nutty_coconut
18. truffle_in_paradise
Enter your desired flavor: 14
1. nuts
2. m_and_ms
3. hot_fudge
4. oreo_cookies
5. no_topping
Enter your desired topping: 6
Please select between 1 - 5 inclusive.
1. nuts
2. m_and_ms
3. hot_fudge
4. oreo_cookies
5. no_topping
Enter your desired topping: 2
How many scoops (1, 2, or 3) would you like? 4
Please select 1, 2, or 3 for number of scoops.
How many scoops (1, 2, or 3) would you like? 2
Would you like a 1: sugar cone, 2: waffle cone, or 3: cup? 5
Please select between 1, 2, or 3 for number of cones.
Would you like a 1: sugar cone, 2: waffle cone, or 3: cup?
3
Your Order:
Cone: cup
Flovor: butter_pecan
Topping: m_and_ms
Number of scoops: 2
Price: $2.89
Would you like to order an ice cream cone? (y / n)y
1. chocolate
2. vanilla
3. coffee
4. mint_chocolate_chip
5. almond_fudge
6. pistachio_almond
7. pralines_and_cream
8. cherries_jubilee
9. oreo_cookies_and_cream
10. peanut_butter_and_chocolate
11. chocolate_chip
12. very_berry_strawberry
13. rocky_road
14. butter_pecan
15. chocolate_fudge
16. french_vanilla
17. nutty_coconut
18. truffle_in_paradise
Enter your desired flavor: 9
1. nuts
2. m_and_ms
3. hot_fudge
4. oreo_cookies
5. no_topping
Enter your desired topping: 5
How many scoops (1, 2, or 3) would you like? 1
Would you like a 1: sugar cone, 2: waffle cone, or 3: cup?
1
Your Order:
Cone: sugar
Flovor: oreo_cookies_and_cream
Topping: no_topping
Number of scoops: 1
Price: $2.58
Would you like to order an ice cream cone? (y / n)n
Your total order for 2 orders of ice cream is
$5.47
RATE THUMBSUP PLEASE
THANKS
In this lab, you will be writing a complete class from scratch, the IceCreamCone class. You...
JAVA PROGRAMMING***** Please provide screenshots of output and write a tester class to test. The starter code has been provided below, just needs to be altered. For this project you will implement the Memento Design Pattern. To continue on with the food theme, you will work with Ice Cream Cones. You will need to use AdvancedIceCreamCone.java. Create at least three Ice Cream Cones - one with chocolate ice cream, one with vanilla ice cream and one with strawberry ice cream....
Consider the class as partially defined here: //class Pizzaorder class Pizzaorder // static public members public static final int MAX TOPPINGS 20: // instance members private String toppings[]; private int numToppings; // constructor public PizzaOrder () { numToppings toppings 0; String[MAX TOPPINGS]; new // accessor tells # toppings on pizza, i.e., #toppings in array public int getNum Toppings ()return numToppings; // etc. We want to write an instance method, addTopping) that will take a String parameter, topping, and add it...
Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...
Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...
Complete the CashRegister class by implementing the methods and adding the correct attributes (characteristics) as discussed in class. Once complete, test the class using the CashRegisterTester class. Make sure you have 3 total items in the cash register. I have two classes, the first is CashRegisterTester below: public class CashRegisterTester { public static void main(String[] args) { //Initialize all variables //Construct a CashRegister object CashRegister register1 = new CashRegister(); //Invole a non-static method of the object //since it is non-static,...
Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...
ise 1. Create a new project named lab6 1. You will be implementing an IceCream class. For the class attributes, let's stick to flavor, number ofscoops, and price (in cents). Note that the price per scoop of any flavor is 99 cents. Therefore, allow the flavor and scoops to be set directly, but not the price! The price should be set automatically based on the number of scoops entered. Some other requirements i. A default constructor and a constructor with...
I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...
Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String input =""; System.out.println("Welcome to the easy square program"); while(true) { System.out.println("Enter the length of the side of a square or enter QUIT to quit"); try { input = keyboard.nextLine(); if(input.equalsIgnoreCase("quit")) break; int length = Integer.parseInt(input); Square s = new Square(); s.setLength(length); s.draw(); System.out.println("The area is "+s.getArea()); System.out.println("The perimeter is "+s.getPerimeter()); } catch(DimensionException e)...
We have written the following Class1 class: class Class1 implements SecretInterface { // instance variable private int x = 0; // constructor public Class1(int x) { this.x = x; } // instance methods public double myMethod1(double x, double y) { return x - y; } public void myMethod2(int x) { System.out.println(x); } public String myMethod3(String x) { return "hi " + x; } } We have also written the following Class2 class: class Class2 implements SecretInterface { // instance variable...