Answer to Question #280106 in Java | JSP | JSF for Jay

Question #280106

1. Using your preferred IDE or this online IDE, create a Java stack consisting of four (4) book titles entered by the user. Pop the stack's elements one by one; each popped element will be added to a queue. Then, print the content of the queue.



1
Expert's answer
2021-12-15T16:23:06-0500
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Stack<String> stack = new Stack<>();
        Queue<String> queue = new LinkedList<>();

        for (int i = 0; i < 4; i++) {
            System.out.println((i + 1) + " Title: ");
            stack.push(in.nextLine());
        }
        for (int i = 0; i < 4; i++) {
            queue.add(stack.pop());
        }
        for (String title : queue) {
            System.out.println(title);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog