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

JAVA need Help PLZ fix today!!! Write two methods using stacks and queues. (NO O

ID: 3837991 • Letter: J

Question

JAVA need Help PLZ fix today!!!

Write two methods using stacks and queues. (NO OTHER ADT's ALLOWED)

Make sure to include a main program that demonstrates that your methods work.

Write a method stutter that accepts a queue of integers as a parameter and replaces every element of the queue with two copies of that element.front [1, 2, 3] back

becomes

front [1, 1, 2, 2, 3, 3] back

Write a method mirror that accepts a queue of strings as a parameter and appends the queue's contents to itself in reverse order.front ["a", "b", "c"] back

becomes

front ["a", "b", "c", "c", "b", "a"] back

Explanation / Answer

/**
* Tester java program for stutter method
* */
//StutterTester.java
import java.util.LinkedList;
import java.util.Queue;
public class StutterTester
{
   public static void main(String[] args)
   {
       //create an instanec of Queue of LinkedList class
       //of type Integer
       Queue<Integer>queue=new LinkedList<Integer>();
       //add elements to the front of the queue
       queue.add(new Integer(1));
       queue.add(new Integer(2));
       queue.add(new Integer(3));
      
       System.out.println("Before stutter calling");
       System.out.println(queue.toString());
       stutter(queue);
       System.out.println("After stutter calling");
       System.out.println(queue.toString());
   }

   /**
   * The method stutter that takes Queue that takes queue
   * object and add each element twice tothe queue
   * */
   private static void stutter(Queue<Integer> queue) {
      
       int size=queue.size();
      
       for (int i = 0; i < size; i++)
       {
           int num=queue.remove();
           queue.add(new Integer(num));
           queue.add(new Integer(num));
       }
      
   }
}

----------------------------------------------------------------------------------------------------

Sample Output:

Before stutter calling
[1, 2, 3]
After stutter calling
[1, 1, 2, 2, 3, 3]

----------------------------------------------------------------------------------------------------


/**
* Tester java program for mirror method
* */
//MirrorTester.java
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class MirrorTester
{
   public static void main(String[] args)
   {
       Queue<String>queue=new LinkedList<String>();
       queue.add("a");
       queue.add("b");
       queue.add("c");

       System.out.println("Before mirror calling");
       System.out.println(queue.toString());
       mirror(queue);
       System.out.println("After mirror calling");
       System.out.println(queue.toString());
   }

  
   /**
   * The method mirror that take Queue of LinkedList class object
   * and using stack , add the mirror of the elements
   * to the queue.
   * */
   private static void mirror(Queue<String> queue)
   {
       //stack object
       Stack<String>stack=new Stack<String>();
       int size=queue.size();

       for (int i = 0; i < size; i++)
       {
           //here remove acts like deque method for queue
           String str=queue.remove();
           //here add method acts like enque method for queue
           queue.add(str);
           stack.push(str);
       }
      
       //now remove from stack top and add to the queue
       //to make queue as mirror
       while(!stack.isEmpty())
       {
           String str=stack.pop();
           queue.add(str);
       }

   }
}

----------------------------------------------------------------------------------------------------

Sample Output:

Before mirror calling
[a, b, c]
After mirror calling
[a, b, c, c, b, a]