Recitation 4

Introduction:

In this recitation, you will practice using stacks by writing code to reverse a stack.

ReversibleStack through Inheritance

Use ReversibleStack.java and fill in the two empty methods. The reverse method must reverse the values stored in the ReversibleStack object and return a new ReversibleStack object. Do not use arrays, linked lists, array lists, vectors, or queues. Run the program to show the TA that it works correctly. Note that this uses the LinkedStack.java class.

ReversibleStack through Composition

Write a ReversibleStackComposition class. This class should have at least one field: a stack object (it must be of type StackInterface). In addition to push, pop, and peek methods, implement a reverse method. Just like the reverse method above, this method should return a stack that is the reverse of the stack field object. The stack field should still have the same order as before the reverse method was called.

Show the TA that your class works correctly by showing the output of:

ReversibleStackComposition<String> stk1 = new ReversibleStackComposition<String>();
ReversibleStackComposition<String> stk2;

stk1.push("A"); System.out.println("push A");
stk1.push("B"); System.out.println("push B");
stk1.push("C"); System.out.println("push C");

stk2 = stk1.reverse();

System.out.println("\nStk1 contains:" + stk1);

System.out.println("\nThe reverse of Stk1 contains:" + stk2);

System.out.println("\nStk1 not destroyed by reverse() method:" + stk1);

Submission and Grading:

This is for your practice. You should be able to complete the first program by the end of recitation, and probably the second program. If you complete a program, call the TA over to look over your program to make sure it is correct. If you do not have time to complete the programs before the end of recitation, you have the option of uploading it to CourseWeb once you are finished to receive feedback on it from the TA. If you upload it after Monday, October 5, please email the TA so he knows to look at it.