Write a program that uses a stack to reverse its inputs. Your stack must be gene
ID: 3840038 • Letter: W
Question
Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: • push, • pop, • isEmpty (returns true if the stack is empty and false otherwise), and • size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must throw an IndexOutOfBoundsException if the user attempts to “pop” something off an empty stack. Name your stack class “Stack” and put it in a file named Stack.java. Create a StackTest.java file to demonstrate all of the methods of your stack. Instantiate two stacks; one for String and one for Integer. Push words/integers onto your stack. After you have pushed a series of words/integers on your stack use a while loop to pop and print each item. If your stack is working properly your inputs should print in reverse order. Be sure that you calls to pop are in a try statement and will catch the IndexOutOfBoundsException, if thrown. Note: Java has a Stack class, but you may not use it. You must create your own Stack class and your class must have the following operations (all of which must be demonstrated): push, pop, isEmpty, and size. Here is an example screen shot of a working stack that reverses words and integers:
BMIS 312 Week 8 NetBeans IDE 7.3 le Edit View Navigate Source Refactor Run Debug Profile Team Iools Window Help default config> Projects Fires 5ervices Start Page StackTest.java R Stack java BMIS 312 W Source History BMIS 312 Week 2 System out print (integerStack.pop BMIS 312 Week 3 63 BMIS 312 Week 4 64 catch (Indexoutor BoundsException e) BMIS 312 week 5.1 System out println VnThe Integer stack is empty BMIS 312 week 52 66 BMIS 312 Week 6 67 BMIS 312 week 62 Systems out println BMIS 312 week 7 I/F) sa n", 69 System printf The Integer atack ia empty source Packages 70 true False (integerstack.iaEmpty E- bmis 312 week 7 71 System out printf The size of the Integer stack is 8d n", integer Stack Par java 72 java ParTest. 73 Libraries 74 .312 Week 8 BMIS StackTest Source Packages bmis 312 yweek 8 Output BMIS 312-Week 8 (run main-Navigato pushing ain in spi Members empty f the St i E-N StackTest The St tack T/F Fal empty D main (StringO args Popping ff of the St trueFalsel b) String Spain in rain The The stack is e pty (T/F) True The size of the string atack i o Pushing 1 2 3 4 f the Integ The Intege T/F Fel. Popping elements off of the stack The Integer atack empty II/E] True the Integ BUILD SUCCESSEUL total t Search (Ctrl+ 69166 INSExplanation / Answer
Solution :-
Here the class "InPlaceStackReverse" POP the current element and checks the satck size = 1, if true then it calls the function "placeCurrAtBottomOfStack". this function will PUSH the popped Items in previous function. So the elements are reversed.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Stack;
public class InPlaceStackReverse
{
static void reverseStack(Stack s)
{
printStack(s, "reverse1");
Object curr = s.pop();
if (s.size() != 1)
reverseStack (s);
placeCurrAtBottomOfStack (s, curr);
printStack(s, "place " + curr + " AtBottom");
}
static void placeCurrAtBottomOfStack(Stack s, Object curr)
{
Object top = s.pop();
if (s.size() == 0)
s.push(curr);
else
placeCurrAtBottomOfStack(s, curr);
s.push(top);
}
---------------------------------------------------------------------------------------------------------------------------------------------------
// main function to test the above
public static void main (String args[])
{
Stack s = new Stack ();
s.push(1); s.push(2); s.push(3);
s.push(4); s.push(5); s.push(6);
reverseStack (s);
}
---------------------------------------------------------------------------------------------------------------------------------------------------
// function to print a stack with some message.
static void printStack(Stack s, String msg)
{
System.out.print(" " + msg + " = ");
for (int i=s.size()-1; i>=0; i--)
System.out.print(s.get(i)+", ");
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.