Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a complete class from scratch , the IceCreamCone class. You will also writ

ID: 3751538 • Letter: W

Question

Write 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. 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. See the example below for how to format the toString() method. Is this class immutable?

Note: If you need a carriage return in a String, use " "

Part II: Ice Cream

Download the following files: PLEASE SEE DROPBOX LINK

• Topping.java – PLEASE CLICK ON LINK : https://www.dropbox.com/s/jt9jq6sesonji2d/Topping.java?dl=0

o public double price() //toppings are $0.15 ("no topping" is free)

• Toppings.java – PLEASE CLICK ON LINK : https://www.dropbox.com/s/dhuyh7y402xtgic/Toppings.java?dl=0

//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 PLEASE CLICK ON LINK : https://www.dropbox.com/s/1ar1ecq7tsodil7/Flavor.java?dl=0

• Flavors.java PLEASE CLICK ON LINK : https://www.dropbox.com/s/9kge30rn94d0qwl/Flavors.java?dl=0

//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 if the input is invalid (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: PLEASE CLICK ON LINK : https://www.dropbox.com/s/jzjmll7spmr1n5l/Currency.java?dl=0

• 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 the 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. Why does this happen?

Part IV: Ice Cream Driver

Download the following file: PLEASE CLICK ON LINK : https://www.dropbox.com/s/lkkicxi36wmn8m1/Keyboard.java?dl=0

• 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()

Example output as a jpg in the LINK. : https://www.dropbox.com/s/wnn776yzaaq1vmz/lab04.jpg?dl=0

Explanation / Answer

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() + " ";

// 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() + " ";

// 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() + " ";

// 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 = " Your Order:";

// Concatenates cone name, flavor name, topping name, and number of scoops

orderList += " Cone: " + cone + " Flovor: " + flavor + " Topping: "

+ topping + " Number of scoops: " + scoops;

// Concatenates price with 2 decimal places by calling formatCurrency() method

orderList += " 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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote