5. Stacks
Feel free to use your laptop
You are strongly encourage to work with others
When you get stuck, ask those sitting around you for help
Get used to working together in the labs
Peer teaching and peer learning has been empirically shown to be very effective
5.1. Playing with Stacks
The goal is to implement an ArrayStack
based on the the details discussed in the
Stack Topic and the ArrayStack Topic.
5.1.1. Implementing an ArrayStack
Download the
Stack interface
and add it to thesrc
folderCreate an
ArrayStack
class for the implementationComplete the
ArrayStack
class based on the ArrayStack Topic’s descriptionNote
Obviously one can simply download the
ArrayStack
code and copy/paste it into theArrayStack
class, but this defeats the purpose of the lab. Instead, slowly and deliberately implement each method and take the time to understand the details.Add the
ArrayStackTest
unit tests to the project in atest
folderRun the
ArrayStackTest
unit tests and ensure all tests pass
5.1.2. Reversing a String
Write a function called
reverseString
that takes a string and returns the reverse of the originalThis function must use a stack for the reversing of the string
Below is a skeletal method and some simple testing code
public static void main(String[] args) { String alphabet = "abcdefghijklmnopqrstuvwxyz"; String tebahpla = reverseString(alphabet); System.out.println(alphabet); System.out.println(tebahpla); } public static String reverseString(String originalString) { Stack<Character> characterStack = new ArrayStack<>(); StringBuilder stringBuilder = new StringBuilder(); // Add Code Here return stringBuilder.toString(); }