Question

In: Computer Science

Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private...

Fill in the following blanks for java code:::

import java.util.NoSuchElementException;


public class CircularQueue<E>
{
   private E[] queue;
   private int front = 0, rear = 0;
   private static final int DEFAULT_CAPACITY = 5;
  
   public CircularQueue(int capacity)
   {
   queue = (E[]) new Object[capacity + 1];
   }
  
   public CircularQueue()
   {
       this(DEFAULT_CAPACITY);
}

  
   //Add a method that will determine if the queue is empty. Recall that the queue is empty if the front and
   //rear index the same location in the array:

   public boolean isEmpty()
   {
       return __________ = __________;
}

   private boolean isFull()
   {
       return ((rear + 1) % queue.length) == front;
}
  
   public void enqueue(E newElement)
   {
      
       if (isFull())
       {
      
       this.expand();
      
   }
       //To add an element to the rear of the queue, move the rear index up and add the element to the rear
       //location in the queue:
       rear = (rear + 1) % queue.length;
       queue[__________] = newElement;

}
  
   public E dequeue()
   {
       if(isEmpty())
       {
           throw new NoSuchElementException("Queue is empty");
       }  
          
           front = (front + 1) % queue.length;
           E frontElement = queue[front];  
           queue[front] = null;
           return frontElement;

   }  
          
   private void expand()
   {
  
       E[] newQueue = (E[]) new Object[(queue.length * 3) / 2 + 1];
  
       int newIndex = 1;
       while (!this.isEmpty())
       {
           // Dequeue all of the current elements
           //add them to the new array beginning at index 1
       newQueue[__________] = this.dequeue();
       newIndex++;
         
}  
       //Set queue to reference the new array and update the front and rear pointers.
       queue = newQueue;
       front = _____;
       rear = newIndex - 1;
   }
  
   }
public class QueueTest
{
   public static void main(String[] args)
{
CircularQueue<String> q = new CircularQueue<String>(4);
q.enqueue("Red");
q.enqueue("Yellow");
q.enqueue("Green");
q.enqueue("Blue");
q.enqueue("Purple");
q.enqueue("Orange");
q.enqueue("White");
System.out.println(q.dequeue());
System.out.println(q.dequeue());
q.enqueue("Grey");

///Create a for loop that removes and prints all elements from the queue
while( )
{

}
}
}          
          
          
          
  

Solutions

Expert Solution

import java.util.NoSuchElementException;

public class CircularQueue<E>

{

    private E[] queue;

    private int front = 0, rear = 0;

    private static final int DEFAULT_CAPACITY = 5;

    public CircularQueue(int capacity)

    {

     queue = (E[]) new Object[capacity + 1];

    }

   

    public CircularQueue()

    {

        this(DEFAULT_CAPACITY);

    }

    //Add a method that will determine if the queue is empty. Recall that the queue is empty if the front and

//rear index the same location in the array:

    public boolean isEmpty()

    {

         return rear = null;

    }

    private boolean isFull()

    {

         return ((rear + 1) % queue.length) == front;

    }

   

    public void enqueue(E newElement)

    {

       

        if (isFull())

        {

       

        this.expand();

       

    }

        //To add an element to the rear of the queue, move the rear index up and add the element to the rear

        //location in the queue:

        rear = (rear + 1) % queue.length;

         queue[rear] = newElement;

}

   

    public E dequeue()

    {

        if(isEmpty())

        {

            throw new NoSuchElementException("Queue is empty");

        }   

           

             front = (front + 1) % queue.length;

             E frontElement = queue[front];   

             queue[front] = null;

             return frontElement;

    }   

           

    private void expand()

    {

   

        E[] newQueue = (E[]) new Object[(queue.length * 3) / 2 + 1];

   

        int newIndex = 1;

         while (!this.isEmpty())

         {

             // Dequeue all of the current elements

             //add them to the new array beginning at index 1

         newQueue[rear] = this.dequeue();

         newIndex++;

        

}   

         //Set queue to reference the new array and update the front and rear pointers.

         queue = newQueue;

         front = rear;

         rear = newIndex - 1;

    }

   

    }

public class QueueTest

{

    public static void main(String[] args)

   {

   CircularQueue<String> q = new CircularQueue<String>(4);

   q.enqueue("Red");

   q.enqueue("Yellow");

   q.enqueue("Green");

   q.enqueue("Blue");

   q.enqueue("Purple");

   q.enqueue("Orange");

   q.enqueue("White");

   System.out.println(q.dequeue());

   System.out.println(q.dequeue());

   q.enqueue("Grey");

///Create a for loop that removes and prints all elements from the queue

q.removeAt(1);

for (int i = 0; i < q.count; i++)

    {

   Console.WriteLine(" - " + q[i]);

    }

}    

}      


Related Solutions

Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
The following code is included for the java programming problem: public class Bunny {        private...
The following code is included for the java programming problem: public class Bunny {        private int bunnyNum;        public Bunny(int i) {               bunnyNum = i;        }        public void hop() {               System.out.println("Bunny " + bunnyNum + " hops");        } } Create an ArrayList <????> with Bunny as the generic type. Use an index for-loop to build (use .add(….) ) the Bunny ArrayList. From the output below, you need to have 5. Use an index for-loop...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");   ...
Write the following Java code into Pseudocode import java.util.*; public class Main { // Searching module...
Write the following Java code into Pseudocode import java.util.*; public class Main { // Searching module public static void score_search(int s,int score[]) { // Initialise flag as 0 int flag=0; // Looping till the end of the array for(int j=0;j<10;j++) { // If the element is found in the array if(s==score[j]) { // Update flag to 1 flag=1; } } // In case flag is 1 element is found if(flag==1) { System.out.println("golf score found"); } // // In case flag...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); HashMap labs = new HashMap(); while (true) { System.out.println("Choose operation : "); System.out.println("1. Create a Lab"); System.out.println("2. Modify a Lab"); System.out.println("3. Delete a Lab"); System.out.println("4. Assign a pc to a Lab"); System.out.println("5. Remove a pc from a Lab"); System.out.println("6. Quit"); int choice = sc.nextInt(); String name=sc.nextLine(); switch (choice) { case 1:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT