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++)...
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;   ...
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:...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT