COSC2006 Lab 8 - The Stack
Lab Demo: The Array Stack. The stack operations (push, pop, peek, etc.) and how to do them on paper. Think about what we're doing, and how to put it into code.
The Stack. Create an array-based stack to hold characters. Implement the following methods: boolean isEmpty(), void push(char item), char pop(), void popAll() and char peek(). You will need a constructor to set up the array reference with a specified number of spaces. Do not dynamically resize the array or set a constant size. Do NOT use a Stack or any other Java-provided data structure. Doing so will result in a mark of zero for this lab. You do not need to create a toString method.
Reversing Strings. One great use of a stack is to reverse strings. Using your stack, create a test program to collect an input string from the user, and display the reverse of the string. Your input string must be able to have spaces in it, and the output must be all on the same line.
Example: The string "Data Structures are cool" would print "looc eras
Parenthesis Checking. Write another test class to check that a
ataD
I string (that also must
be able to have spaces in it) contains the same number of opening parenthesis "(" characters as
it does closing parenthesis ")" characters. Show if the string is valid or not by pushing opening
parenthesis characters to the stack and popping when a closing parenthesis character is found.
Assume that all opening parenthesis characters will the string before the closing ones. Your success case is that when you are done examining the entire string, the stack is empty
(Valid string).
One failure case is that there are more opening parentheses than closing parentheses. This will happen if there are still characters on the stack when you are done examining the string.
The other failure case is that there are more closing parentheses than opening parentheses.
This happens if, at any point while examining the string, you can't pop something from the
stack.
Your output should look something like the following:
Do Enter a string:(((((This string is valid ))))) The string" This string is valid)" has the correct number of parentheses.
Enter a string:(((((This string has too many opening parentheses ))) The string "This string has too many opening parentheses )))" has too many opening parentheses.
Enter a string:((((( This string has too many closing parentheses)}}}}}}} The string "((((( This string has too many closing parentheses )})})})" has too many closing parentheses.
Copyright ©2021-2022 by Prof. Johnny Console, Algoma University. All rights reserved. No parts of this work may be reproduced by any means without prior
written permission from the author in java