Question

JAVA PROGRAMMING***** Please provide screenshots of output and write a tester class to test. The ...

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. You can make your own choices for the other instance variables.
  • Save the state of these cones.
  • For the second part, ask the user which cone do they want: chocolate, vanilla, or strawberry or ....
  • Restore the ice cream cone to the state that the user selected.
  • Display the details of the ice cream cone that they chose.
/* This class is used to model the properties and behaviors of an ice cream cone.
   There are currently restriction on the construction of the cone: only one
   flavor of ice cream is allowed. */
import java.util.*;
public class AdvancedIceCreamCone {
        private int numberOfScoops;
        private String flavor;
        private String typeOfCone;
        private ArrayList<String> toppings = new ArrayList<String>();


//the default constructor creates a one scoop, vanilla ice cream cone using the regular type of cone and no toppings
        public AdvancedIceCreamCone() {
                numberOfScoops=1;
                flavor="vanilla";
                typeOfCone="regular";
        }
/*this constructor lets you create an ice cream code to your liking. It takes in three parameters:
  the number of scoops, the flavor of the ice cream and the type of cone */
        public AdvancedIceCreamCone(int ns,String flv,String cone) {
                numberOfScoops=ns;
                flavor=flv;
                typeOfCone=cone;
        }
//this method returns the number of scoops in the cone
        public int getNumberOfScoops () {
                return numberOfScoops;
        }
//this method returns the ice cream flavor
        public String getFlavor() {
                return flavor;
        }
//this method returns the type of cone
        public String getTypeOfCone() {
                return typeOfCone;
        }
//this method allows you to add one scoop of ice cream at a time
        public void addScoop() {
                numberOfScoops=numberOfScoops+1;
        }
//this method allows you to change the ice cream flavor
        public void setFlavor(String flv) {
                        flavor=flv;
        }
//this method allows you to change the type of cone
        public void setTypeOfCone(String cone) {
                typeOfCone=cone;
        }
//this method allows you to add  toppings
    public void addToppings(String top) {
                  toppings.add(top);
        }

//this method returns a String with a list of the toppings
        public ArrayList<String> getToppings () {
                return toppings;
        }

//this method overrides the inherited toString()
        public String toString() {
                return ("The number of scoops is " + numberOfScoops + ". The flavor is " +
                  flavor + ". And the type of cone is " + typeOfCone + " and the toppings are: " + getToppings());
          }

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

To implement the memento design, we use a helper class that stores and restores the states of the cone. The following program is implemented with two public classes (this will require two .java files).

1. AdvancedIceCreamCone.java

import java.util.*;

public class AdvancedIceCreamCone {

private int numberOfScoops;
private String flavor;
private String typeOfCone;
private ArrayList<String> toppings = new ArrayList<String>();

//the default constructor creates a one scoop, vanilla ice cream cone using the regular type of cone and no toppings
public AdvancedIceCreamCone() {
numberOfScoops = 1;
flavor = "vanilla";
typeOfCone = "regular";
}

/*this constructor lets you create an ice cream code to your liking. It takes in three parameters:
the number of scoops, the flavor of the ice cream and the type of cone */
public AdvancedIceCreamCone(int ns, String flv, String cone) {
numberOfScoops = ns;
flavor = flv;
typeOfCone = cone;
}
//this method returns the number of scoops in the cone

public int getNumberOfScoops() {
return numberOfScoops;
}
//this method returns the ice cream flavor

public String getFlavor() {
return flavor;
}
//this method returns the type of cone

public String getTypeOfCone() {
return typeOfCone;
}
//this method allows you to add one scoop of ice cream at a time

public void addScoop() {
numberOfScoops = numberOfScoops + 1;
}
//this method allows you to change the ice cream flavor

public void setFlavor(String flv) {
flavor = flv;
}
//this method allows you to change the type of cone

public void setTypeOfCone(String cone) {
typeOfCone = cone;
}
//this method allows you to add toppings

public void addToppings(String top) {
toppings.add(top);
}

//this method returns a String with a list of the toppings
public ArrayList<String> getToppings() {
return toppings;
}
  
public Memento saveToMemento()
{
System.out.println("Saving state to Memento.");
return new Memento(numberOfScoops, flavor, typeOfCone, toppings);
}
  
public void restoreFromMemento(Memento memento)
{
numberOfScoops = memento.getScoops();
flavor = memento.getFlavor();
typeOfCone = memento.getToc();
toppings = memento.getToppings();
System.out.println("State restored from Memento.");
}

//this method overrides the inherited toString()
public String toString() {
return ("The number of scoops is " + numberOfScoops + ". The flavor is "
+ flavor + ". And the type of cone is " + typeOfCone + " and the toppings are: " + getToppings());
}
  
public static class Memento {

private final int numberOfScoops;
private final String flavor;
private final String typeOfCone;
private ArrayList<String> toppings = new ArrayList<String>();

public Memento(int ns, String fl, String toc, ArrayList<String> tp) {
numberOfScoops = ns;
flavor = fl;
typeOfCone = toc;
toppings = new ArrayList<>(tp);
}

public int getScoops() {
return numberOfScoops;
}
  
public String getFlavor() {
return flavor;
}
  
public String getToc() {
return typeOfCone;
}
  
public ArrayList<String> getToppings() {
return toppings;
}
}

}

Note the new memento class added.

2. MementoDesign.java

import java.util.*;
public class MementoDesign {

public static void main(String[] args) {
  
List<AdvancedIceCreamCone.Memento> savedStates = new ArrayList<AdvancedIceCreamCone.Memento>();
  
AdvancedIceCreamCone chocolate = new AdvancedIceCreamCone(1,"chocolate","waffle");
AdvancedIceCreamCone vanilla = new AdvancedIceCreamCone();
AdvancedIceCreamCone strawberry = new AdvancedIceCreamCone(1,"strawberry","regular");
  
chocolate.addToppings("caramel");
savedStates.add(chocolate.saveToMemento());
  
vanilla.addToppings("cream");
savedStates.add(vanilla.saveToMemento());
  
strawberry.addToppings("caramel");
savedStates.add(strawberry.saveToMemento());
  
// Add another topping to chocolate
chocolate.addToppings("Cream");
System.out.println(chocolate.toString());
  
// Now restore to saved state
chocolate.restoreFromMemento(savedStates.get(0));
System.out.println(chocolate.toString());
  
// Add another topping to vanilla
vanilla.addToppings("caramel");
System.out.println(vanilla.toString());
  
// Now restore to saved state
vanilla.restoreFromMemento(savedStates.get(1));
System.out.println(vanilla.toString());
  
// Add another topping to strawberry
strawberry.addToppings("choco chips");
System.out.println(strawberry.toString());
  
// Now restore to saved state
strawberry.restoreFromMemento(savedStates.get(2));
System.out.println(strawberry.toString());
  
  
// Part 2
  
System.out.println("\nPart 2");
  
Scanner s = new Scanner(System.in);
System.out.println("\n\nEnter your ice cream cone flavor (chocolate, vanilla, or strawberry or ...): ");
String fl = s.next();
  
AdvancedIceCreamCone userCone = new AdvancedIceCreamCone(1,fl,"regular");
//Save this state
savedStates.add(userCone.saveToMemento());
  
// Now make changes
System.out.println("\nMaking changes to order");
userCone.addScoop();
userCone.addToppings("choco chips");
userCone.setTypeOfCone("waffle");
userCone.setFlavor("Mango");
  
// Print new state
System.out.println("After Modifications: " + userCone.toString());
  
// Restore to original state
userCone.restoreFromMemento(savedStates.get(3));
  
// Print again
System.out.println("Original Order:"+userCone.toString());
}
  
}

Output:

run: Saving state to Memento. Saving state to Memento. Saving state to Memento. The number of scoops is 1. The flavor is choc

There are a lot of comments to help you understand the code. All the best!

Add a comment
Know the answer?
Add Answer to:
JAVA PROGRAMMING***** Please provide screenshots of output and write a tester class to test. The ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this lab, you will be writing a complete class from scratch, the IceCreamCone class. You...

    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...

  • ise 1. Create a new project named lab6 1. You will be implementing an IceCream class. For the class attributes, let&#39...

    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...

  • Write in java and the class need to use give in the end copy it is...

    Write in java and the class need to use give in the end copy it is fine. Problem Use the Plant, Tree, Flower and Vegetable classes you have already created. Add an equals method to Plant, Tree and Flower only (see #2 below and the code at the end). The equals method in Plant will check the name. In Tree it will test name (use the parent equals), and the height. In Flower it will check name and color. In...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private...

    Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return...

  • Please help with Java programming! This code is to implement a Clock class. Use my beginning...

    Please help with Java programming! This code is to implement a Clock class. Use my beginning code below to test your implementation. public class Clock { // Declare your fields here /** * The constructor builds a Clock object and sets time to 00:00 * and the alarm to 00:00, also. * */ public Clock() { setHr(0); setMin(0); setAlarmHr(0); setAlarmMin(0); } /** * setHr() will validate and set the value of the hr field * for the clock. * *...

  • PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data...

    PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data fields: name (String), age(int), company(String) •Methods: •Public String getName(); •Public int getAge(); •Public String getCom(); •Public void setName(String n); •Public void setAge(int a); •Public void setCom(String c); •toString(): \\ can be used to output information on this name card 2, write a class DoublyNameCardList.java, which is a doubly linked list and each node of the list a name card. In the DoublyNameCardList.java: •Data field:...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the...

    NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the current displayed customer PLEASEEEEEEEEEE package bankexample; import java.util.UUID; public class Customer { private UUID id; private String email; private String password; private String firstName; private String lastName;    public Customer(String firstName, String lastName, String email, String password) { this.id = UUID.randomUUID(); this.firstName = firstName; this.lastName = lastName; this.email = email; this.password = password; } public String getFirstName() { return firstName; } public void setFirstName(String...

  • Java: Please help with my Tester. I'm having a hard time figuring out my error. Thank...

    Java: Please help with my Tester. I'm having a hard time figuring out my error. Thank you. What the tester requires: The AccountTester This class should consists of a main method that tests all the methods in each class, either directly by calling the method from the Tester or indirectly by having another method call a method. User and Bot information will be read from a file. A sample file is provided. Use this file format to aid in grading....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT