/** * Class CharStack is based on the StackX class from Robert LaFore's book, * "Data Structures and Algorithms in Java" published by the Waite Group Press. * This class creates a char array of the size passed to the constructor. * Each element in the stack array can hold one char value. * * @author Robert LaFore, Modified by David Thibodeau * @version 16-July-2002 */ public class CharStack { private int maxSize; // size of stack array private char[] stackArray; private int top; // top of the stack /** * Constructor for objects of class CharStack */ public CharStack(int s) { maxSize = s; // set array size stackArray = new char[maxSize]; // create array top = -1; // top indicator set to show no items in array yet } /** * push method - Pushes a char onto the stack * * @param c the char to be pushed * @return void */ public void push(char c) { stackArray[++top] = c; // increment top, insert char } /** * pop method - Pops a char off the stack and returns the * char value. * * @param void * @return char The value popped off the stack */ public char pop() { return stackArray[top--]; // return char, decrement top } /** * push method - Pushes a char onto the stack * * @param c the char to be pushed * @return void */ public char peek() { return stackArray[top]; // return char at top of stack } /** * isEmpty method - Returns true is the stack is empty * * @param void * @return boolean - true if the stack is empty */ public boolean isEmpty() { return (top == -1); // returns true if top equals -1 } /** * isFull method - Returns true is the stack array is full * * @param void * @return boolean - true if the stack is full */ public boolean isFull() { return (top == maxSize); // returns true if top equals maxSize of the array } } // end Class CharStack ///////////////////////////////////////////////////////////////////////////////// /** * Class StackGUI provides a front-end for the CharStack class. The user * enters a sentence into a textfield. Once entered, the user clicks the reverse * button and by using the stack, the order of the characters are reversed * and output into another TextField object. * * @author David Thibodeau * @version 16-July-20002 */ import java.awt.* ; import java.awt.event.*; import javax.swing.*; public class StackGUI extends JFrame implements ActionListener { // Sentence input panel JLabel inputLabel; JTextField inputText; JButton inputButton; JPanel inputPanel; // Sentence output panel JLabel outputLabel; JTextField outputText; JPanel outputPanel; /** * Constructor for objects of class StackGUI * This will generate the GUI for the class StackGUI */ public StackGUI() { setTitle( "Sentence Reverser" ); setDefaultCloseOperation( EXIT_ON_CLOSE ); // input group inputLabel = new JLabel( "Enter your sentence here:" ); inputText = new JTextField(30); // Sets concurrent # of chars viewable to 30 inputButton = new JButton( "Reverse" ); inputButton.addActionListener(this); inputButton.setActionCommand( "Reverse" ); inputPanel = new JPanel(); inputPanel.add ( inputLabel ); inputPanel.add ( inputText ); inputPanel.add ( inputButton ); // output group outputLabel = new JLabel ( "Reversed sentence appears here:" ); outputText = new JTextField(30); outputText.setEditable( false ); outputPanel = new JPanel(); outputPanel.add ( outputLabel ); outputPanel.add ( outputText ); // content pane getContentPane().setLayout( new BoxLayout( getContentPane(), BoxLayout.Y_AXIS ) ); getContentPane().add( inputPanel ); getContentPane().add( outputPanel ); } /** * actionPerformed method. This method handles the GUI click events * for the reverse button. When the button is clicked, the sentence * is pushed onto the stack object one char at a time. Once this is * complete, a while loop pops the stack until the isEmpty method * returns true. Once that happens, the output text is placed into * the outputText textfield. * @param ActionEvent supplied by Java's Swing event handler * @return void */ public void actionPerformed( ActionEvent evt ) { int stackSize; String sentence = "", reversed = ""; CharStack theStack; if ( evt.getActionCommand().equals( "Reverse" ) ) { sentence = inputText.getText(); stackSize = sentence.length(); theStack = new CharStack(stackSize); for(int i = 0; i < stackSize; i++) { theStack.push( sentence.charAt(i) ); } // pushes all chars onto stack while( !theStack.isEmpty() ) { reversed += theStack.pop(); } // pops until isEmpty is true outputText.setText( reversed ); } } /** * main method. This creates a new instance of StackGUI, then * instantiates a new WindowQuitter() object and lastly sets the * size and visibility of the GUI window. */ public static void main ( String[] args ) { // New GUI instance StackGUI stackApp = new StackGUI(); // New WindowQuitter instance WindowQuitter wquit = new WindowQuitter(); stackApp.addWindowListener( wquit ); // Set window size & make visible stackApp.setSize( 400, 200 ); stackApp.setVisible( true ); } // end main } // end Class StackGUI class WindowQuitter extends WindowAdapter { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } }