Question

JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a...

JAVA QUESTION 2.One use of a Stack is to reverse the order of input.

Write a complete method that reads a series of Strings from the user. The user enters "end" to stop inputting words. Then, output the Strings in reverse order of how they were entered. Do not output the String “end”.

Use a stack to accomplish this task. Invoke only the methods push, pop, peek, and isEmpty on the stack object.

Here is an example of how the program would run (user input in blue):

Enter a word or 'end' to quit: Hello

Enter a word or 'end' to quit: Java

Enter a word or 'end' to quit: World

Enter a word or 'end' to quit: end

You entered (in reverse):

World

Java

Hello

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;
import java.util.Stack;

public class StackReverse {

    public static void main(String[] args) {
        Stack<String> strings = new Stack<>();
        Scanner in = new Scanner(System.in);
        String str;
        while (true) {
            System.out.print("Enter a word or 'end' to quit: ");
            str = in.next();
            if(str.equals("end")) {
                break;
            }
            strings.push(str);
        }
        System.out.println("You entered (in reverse):");
        while (!strings.isEmpty()) {
            System.out.println(strings.pop());
        }
    }

}

Enter a word or end to quit: Hello Enter a word or end to quit: Java Enter a word or end to quit: World Enter a word or

Add a comment
Know the answer?
Add Answer to:
JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    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...

  • Write a Java program that will create an array of Strings with 25 elements. Input Strings...

    Write a Java program that will create an array of Strings with 25 elements. Input Strings from the user and store them in the array until either the user enters “quit” or the array is full. HINT: the sentinel value is “quit” all lowercase. “Quit” or “QUIT” should not stop the loop. HINT: you have to use the equals method (not ==) when you are comparing two Strings. After the input is complete, print the Strings that the user entered...

  • C++ Program Takes Strings Until 'QUIT' Input then Displays Strings in Reverse Order

    Write a C++ program that reads strings until "QUIT" input, which then displays all strings in the reverse order they were entered.Tell the user the program will read strings until "QUIT" is entered.LoopAsk user for a stringIf the string is "QUIT" break the loop, otherwise store data3. Inform user data will be displayed in reverse.4. Display ALL strings in reverse.5. Free the space used to store the strings.

  • Using Java You are given a Node class and a List class: public class Node {...

    Using Java You are given a Node class and a List class: public class Node {    int   data;     Node next; } public class List {     Node first; } You are also given a Stack class. The following functions are available for use: public class Stack { public boolean isEmpty(){}; public void push(int n){}; public int pop(){};} Write a Java method snglyRevToStck that pushes the data found in a linked list t in reverse order into the stack...

  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

  • Write a program that takes in a line of text as input, and outputs that line of text in reverse.

     4.24 LAB: Print string in reverse Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or 'q" for the line of text. Ex: If the input is: Hello there Hey quit the output is: ereht ollel yeH

  • In Java please. 2. Create a stack class MyStack2 that extends ArrayList. Write a test program...

    In Java please. 2. Create a stack class MyStack2 that extends ArrayList. Write a test program that reads a number of strings from the user then displays them in reverse order. 3. Modify your code in (2) so that MyStack become a generic class (i.e. MyStack).

  • Write a program that takes in a line of text as input, and outputs that line of text in reverse.

    Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text.Ex: If the input is:Hello there Hey quitthen the output is:ereht olleH yeHThere is a mistake in my code I can not find. my output is : ereht olleH ereht olleHyeHHow can I fix this?I saw some people use void or the function reverse but we didnt...

  • Use Java to implement a basic stack using an array of integers. For the stack, you...

    Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...

  • java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...

    java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> {    LinkedList<T> list;       public Stack() {        list = new LinkedList<T>();    }       public boolean isEmpty() {        return list.isEmpty();   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT