Question: Your task is to create software for a stack as an object. You need to create a simple stack class and then test it by
Your task is to create software for a stack as an object. You need to create a simple stack class and then test it by using the stack to reverse the items in an array of Strings.
You should create an array-based stack class with push and pop methods, similar to what is described in the reading assignment. The datatype for this stack will be String. Your stack class should implement a stack of Strings. (Each item in the stack will be a String holding the name of a city.)
You should create a String array in your main() method that contains the list of cities shown below in order. This is an array of source data -- it is not the array in your stack class, The list represents the path to get from Philadelphia to Chicago. You can use an explicit array declaration to do this, such as:
String cities[] = { "Philadelphia, PA", "Harrisburg, PA", "Pittsburg, PA", "Cleveland, OH", "Toledo, OH", "Gary, IN", "Chicago, IL" };
Our task is to test the Stack Class by reversing the order to get the path from Chicago to Philadelphia. Remember, this isn't about the path; we are just using this problem to test our stack class.
After you create the Stack Class, your main method in the project class should :
- instantiate the String array of cities. This array is not part of your stack software. It is data in the main() method to be used to test your Stack class.
- instantiate a String stack using your Stack class.
- iterate the source data array and print the cities in order, showing the path from Philadelphia to Chicago. Make sure your printout is easy to read.
- iterate the data source array again, and push each city name onto the stack one at a time.
- using a loop that continues until the stack is empty, pop each city name from the stack and put it back into the data source array starting in order at position 0 in the data source array. The result should be the same data that you started with, but in reverse order.
- Iterate the source data array again, printing the cities in the order in which they are in the array.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
