Question

In: Computer Science

Using the Stack ADT: Create a program that uses a stack. Your program should ask the...

Using the Stack ADT:

  1. Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry.
  2. (Optional) Create a similar program that uses a stack. Your new program should ask the user to input a line of text and then it should print out the line of text in reverse. To do this your application should use a stack of Character.

In Java please.

Solutions

Expert Solution

The solution of the above code is provided below. If you like the solution don't forget to give a thumbs up.

Solution part a:

ReverseStackOfWords.java

import java.util.Stack;

import java.util.*;

public class ReverseStackOfWords {

        public static void main(String[] args) {

                Scanner sc = new Scanner(System.in);

                Stack<String> words = new Stack<>();

                // storing few lines of text into text variable of string type
                String text = sc.nextLine();

                // splitting and storing into array of string type
                String[] stringArray = text.split(" ");

                // pushing string words into stack
                for (String str : stringArray) {
                        words.push(str);
                }

                // using pop operation to remove and print the same elements using using poll()
                // printing the elements in reverse order
                
                System.out.println("Entered Text in Reverse order \n");
                
                while(words.empty() != true) {
                        System.out.print(words.pop()+" ");
                }
        }

}

Output part 1:

Solution part b:

ReverseStackOfCharacters.java

import java.util.Scanner;
import java.util.Stack;

public class ReverseStackOfCharacters {

        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);

                Stack<Character> charStack = new Stack<>();

                // storing few lines of text into text variable of string type
                String text = sc.nextLine();

                // splitting and storing into array of string type
                Character[] charArray = new Character[text.length()];
                
                for(int c=0; c<text.length(); c++) {
                        charArray[c] = text.charAt(c);
                }

                // pushing string words into stack
                for (Character ch : charArray) {
                        charStack.push(ch);
                }

                // using pop operation to remove and print the same elements using using poll()
                // printing the elements in reverse order

                System.out.println("Entered Text in Reverse order \n");

                while(charStack.empty() != true) {
                        System.out.print(charStack.pop());
                }
        }

}


Output part b:

End of solution


Related Solutions

2. Using the Stack ADT: Create a program that uses a stack. Your program should ask...
2. Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. In Java please.
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the...
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the user to input a few lines of text and then outputs strings in same order of entry. (use of the library ArrayDeque) In Java please.
write an implementation of the ADT stack that uses a resizeable array to represent the stack...
write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the end of the array. Please use c++ for this question.
Create an ADT class that creates a friend contact list. The program should be able to...
Create an ADT class that creates a friend contact list. The program should be able to add, remove and view the contacts. In C++
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of the method push, pop, top, isEmpty, and size. You should not use any extra data structure. Related codes: public interface Stack<E> { int size( ); boolean isEmpty( ); void push(E e); E top( ); E pop( ); } public class LinkedStack<E> implements Stack<E> { private SinglyLinkedList<E> list = new SinglyLinkedList<>( );    public LinkedStack( ) { }    public int size( ) { return...
Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for...
Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an array of Strings to hold the user’s input –Use a while loop to prompt for and read the Strings (lines of text) from the user at the command line. Typically, this would be a 'for' loop since we know the number of times to execute the loop based upon the number...
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create 2 threads that will act as counters. One thread should count down from 5 to 0. Once that thread reaches 0, then a second thread should be used to count up to 20.
Using (C programming language) Create a health monitoring program, that will ask user for their name,...
Using (C programming language) Create a health monitoring program, that will ask user for their name, age, gender, weight, height and other health related questions like blood pressure and etc. Based on the provided information, program will tell user BMI, blood pressure numbers if they fall in healthy range or not and etc. Suggestions can be made as what should be calorie intake per day and the amount of exercise based on user input data. User should be able to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT