I need help with the following question. it needs to be in Java language. The method should check thst size() > 0 and return data[rear].
write a new assessor method that returns a copy of the item at the rear of the queue. Use the array version of a queue.
I nees an asscessor method that that returns a copy of the item at the rear of the queue. It needs to use the array version of a queue.
Tge body of the method should check that size() >0 then return data[rear]. the pictures were of the generic class arrayqueue.
Te trimtoSize method has three cases that are similar to the ensure- All the methods and their specifications are given in Figure 79. CITY method Specification and implementation of the Array Version of the Generic Gure Clans FIGURE 7.9 Generic Class ArrayQueue public class Array Queue
from the package edu.colorado.collections An ArrayQueue is a queue of references to E objects. Limitations: capacity is limited by the amount of free memory on the machine. The constructors, Clone, (1) The capacity of one of these queues can change after it's created, but the maximum ensureCapacity, add, and trimtosize will result in an OutOfMemoryError when free (2) A queue's capacity cannot exceed the largest integer, 2.147,483,647 Integer .MAX_VALUE). Any attempt to create a larger capacity results in failure due to an memory is exhausted. arithmetic overflow. (continue 388 Chapter 7/Qw (FIGURE 7.9 continued) Specification Constructor for the Array Queue public ArrayQueue() Initialize an empty queue with an initial capacity of 10. Note that the add method works efficiently (without needing more memory) until this capacity is reached. Postcondition: This queue is empty and has an initial capacity of 10. Throws: OutOfMemoryError Indicates insufficient memory for new Object[10]. Second Constructor for the Array Queue Array Queue (int initia capacity efficiently (memory) until this capacity is initial Capacity - the initial capacity of this queue initialCapacity is non-negative. Initialize an empty queue with a specified initial capacity. Note that the add method works Parameter: Precondition: Postcondition: This queue is empty and has the given initial capacity. Throws: 111egalArgumentException Indicates that initialCapacity is negative. Throws: OutOfMemoryError Indicates insufficient memory for new Object[initial Capacity]. add-IsEmpty-remove-size public void add(E) public boolean isEmpty() public E remove() public int size These are the standard queue specifications from Figure 7.2 on page 363. clone public ArrayQueue clone) Generate a copy of this queue. Returns: The retum value is a copy of this queue. Subsequent changes to the copy will not affect de original, nor vice versa. Throws: OutOfMemoryError Indicates insufficient memory for creating the clone. Implementation of the Class 389 FERRE 79 continued) easureCapacity Parameter: public void ensureCapacity (int minimun Capacity) Change the current capacity of this queue minimum Capacity - the new capacity for this queue This queue's capacity has been changed to at least in muncapacity. If the capacity was already at or greater than minimumCapacity, then the capacity is left unchanged. Indicates insufficient memory for new Object(minimum Capacity). Postcondition: Throws: OutOfMemoryError gerCapacity public int getCapacity) Accessor method to determine the current capacity of this queue. The add method works efficiently (without needing more memory) until this capacity is reached Returns: the current capacity of this queue crime To Size public void trimtoSize() Reduce the current capacity of this queue to its actual size (i.e., the number of items it contains). This queue's capacity has been changed to its current size. Throws: OutOfMemoryError Indicates insufficient memory for altering the capacity. Postcondition: Implementation File:ArrayQueue.java from the package edu.colorado.collections 1 Complete documentation is avallable on pages 387-389 or from the ArrayQueue link at 1/ http://www.cs.colorado.edu/-main/docs/ package edu.colorado.collections; import java.util.NoSuchElementException; public class ArrayQueue implements Cloneable { // Invariant of the ArrayQueue class: // 1. The number of items in the queue is in the instance variable manyltems. // 2. For a nonempty queue, the items are stored in a circular array beginning at // dataffront) and continuing through datafrear). // 3. For an empty queue, many/tems is zero and data is a reference to an array, but // we don't care about front and rear. private object[] data; private int many Items; private int front; (cond private int rear: ed 390 Chapter 7/Queues (FIGURE 7.9 continued) public ArrayQueue { final int INITIAL CAPACITY - 10: many Items = 0; // We don't care about front and rear for an empty queue. data = new Object (INITIAL_CAPACITY); } public ArrayQueue (int initial capacity { if (initialCapacity clone { // Clone an ArrayQueue. ArrayQueue answer: try { answer - (ArrayQueue) super.clone(): catch (CloneNot SupportedException e) // This exception should not occur. But if it does, it would probably indicate a Il programming error that made super clone unavailable. // The most common error would be forgetting the "Implements Cloneable // clause at the start of this class. throw new RuntimeException ("This class does not implement Cloneable."); } answer.data = data.clone(): return answer: } public int getCapacity { return data.length; } public boolean isEmpty { return (manyItems ) 0); private int nextIndex(int i) { See the answer to Self-Test Exercise 17. } lo W R 1937/ FIGURE (FIGURE 79 continued pub PortonCapacity > ) Object[] biggerArray: int nl, n2 if (data. Tength > minimum capacity) // No change needed. return; else if (many Items - 0) data-new Object[minimumCapacity]: else if (front crear) Create larger array and copy datalfront) .data(rear) into it. ]: data - biggerArray: 2/ Just increase the size of the array because the queue is empty. Systen, arraycopy (data, front, biggerArray, front, many items); Linked A queus the fron erences last noc 3 { // Create a bigger array, but be careful about copying items into it. The queur else // occur in two segments. The first segment goes from datalfront to the end // array, and the second segment goes from data[0] to data[rear. The varios // and n2 will be set to the number of items in these two segments. We w // these to biggerArray[o... manyltems-1). biggerArray = new Object[minimumCapacity]: nl - data. length - front; n2 = rear + 1; System.arraycopy (data, front, biggerArray, 0, nl); System arraycopy (data, o, biggerArray, nl, n2): front = 0; rear - manyItems-1; biggerArray: Figur queue is the data } 3 eSuppresswarnings ("unchecked") // See the warnings discussion on page 26 public E remove { E answer: if (many Items 0) throw new NoSuchelementException("Queue underflow."); answer = (E) data(front); front = nextIndex(front); manyItem --; return answer; } PrtScr Insert Delete TE Poup Poon Home End + L Backspace Num Lock * i 7 CO 9 Enter 4 5 6 Implementations of the Queue Clus FIGURE 7.9 continued) public int size return many Items: 1 public void trimtoSize(). || See the answer to Self-Test Exercise 18 1