Question
this is a data structures course
21. (20 points) We want to define a generic class ArrayQueue, which uses a circular array. public class Array Queue< II (1) L
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the answer below, all the details are mentioned as the comments.

Answer 1:

//to save the default capacity
private static final int DEFAULT_CAPACITY = 10;
//to store the elements of the queue
private T[] queue; 
// total number of elements currently in the queue
private int size,
// front element index
private int front, 
// back element index
private int back;        

Answer 2:

//constructor  
public ArrayQueue (int initialCapacity){
   //initialization of elements
   size = 0;
   front = 0;
   rear = 0;
//creation of the array for the initialCapacity passed
   queue = (T[])(new Object[initialCapacity]);
}

Answer 3:

public T remove(){
   //get the front element
   T e = getFront();
   //free the location
   A[front%size] = null;
   //increment the front
   front++;
   //decrease the size
   size--;
   //return the element
   return e;
}

Please let us know in the comments if you face any problems.

Add a comment
Know the answer?
Add Answer to:
this is a data structures course 21. (20 points) We want to define a generic class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ArrayQueue

    Implement the ArrayQueue classIn the ‘Queues’ lecture, review the ‘Introduce next lab’ section.  See here that we can use a circular array to implement the queue data structure.  You must write a class named ArrayQueue that does this.ArrayQueue will be a generic class, that implements our generic QueueInterface interface.  This demonstrates the Java interface feature, where we have already implemented queue dynamically, using the LinkedQueue class covered during the lecture.Many classes are given to youDownload and unzip the Circular array project from Canvas, ‘Queues’ module, Example programs.  Open the project in...

  • Write a Client class with a main method that tests the data structures as follows: For...

    Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue and LinkedQueue: Perform a timing test for each of these data structures. Each timing test should measure in nanoseconds how long it takes to add N Integers to the structure and how long it takes to remove N Integers from the structure. N should vary from 10 to 100,000,000 increasing N by a factor of 10 for each test. Depending...

  • Question 11 (20 points) Given the class Point below, define a class Circle that represent a...

    Question 11 (20 points) Given the class Point below, define a class Circle that represent a circle with a given center and radius. The circle class should have a center attribute named center as well as a floating point radius attribute. The center is a point object, defined by the class Point. The class should also have these members: the constructor of the class, which should take parameters to initialize all attributes - a getter for center a setter for...

  • Task 1: 1. Write a generic class named MyList, with a type parameter T. The type...

    Task 1: 1. Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of type T. Write the class constructor to create the ArrayList. 2. Write a public method named add, which accepts a parameter of type T. When an argument is passed to the method, add it to the ArrayList. 3. Write a public method...

  • Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an...

    Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an array of elements as a parameter and uses that array as the heap rather than creating a new array. Of course, the array passed in is probably not in proper heap ordering, so you must rearrange it until it is. There's a neat trick for achieving this: If you just "bubble down" all of the non-leaf nodes of the heap, starting from the last...

  • Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an...

    Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an array of elements as a parameter and uses that array as the heap rather than creating a new array. Of course, the array passed in is probably not in proper heap ordering, so you must rearrange it until it is. There's a neat trick for achieving this: If you just "bubble down" all of the non-leaf nodes of the heap, starting from the last...

  • Course Class Create a class called Course. It will not have a main method; it is...

    Course Class Create a class called Course. It will not have a main method; it is a business class. Put in your documentation comments at the top. Be sure to include your name. Course must have the following instance variables named as shown in the table: Variable name Description crn A unique number given each semester to a section (stands for Course Registration Number) subject A 4-letter abbreviation for the course (e.g., ITEC, PHED, CHEM) number A 4-digit number associated...

  • QUEUEBOX: Using an Array of initial size of five (5) for storage, start with the following...

    QUEUEBOX: Using an Array of initial size of five (5) for storage, start with the following generic class declaration for QueueBox: public class QueueBox<E> { private E[] elements = (ED))( new Object[5]); private int front_idx = 0; private int rear_idx = 0; private int count = 0; Hint: use the count variable to keep track of how many elements are in the queue (increment count when enqueing and decrement when dequeing). Makes it a lot easier to determine if the...

  • Consider the class as partially defined here: //class Pizzaorder class Pizzaorder // static public members public...

    Consider the class as partially defined here: //class Pizzaorder class Pizzaorder // static public members public static final int MAX TOPPINGS 20: // instance members private String toppings[]; private int numToppings; // constructor public PizzaOrder () { numToppings toppings 0; String[MAX TOPPINGS]; new // accessor tells # toppings on pizza, i.e., #toppings in array public int getNum Toppings ()return numToppings; // etc. We want to write an instance method, addTopping) that will take a String parameter, topping, and add it...

  • Need Help! Please Solve using JAVA! TopResult Task: There are a number of situations in which we ...

    Need Help! Please Solve using JAVA! TopResult Task: There are a number of situations in which we want to keep track of the highest value we've seen so far - a highest score, a most recent entry, a name which is closest to the end (or the front) of a list, etc. We can imagine writing a simple class which will do this: a class which will let us enter new results one by one, and at any point will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT