Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a class called IceCreamCone and have it implement IceCreamConeADT . Youll need to import java.util.Stack in order to utilize Javas Stack class. Now create

Create a class called IceCreamCone and have it implement IceCreamConeADT. Youll need to import java.util.Stack in order to utilize Javas Stack class.

Now create two fields: flavors and numScoops, flavors will be a Stack containing String objects (Stack), which will represent the flavors of ice cream. The second field numScoops will represent the number of ice cream scoops on the cone.

Next create a default constructor and initialize your two fields. Initialize numScoops field to 0.

To make your class compile, go to the line where your class declaration appears and click the compiler error icon (the light bulb with the red X) and select Add Unimplemented Methods. This will implement your methods as stubs so that the compiler will accept them and you will be able to write tests.

Writing Tests

Now create a test class for IceCreamCone called IceCreamConeTest.

Look at the IceCreamConeADT JavaDoc comments to determine how each method should behave, then write your test methods accordingly. Your tests shouldnt pass until you implement the methods in IceCreamCone.

Notice that in this implementation (in IceCreamCone class) the field variable numScoops needs to be updated when the number of scoops changes.

Implementing methods

Once youve finished your tests, implement the methods in IceCreamCone. Remember you should also always implement the toString() and equals() methods.

Think about how to use the Stack class to implement your methods.

As a quick reference, heres a list of the available methods for you from the Stack class:

Boolean empty() //Check if the stack is empty E peek() //Returns the item at the top of the stack without removing it E pop() //Returns the item at the top of the stack and removes it E push(E item) //Adds an item to the top of the stack int search(Object o) //returns the position of the location of the object in the stack boolean contains(Object o) //Returns true if the stack contains the specified element. Method inherit from class java.util.Vector. String toString() //Returns a string in the format [Obj1, Obj2, Obj3, ..., ObjN]. Method inherit from class java.util.Vector. boolean equals(Object o) //Returns true if both stacks contain the same elements in the same order.Method inherit from class java.util.Vector.

Look at the methods in the Stack API (Links to an external site.)Links to an external site. to see all of Stack's methods in more detail.

When you use assertEquals in your testing, it will use the equals() method for comparison. Unless the equals() method is overridden, the equals method from Object will get called. As that method only compares for identity, we need to override the equals() method. Based on the usual boiler plate and adapting it using the Stack equals, write your IceCreamCone equals method like this:

 @Override public boolean equals(Object other) { if (other == this) { return true; } if ((other == null) || (other.getClass() != IceCreamCone.class)) { return false; } return flavors.equals(((IceCreamCone) other).flavors); } 

The methods need to be implemented are below:

public class IceCreamCone implements IceCreamConeADT{

private Stack flavors; private int numScoops; public IceCreamCone() { flavors = null; numScoops = 0; }

@Override public String eatScoop() { return null; }

@Override public void addScoop(String flavor) { //iceCreamStack.push(flavor); }

@Override public int numScoops() { return 0; }

@Override public boolean contains(String flavor) { return false; }

@Override public boolean emptyCone() { return false; }

@Override public String currentScoop() { return null; } @Override public boolean equals(Object other) { if (other == this) { return true; } if ((other == null) || (other.getClass() != IceCreamCone.class)) { return false; } return flavors.equals(((IceCreamCone) other).flavors); } }

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

More Books

Students also viewed these Databases questions