Question: /* The DataStructureException class Anderson, Franceschi */ public class DataStructureException extends Exception { public DataStructureException( String s ) { super( s ); } } /*

/* The DataStructureException class Anderson, Franceschi */

public class DataStructureException extends Exception { public DataStructureException( String s ) { super( s ); } }

/* 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. } }

/* 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 < StackArray.CAPACITY; i++ ) { g.drawRect( XSTART, YSTART - i * HEIGHT, WIDTH, HEIGHT ); g.drawString( "" + i, XSTART - WIDTH / 4, (int) ( YSTART - ( i - 0.7 ) * HEIGHT ) ); } }

public void drawStackContents( Graphics g ) { int i = 0; g.setColor( Color.BLACK ); for ( i = 0; i <= sa.getTop( ); i++ ) { g.drawString( "" + sa.get( i ), XSTART + WIDTH / 2 - 5, (int) ( YSTART - ( i - 0.7 ) * HEIGHT ) ); } g.setColor( Color.RED ); for ( i = sa.getTop( ) + 1; i < StackArray.CAPACITY; i++ ) { g.drawString( "" + sa.get( i ), XSTART + WIDTH / 2 - 5, (int) ( YSTART - ( i - 0.7 ) * HEIGHT ) ); } }

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

/* StackPractice * Anderson, Franceschi */

import java.awt.*; import javax.swing.*; import java.awt.event.*;

public class StackPractice extends JFrame { Container contents; // GUI components private JButton push; private JButton pop;

private ButtonHandler bh;

private StackArrayDrawing sad;

private static StackPractice app; private boolean firstTime = true;

public StackPractice( ) { super( "Push or pop" ); contents = getContentPane( ); contents.setLayout( new FlowLayout( ) );

sad = new StackArrayDrawing( );

push = new JButton( "push" ); contents.add( push ); pop = new JButton( "pop" ); contents.add( pop );

bh = new ButtonHandler( ); push.addActionListener( bh ); pop.addActionListener( bh );

setSize( 500,400 ); setVisible( true ); }

private void animate( boolean pushSuccess, int pushedValue ) { sad.setPushSuccess( pushSuccess ); sad.setPushedValue( pushedValue ); try { repaint( ); Thread.sleep( 200 ); } catch ( InterruptedException e ) { System.out.println( "IE Exception " + e.getMessage( ) ); System.out.println( e.toString( ) ); } }

private void animate( int poppedValue ) { sad.setPopSuccess( true ); sad.setPoppedValue( poppedValue ); try { repaint( ); Thread.sleep( 200 ); } catch ( InterruptedException e ) { System.out.println( "IE Exception " + e.getMessage( ) ); System.out.println( e.toString( ) ); } }

private void animate( ) { sad.setPopSuccess( false ); try { repaint( ); Thread.sleep( 200 ); } catch ( InterruptedException e ) { System.out.println( "IE Exception " + e.getMessage( ) ); System.out.println( e.toString( ) ); } }

public void paint( Graphics g ) { if ( firstTime ) firstTime = false; super.paint( g ); sad.draw( g ); }

public static void main( String [] args ) { app = new StackPractice( ); app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); }

private class ButtonHandler implements ActionListener { private boolean success = false;

public void actionPerformed( ActionEvent e ) { sad.setStarted( true ); if ( e.getSource( ) == push ) { sad.setOperation( true ); int insertValue = getValue( ); success = sad.push( insertValue ); animate( success, insertValue ); } else // ( e.getSource( ) == pop ) { sad.setOperation( false ); try { int popped = sad.pop( ); animate( popped ); } catch( DataStructureException dse ) { System.out.println( dse.getMessage( ) ); animate( ); } } } }

public int getValue( ) { int value = 0; boolean goodInput = false; while ( !goodInput ) { try { String answer = JOptionPane.showInputDialog ( null, "Enter an integer value" ); value = Integer.parseInt( answer ); goodInput = true; } catch( Exception e ) { } } return value; } }

Please only answer if you know the answer because I do not have a lot of questions remaining for this month. Changes have to be made ony to the public class StackArray and everything else stays the same. Thank you.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!