Question

In: Computer Science

Convert the Queue to Generic implementation: public class MyQueueImpl implements MyQueue {    private int capacity;...

Convert the Queue to Generic implementation:

public class MyQueueImpl implements MyQueue {
   private int capacity;
   private int front;
   private int rear;
   private int[] arr;
   public MyQueueImpl(int capacity){
       this.capacity = capacity;
       this.front = 0;
       this.rear = -1;
       this.arr = new int[this.capacity];
   }
   @Override
   public boolean enQueue(int v) {      
           if(this.rear == this.capacity - 1) {
               //Perform shift
               int tempSize = this.size();
               for(int i=0; i < tempSize; i++) {
                   arr[i] = arr[front];
                   front++;
               }
               front = 0;
               rear = tempSize - 1;
           }
           this.rear ++;
           arr[rear] = v;
           return true;
   }
   @Override
   public int deQueue() {
       return arr[front++];
      
   }
   public String toString() {
       String content = "Queue :: ";
      
       for(int i=front; i<= rear; i++) {
           content += "\n" + arr[i];
       }
       return content;
   }
   @Override
   public boolean isFull() {
       return (this.size() == this.capacity);
   }
   @Override
   public int size() {
       return rear - front + 1;
   }
   @Override
   public boolean isEmpty() {
       // TODO Auto-generated method stub
       return (this.size() == 0);
   }
   @Override
   public int peek() {
       // TODO Auto-generated method stub
       return this.arr[this.front];
   }
}

Solutions

Expert Solution

Short Summary:

  • Converted the MyQueueImpl class to implement generics.
  • We cannot create generic type array inside the MyQueueImpl class in JAVA. So, have to use generic ArrayList instead of array.
  • Shown you the test driver class and sample run

**************Please upvote the answer and appreciate our time.************

Source Code:

MyQueue.java File:

public interface MyQueue<E> {
   public boolean enQueue(E v);
   public E deQueue();
   public boolean isFull();
   public int size();
   public boolean isEmpty();
   public E peek();
}

MyQueueImpl.java File:

import java.util.ArrayList;

public class MyQueueImpl<E> implements MyQueue<E> {
   private int capacity;
   private int front;
   private int rear;
   private ArrayList<E> arr;

   public MyQueueImpl(int capacity) {
       this.capacity = capacity;
       this.front = 0;
       this.rear = -1;
       this.arr = new ArrayList<E>();
   }

   @Override
   public boolean enQueue(E v) {
       if (this.rear == this.capacity - 1) {
           // Perform shift
           int tempSize = this.size();
           for (int i = 0; i < tempSize; i++) {
               arr.add(i, arr.get(front));
               front++;
           }
           front = 0;
           rear = tempSize - 1;
       }
       this.rear++;
       arr.add(rear, v);
       return true;
   }

   @Override
   public E deQueue() {
       return arr.get(front++);

   }

   public String toString() {
       String content = "Queue :: ";

       for (int i = front; i <= rear; i++) {
           content += "\n" + arr.get(i);
       }
       return content;
   }

   @Override
   public boolean isFull() {
       return (this.size() == this.capacity);
   }

   @Override
   public int size() {
       return rear - front + 1;
   }

   @Override
   public boolean isEmpty() {
       // TODO Auto-generated method stub
       return (this.size() == 0);
   }

   @Override
   public E peek() {
       // TODO Auto-generated method stub
       return this.arr.get(this.front);
   }
}

GenericQueueTest.java File:
public class GenericQueueTest {

   public static void main(String[] args) {
      
       // IntegerQueue
       MyQueueImpl<Integer> intQueue = new MyQueueImpl<Integer>(10);
       intQueue.enQueue(10);
       intQueue.enQueue(20);
       intQueue.enQueue(30);
       intQueue.enQueue(40);
       intQueue.enQueue(50);
       System.out.println("Integer Queue: \n" + intQueue.toString());
       System.out.println("Integer Queue Peek: " + intQueue.peek());
       while(!intQueue.isEmpty()) {
           System.out.println("Integer Dequeued: " + intQueue.deQueue());
       }
      
       // IntegerQueue
       System.out.print("\n\n\n");
       MyQueueImpl<String> stringQueue = new MyQueueImpl<String>(10);
       stringQueue.enQueue("Java");
       stringQueue.enQueue("Generics");
       stringQueue.enQueue("Programming");
       stringQueue.enQueue("is");
       stringQueue.enQueue("Fun");
       System.out.println("String Queue: \n" + stringQueue.toString());
       System.out.println("String Queue Peek: " + stringQueue.peek());
       while(!stringQueue.isEmpty()) {
           System.out.println("String Dequeued: " + stringQueue.deQueue());
       }

   }

}

Sample Run:


Related Solutions

public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /**...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /** * Constructor for objects of class Classroom */ public Classroom() { this.capacity = 0; }    /** * Constructor for objects of class Classroom * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Classroom(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room...
public class Clock { private int hr; private int min; private int sec; public Clock() {...
public class Clock { private int hr; private int min; private int sec; public Clock() { setTime(0, 0, 0); } public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } public void setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec =...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
import java.util.NoSuchElementException; public class ArrayBasedDeque<AnyType> implements deque<AnyType> {       private static int MAX_SIZE = 5;...
import java.util.NoSuchElementException; public class ArrayBasedDeque<AnyType> implements deque<AnyType> {       private static int MAX_SIZE = 5; // initial array size    //DO NOT CHANGE this, it is set to 5 to make sure your code    //will pass all the tests and works with no issue.       // add all data fields which are required    /**    * ArrayBasedDeque() constructs an empty deque.    */    public ArrayBasedDeque(){        //complete    }       /**    *...
public class IntNode               {            private int data;            pri
public class IntNode               {            private int data;            private IntNode link;            public IntNode(int data, IntNode link){.. }            public int     getData( )          {.. }            public IntNode getLink( )           {.. }            public void    setData(int data)     {.. }            public void    setLink(IntNode link) {.. }         } All questions are based on the above class, and the following declaration.   // Declare an empty, singly linked list with a head and a tail reference. // you need to make sure that head always points to...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT