Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in java Laboratory 3: Ice Cream! Download Lab03.zip from ilearn and extract it into your working lab 3 directory In this lab, you will be

in java

Laboratory 3: Ice Cream! Download Lab03.zip from ilearn and extract it into your working lab 3 directory 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 " " 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 files. To compile: javac *.java To run: java IceCreamDriver You can also run your driver with my input file via: java IceCreamDriver < ice_cream_test.txt (or by running make.bat)

//topping.java

public enum Topping { nuts(0.15), m_and_ms(0.15), hot_fudge(0.15), oreo_cookies(0.15), no_topping(0.00);

private double toppingPrice;

private Topping(double price) { toppingPrice = price; }

public double price() { return toppingPrice; } }

//toppings.java

public String listToppings() { int index = 1;

//loop through the enumeration, listing the flavors String topping_str = "";

for (Topping top : all_toppings) { topping_str += index + ". " + top.toString() + " "; index++; }

return topping_str; }

public Topping getTopping(int index) //1-based { int num_toppings = numToppings(); if (index < 1 || index > num_toppings) { index = 1; }

return all_toppings[index - 1]; } } //flavor.java

public 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; }

//flavors.java

public class Flavors { private static Flavors flavors = new Flavors(); private Flavor[] all_flavors;

private Flavors() { all_flavors = Flavor.values(); }

public static Flavors getFlavors() { return flavors; }

public int numFlavors() { return all_flavors.length; }

public String listFlavors() { int index = 1;

//loop through the enumeration, listing the flavors String flavor_str = "";

for (Flavor flavor : all_flavors) { flavor_str += index + ". " + flavor.toString() + " "; index++; }

return flavor_str; }

//currency.class

7 1 fmt Ljava/text/DecimalFormat; ()V Code LineNumberTable formatCurrency (D)Ljava/lang/String; SourceFile Currency.java ! BootstrapMethods " # $ % java/text/DecimalFormat #,##0.00 & Currency java/lang/Object format ' ( $ makeConcatWithConstants &(Ljava/lang/String;)Ljava/lang/String; (Ljava/lang/String;)V ) $ - $java/lang/invoke/StringConcatFactory / Lookup InnerClasses (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite; 0 %java/lang/invoke/MethodHandles$Lookup java/lang/invoke/MethodHandles ! * + & M, % Y , * . +

//keyboard.class

7 K ( ) * + , - . * / 0 1 2 3 4 5 6 7 8 9 ( : kb LKeyboard; scan Ljava/util/Scanner; ()V Code LineNumberTable getKeyboard ()LKeyboard; readInt (Ljava/lang/String;)I StackMapTable ; readDouble (Ljava/lang/String;)D readString &(Ljava/lang/String;)Ljava/lang/String; SourceFile Keyboard.java java/util/Scanner < = > ? @ A B C D E F # $ java/util/InputMismatchException java/util/NoSuchElementException G H I J Keyboard java/lang/Object java/lang/String java/lang/System in Ljava/io/InputStream; (Ljava/io/InputStream;)V out Ljava/io/PrintStream; java/io/PrintStream print (Ljava/lang/String;)V nextInt ()I nextDouble ()D nextLine ()Ljava/lang/String; ! 3 * * Y 4 + =* =* W N* W= N* W= ( 6 ! " - $ & # ' % - ( ) ) + 0 , 2 . L ! " 6 + I* I* W :* WI :* WI( ) 6 7 8 < = H ? A $ B & H ) D + F 2 G 4 J M # $ z " + M* M N* W M, " S T X ^ Z \ ] ` % # Y & '

public Flavor getFlavor(int index) { int num_flavors = numFlavors(); if (index < 1 || index > num_flavors) { index = 1; }

return all_flavors[index - 1]; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Robb,Carlos Coronel

5th Edition

061906269X, 9780619062699

More Books

Students also viewed these Databases questions