Question

You are required to write a program in JAVA based on the problem description given. Read...

You are required to write a program in JAVA based on the problem description given. Read the problem description and write a complete program with necessary useful comment for good documentation. Compile and execute the program. ASSIGNMENT OBJECTIVES: • To introduce queue data structure. DESCRIPTIONS OF PROBLEM:

Exercise : Write a program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be the same as the input. Your program should be using a stack and a queue to complete this process.

1. Push into stack

2. Pop from stack

3. Enqueue into queue

4. Dequeue from queue

5. Push into stack

6. Pop from stack and display

Homework Answers

Answer #1

Program :

import java.util.Stack;
import java.util.Queue;
import java.util.*;
import java.util.Scanner;

class Main
{
    // create an empty stack and Queue of characters
    static Stack<Character> stack = new Stack<Character>();
        static Stack<Character> stack1 = new Stack<Character>();
        static Queue<Character> q = new LinkedList<Character>(); 
        public static String Result(String str)
        {
            // push every character of the given string into the stack
                char[] ch = str.toCharArray();
                int k = 0;
                for (int i = 0; i < str.length(); i++)
                        stack.push(ch[i]);
                // pop characters from the stack until it is empty to Queue
                while (!stack.isEmpty())
                {
                         q.add(stack.pop());
                }
                // push characters from the Queue until it is empty to stack
                while (!q.isEmpty())
                {
                        stack1.push(q.remove());
                }
                while (!stack1.isEmpty())
                {
                        // assign each popped character back to the character array
                        ch[k++] = stack1.pop();
                }
                // convert the character array into string and return it
                return String.copyValueOf(ch);
        }
        public static void main (String[] args)
        {
                Scanner o = new Scanner(System.in);  // Create a Scanner object
        System.out.print("Enter String:");

        String str = o.nextLine();  // Read user input
                System.out.println("Result :" +Result(str)); //Call Result method and print output.
        }
}

Output :

Thank You Have a Great Day !!! Please do like.

Know the answer?
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
Similar Questions
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These...
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These same mechanisms can be used as a part of writing a simple compiler. Write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers (to make things easier) such as (6 + 2) • 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of this infix expression is 6...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
In JAVA: Write a program that reads an integer, a list of words, and a character....
In JAVA: Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list of words will always contain fewer than 20 words. Ex: If the input is: 4 hello zoo sleep drizzle...
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the...
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the input infix expression one character at a time and push every character read ( other than ')' ) onto stk1. Do not push the character read if it is ')'. (3) As long as stk1 is not empty, do the following:            Fetch the top character ( call it c ) of stk1 and pop stk1.            if c is an alphabetic character (a-z),...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the...
Problem: Read in a word and display the requested characters from that word in the requested...
Problem: Read in a word and display the requested characters from that word in the requested format. Write a Java program that ● prompts the user for a word and reads it, ● converts all characters of the input word to upper case and displays every character whose index is a multiple of 3 starting from the 1st one, ● next converts all characters of the input word to lower case and displays the word with the characters in reverse...
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Note: There is a white space in between the numbers. Java programming please answer ASAP before 2:30
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty queue,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT