Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

replace the stack with queues 1 import java.util.Scanner; 2 import java.util.Sta

ID: 3774808 • Letter: R

Question

replace the stack with queues

1 import java.util.Scanner;
2 import java.util.Stack;
3
4 public class ToyStack{
5 public static void main(String args[])
6 {
7 Scanner s = new Scanner(System.in);
8 Stack<Integer> toystack = new Stack<>();
9 char c;
10 boolean input=false;
11 while(input==false)
12 {
13
14 System.out.println("Enter E, R, A, S, or M");
15 c = s.next().charAt(0);
16 if(c== 'E')
17 {
18 pushInteger(toystack);
19 }
20 if(c=='R')
21 {
22 removeInteger(toystack);
23 break; // Break should be here
24 }
25 if(c=='A')
26 {
27 computeSum(toystack);
28 }
29 if(c=='S')
30 {
31 absvalueInteger(toystack);
32 }
33 if(c=='M')
34 {
35 multiInteger(toystack);
36 }
37
38 }
39 }
40 public static Stack<Integer> pushInteger(Stack <Integer> toystackE)
41 {
42 Scanner kb=new Scanner(System.in);
43 System.out.println("Enter two integers: ");
44 int a = kb.nextInt();
45 int b = kb.nextInt();
46 toystackE.push(a);
47 toystackE.push(b);
48 return toystackE;
49 }
50 public static void removeInteger(Stack <Integer> toystackeR)
51 {
52 while(toystackeR.empty() !=true)
53 {
54 System.out.println(toystackeR.pop()); // This is how you display your result.
55 }
56
57 }
58 public static void computeSum(Stack <Integer> toystackA)
59 {
60 int a,b;
61 a =toystackA.pop();
62 b =toystackA.pop();
63 int c;
64 c=a+b;
65 toystackA.push(c);
66 }
67 public static void absvalueInteger(Stack <Integer> toystackS)
68 {
69 int a=toystackS.pop();
70 int b=toystackS.pop();
71 int c=Math.abs(b-a);
72 toystackS.push(c);
73 }
74 public static void multiInteger(Stack <Integer> toystackM)
75 {
76 int a,b,c;
77 a=toystackM.pop();
78 b=toystackM.pop();
79 c=a*b;
80 toystackM.push(c);
81 }
82 public static void printStack(Stack <Integer> toystack)
83 {
84 if(toystack.isEmpty())
85 System.out.println("Toystack is empty");
86 else
87 System.out.printf("%s TOP ", toystack);
88 }
89 }
90
91

Explanation / Answer

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class ToyQueue {
   public static void main(String args[]) {
       Scanner s = new Scanner(System.in);
       Queue<Integer> toyQueue = new LinkedList<Integer>();
       char c;
       boolean input = false;
       while (input == false) {

           System.out.println("Enter E, R, A, S, or M");
           c = s.next().charAt(0);
           if (c == 'E') {
               pushInteger(toyQueue);
           }
           if (c == 'R') {
               removeInteger(toyQueue);
               break; // Break should be here
           }
           if (c == 'A') {
               computeSum(toyQueue);
           }
           if (c == 'S') {
               absvalueInteger(toyQueue);
           }
           if (c == 'M') {
               multiInteger(toyQueue);
           }

       }
   }

   public static Queue<Integer> pushInteger(Queue<Integer> toyQueueE) {
       Scanner kb = new Scanner(System.in);
       System.out.println("Enter two integers: ");
       int a = kb.nextInt();
       int b = kb.nextInt();
       toyQueueE.add(a);
       toyQueueE.add(b);
       return toyQueueE;
   }

   public static void removeInteger(Queue<Integer> toyQueueeR) {
       while (toyQueueeR.isEmpty() != true) {
           System.out.println(toyQueueeR.remove()); // This is how you display
                                                       // your result.
       }

   }

   public static void computeSum(Queue<Integer> toyQueueA) {
       int a, b;
       a = toyQueueA.remove();
       b = toyQueueA.remove();
       int c;
       c = a + b;
       toyQueueA.add(c);
   }

   public static void absvalueInteger(Queue<Integer> toyQueueS) {
       int a = toyQueueS.remove();
       int b = toyQueueS.remove();
       int c = Math.abs(b - a);
       toyQueueS.add(c);
   }

   public static void multiInteger(Queue<Integer> toyQueueM) {
       int a, b, c;
       a = toyQueueM.remove();
       b = toyQueueM.remove();
       c = a * b;
       toyQueueM.add(c);
   }

   public static void printQueue(Queue<Integer> toyQueue) {
       if (toyQueue.isEmpty())
           System.out.println("ToyQueue is empty");
       else
           System.out.printf("%s TOP ", toyQueue);
   }
}

OUTPUT:

Enter E, R, A, S, or M
E
Enter two integers:
1
2
Enter E, R, A, S, or M
A
Enter E, R, A, S, or M
E
Enter two integers:
1
3
Enter E, R, A, S, or M
S
Enter E, R, A, S, or M
E
Enter two integers:
3
4
Enter E, R, A, S, or M
M
Enter E, R, A, S, or M
R
3
4
6