Question: package stack; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import javax.swing. * ; / * * CS 1 1 2 Stack Lab * *
package stack;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import javax.swing.;
CS Stack Lab
Implement push and pop using an array based
stack of strings to implement the ice cream display
@author Colin Sullivan
public class IceCreamStack
Array representing a stack. Index is the bottom of the stack.
private String iceCreamScoops;
First open spot in the stack array
private int next;
Default constructor for an empty stack with a capacity of
public IceCreamStack
iceCreamScoops new String;
next ;
Resize method which will double the capacity of the stack
and Copy all elements
Create a new String array, with its size double the current
iceCreamScoops array size
For each index in iceCreamScoops, copy the item at that index
to the same index in the new doubled array
Set the class attribute thisiceCreamScoops equal to your
new doubled array
public void resize
WRITE YOUR CODE HERE
Given a String flavor, push it onto the Stack
The class variable "next" is equivalent to the first
empty spot in the stack array.
Therefore, next is the top of the stack
If the stack is full next array length call resize to double it
Place the string at 'next' index in the array, and increment next by one.
public void pushString flavor
WRITE YOUR CODE HERE
Pop and return the top item of the stack
If the stack is empty next return null
E
public String pop
if next
return null;
String flavor iceCreamScoopsnext;
return flavor;
Since the array is zeroindexed, next numScoops
@returns number of scoops
public int getScoops
return next;
Everything past here runs the driver, no need to modify anything
private class IceCreamPanel extends JPanel
private IceCreamPanel
super;
@Override
public void paintComponentGraphics g
super.paintComponentg;
int coneX iceCreamDisplay.getWidth;
int coneY ;
for int scoop next ; scoop ; scoop
Color c iceCreamScoopsscoopequalschocolatenew Color
: iceCreamScoopsscoopequalsstrawberrynew Color
: new Color;
gsetColorc;
gfillOvalconeX coneY ;
coneY ;
gsetColornew Color;
int coneXPoints coneX, coneX coneX ;
int coneYPoints coneY, coneY coneY ;
gfillPolygonconeXPoints coneYPoints, coneXPoints.length;
this.setPreferredSizenew Dimensionnext ;
private IceCreamPanel iceCreamDisplay;
public String getScoopArray
return this.iceCreamScoops;
public void Driver
JFrame display new JFrameIce Cream Stack Lab";
display.setDefaultCloseOperationJFrameEXITONCLOSE;
JPanel window new JPanel;
window.setLayoutnew BoxLayoutwindow BoxLayout.PAGEAXIS;
iceCreamDisplay new IceCreamPanel;
JScrollPane scroll new JScrollPaneiceCreamDisplay ScrollPaneConstants.VERTICALSCROLLBARASNEEDED,
ScrollPaneConstants.HORIZONTALSCROLLBARNEVER;
JPanel controls new JPanel;
controls.setLayoutnew BoxLayoutcontrols BoxLayout.PAGEAXIS;
JPanel button new JPanel;
JPanel line new JPanel;
JLabel numberScoops new JLabelNumber of Scoops: this.getScoops;
JButton addVanilla new JButtonAdd Vanilla";
addVanilla.addActionListenerActionEvent e
this.pushvanilla;
int scoop this.getScoops;
SwingUtilities.invokeLaternew Runnable
@Override
public void run
numberScoops.setTextNumber of Scoops: scoop;
iceCreamDisplay.revalidate;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
