Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* StackArrayDrawing * Anderson, Franceschi */ import java.awt.Graphics; import javax.swing.JFrame; import java.awt.Color; public class StackArrayDrawing { public static final int XSTART = 200; public static

image text in transcribed

/* StackArrayDrawing * Anderson, Franceschi */

import java.awt.Graphics; import javax.swing.JFrame; import java.awt.Color;

public class StackArrayDrawing { public static final int XSTART = 200; public static final int YSTART = 300; public static final int WIDTH = 100; public static final int HEIGHT = 25;

private StackArray sa;

private boolean pushSuccess; private boolean popSuccess; private int pushedValue; private int poppedValue;

private boolean operation; // true for push, false for pop private String description = ""; private boolean started;

public StackArrayDrawing( ) { sa = new StackArray( ); }

public void setPushSuccess( boolean newPushSuccess ) { started = true; pushSuccess = newPushSuccess; }

public void setPopSuccess( boolean newPopSuccess ) { popSuccess = newPopSuccess; }

public void setPushedValue( int newPushedValue ) { pushedValue = newPushedValue; }

public void setPoppedValue( int newPoppedValue ) { poppedValue = newPoppedValue; }

public void setOperation( boolean newOperation ) { operation = newOperation; }

public void setStarted( boolean newStarted ) { started = newStarted; }

public boolean push( int value ) { return ( sa.push( value ) ); }

public int pop( ) throws DataStructureException { return( sa.pop( ) ); }

public void draw( Graphics g ) { // draw the empty stack and indices g.setColor( Color.BLUE ); drawEmptyStack( g );

// draw the stack contents g.setColor( Color.RED ); drawStackContents( g );

// draw top g.setColor( Color.BLACK ); drawTop( g );

// determine operation if ( operation ) // push { if ( pushSuccess ) { description = "successfully pushed " + pushedValue; } else { description = "failed to push " + pushedValue; } } else // pop { if ( popSuccess ) { description = "successfully popped " + poppedValue; } else { description = "failed to pop"; } } if ( !started ) description = ""; drawDescription( g ); }

public void drawEmptyStack( Graphics g ) { for ( int i = 0; i

public void drawStackContents( Graphics g ) { int i = 0; g.setColor( Color.BLACK ); for ( i = 0; i

public void drawTop( Graphics g ) { g.drawString( "top", XSTART - WIDTH / 2, (int) ( YSTART - ( sa.getTop( ) - 0.7 ) * HEIGHT ) ); }

public void drawDescription( Graphics g ) { g.drawString( description, XSTART + WIDTH + 20, YSTART / 2 ); } }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/* StackArray * Anderson, Franceschi */

import java.awt.Graphics; import javax.swing.JFrame; import java.awt.Color;

public class StackArray { public static final int CAPACITY = 10;

private int[] stack; private int top;

public StackArray() { stack = new int[CAPACITY]; top = -1; }

public int get(int index) { return stack[index]; }

public int getTop() { return top; }

/** * push method * * @param value value to be pushed onto the stack * @return true if successful, false if unsuccessful */ public boolean push(int value) { // ***** 1. Student code starts here ***** // stack is an int array instance variable representing // the array that stores our stack

// top is an instance variable representing // the index of the top of the stack // CAPACITY is a constant instance variable representing // the size of the array stack // The push method adds the argument value // to the top of the stack, if it is possible // code the push method here // Part 1 student code starts here:

return true; // replace this dummy return statement

// Part 1 student code ends here. }

/** * pop method * * @return the value of the top element of the stack, if successful */ public int pop() throws DataStructureException { // ***** 2. Student code restarts here ***** // stack is an int array instance variable representing // the array that stores our stack

// top is an instance variable representing // the index of the top of the stack // CAPACITY is a constant instance variable representing // the size of the array stack // The pop method deletes the element // at the top of the stack, if it is possible // Code the pop method here // Part 2 student code starts here:

return 0; // replace this dummy return statement

// Part 2 student code ends here. } }

output

image text in transcribed

image text in transcribed

image text in transcribed

DataStructureException.java StackArray java StackArayDrawing.java xStackPractice.java x Source History 1The DataStructureException class Anderson, Franceschi 5 public class DataStructureException extends Exception 7public DataStructureException String s) 9 super 10 12 DataStructureException.java StackArray java StackArayDrawing.java xStackPractice.java x Source History 1The DataStructureException class Anderson, Franceschi 5 public class DataStructureException extends Exception 7public DataStructureException String s) 9 super 10 12

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 Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions