Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions

Question

Question Can I collect benefits if I become disabled?

Answered: 1 week ago

Question

Question May I set up a Keogh plan in addition to an IRA?

Answered: 1 week ago