Question: 7.20 ReverseStrings Complete the code for the ReverseStrings such that the user can use a stack to store Strings and then output the Strings in

7.20 ReverseStrings

Complete the code for the ReverseStrings such that the user can use a stack to store Strings and then output the Strings in the opposite order from which they were entered. You can ignore the warning: [unchecked] unchecked cast.

Here is the output from a sample run:

the beginning of a story

is often different than

the end of a story

Reverse is:

the end of a story

is often different than

the beginning of a story

ReverseStrings.java

import java.util.Scanner; public class ReverseStrings{ public static void main(String[] args){ Scanner keyboard = new Scanner(System.in); StackInterface stack = new ArrayBasedStack(3); String line; for(int i = 1; i <= 3; i++){ //enter you code here to prompt the user for a line of text, save the text, then add to the stack } System.out.println("Reverse is: "); for(int i = 1; i <=3; i++){ //enter the code to print the lines in reverse } }//end main }//end class

ArrayBasedStack.java

public class ArrayBasedStack extends Object implements StackInterface{ private int top; private T [] stack; public ArrayBasedStack(){ super(); top = -1; stack = (T[])new Object[10]; } public ArrayBasedStack(int size){ super(); top = -1; if(size > 0){ stack = (T[]) new Object[size]; } else{ stack = (T[]) new Object[10]; } } public void push(T item) { if(top < stack.length - 1) { top++; stack[top] = item; }

}

public void pop() { if(top >= 0) { stack[top] = null; top--; } }

public String peek() { String topItem; if(top >= 0) { topItem = stack[top].toString(); } else { topItem = "The stack is empty"; } return topItem; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!