import java.util.Arrays; import java.util.stream.Collectors; public class PQ3<E>
ID: 3728797 • Letter: I
Question
import java.util.Arrays;
import java.util.stream.Collectors;
public class PQ3<E> {
private E[] queue;
private int added = 0;
private int removed = 0;
/**
* initializes objects of type PQ3. This should instantiate the queue field.
* @param capacity the maximum size of the queue
*/
public PQ3(int capacity){
}
/**
* @return the queue
*/
public E[] getQueue(){
return queue;
}
/**
* Sets the queue so that we can decouple testing for the add and remove operations
* @param items items for the array
*/
public void setQueue(E[] items){
queue = items;
added=items.length;
removed = 0;
}
/**
* A method to help with testing
* @param items a list of the items to print
* @return returns a String that is similar to Arrays.toString but fills in "null" if there is a null.
*/
public static String formatOutput(Object[] items){
if (items == null) return "null";
return String.format("[%s] ",
String.join(", ",
Arrays.stream(items).map(o -> ((o != null) ? o.toString() : "null")).collect(Collectors.toList())));
}
/**
* @param capacity the size of the array
* @return a generic array of size capacity
*/
public E[] newArray(int capacity){
@SuppressWarnings("unchecked")
E[] arr = (E[]) new Object[capacity];
return arr;
}
/**
* If the number of items to remove is larger than the current size of the queue,
* return null. Otherwise remove and return n (inclusive) items from the queue.
* @param n the number of elements to remove
* @return the list of items removed in the order they were removed, from front
* to the back.
*/
public E[] removeNItems(int n){
// YOUR CODE HERE
return null;
}
/**
* If the number of items to be added + the current elements would exceed the capacity of the queue, return false.
* Otherwise add the items to the queue from the first element to the last element and return true.
* @param items the items to be added to the queue
* @return true if added successfully, false otherwise.
*/
public boolean addNItems(E[] items){
// YOUR CODE HERE
return false;
}
/**
* @return the size of the queue
*/
public int size(){
return added - removed;
}
public static void main(String [] args){
// testing the constructor
PQ3<Integer> pq3 = new PQ3<>(10);
System.out.print("Testing the constructor with a capacity of 10: " + formatOutput(pq3.getQueue()));
System.out.println("Size of queue after operation: " + pq3.size());
// testing the newArray method
System.out.println("Testing the newArray method with a capacity of 5: " + formatOutput(pq3.newArray(5)));
System.out.println("Trying to add 5 items");
Integer[] addFive = {1, 2, 3, 4, 5};
System.out.println("Able to add 5 items-> " + pq3.addNItems(addFive));
System.out.println("Size of queue after operation: " + pq3.size());
System.out.println("Array after adding 5 ints: " + formatOutput(pq3.getQueue()));
System.out.println("Trying to remove 4 items");
Object[] fourItemsRemoved = pq3.removeNItems(4);
System.out.print("Items removed: " + formatOutput(fourItemsRemoved));
System.out.println("Size of queue after operation: " + pq3.size());
System.out.println("Array after trying to remove four items: " + formatOutput(pq3.getQueue()));
}
}
Please do not import any more java.utils and change any of the methods that have already been written thank you!
Instructions Use the javadoc to complete the following methods using a circular array queue 1. public P03(int capacity) (10 points) 2. public E[] newArray(int capacity) (10 points) 3. public E[] removeNItems (int n) (35 points) 4. public boolean addNItems(E[] items) (35 points) 5. public int size) (10 points)Explanation / Answer
import java.util.Arrays;
import java.util.stream.Collectors;
public class PQ3<E> {
private E[] queue;
private int added = 0;
private int removed = 0;
/**
* initializes objects of type PQ3. This should instantiate the queue field.
* @param capacity the maximum size of the queue
*/
public PQ3(int capacity) {
queue = (E[]) new Object[capacity];
}
/**
* @return the queue
*/
public E[] getQueue() {
return queue;
}
/**
* Sets the queue so that we can decouple testing for the add and remove operations
* @param items items for the array
*/
public void setQueue(E[] items) {
queue = items;
added=items.length;
removed = 0;
}
/**
* A method to help with testing
* @param items a list of the items to print
* @return returns a String that is similar to Arrays.toString but fills in "null" if there is a null.
*/
public static String formatOutput(Object[] items) {
if (items == null) return "null";
return String.format("[%s] ", String.join(", ",
Arrays.stream(items).map(o -> ((o != null) ? o.toString() : "null")).collect(Collectors.toList())));
}
/**
* @param capacity the size of the array
* @return a generic array of size capacity
*/
public E[] newArray(int capacity) {
@SuppressWarnings("unchecked")
E[] arr = (E[]) new Object[capacity];
return arr;
}
/**
* If the number of items to remove is larger than the current size of the queue,
* return null. Otherwise remove and return n (inclusive) items from the queue.
* @param n the number of elements to remove
* @return the list of items removed in the order they were removed, from front
* to the back.
*/
public E[] removeNItems(int n){
// YOUR CODE HERE
if(n > added) return null;
for (int i=0; i<n; i++) {
queue[i] = null;
removed++;
}
return queue;
}
/**
* If the number of items to be added + the current elements would exceed the capacity of the queue, return false.
* Otherwise add the items to the queue from the first element to the last element and return true.
* @param items the items to be added to the queue
* @return true if added successfully, false otherwise.
*/
public boolean addNItems(E[] items){
// YOUR CODE HERE
if(this.added+items.length > queue.length) return false;
for (Object e : items){
queue[added++] = (E)e;
}
return true;
}
/**
* @return the size of the queue
*/
public int size(){
return added - removed;
}
public static void main(String [] args){
// testing the constructor
PQ3<Integer> pq3 = new PQ3<>(10);
System.out.print("Testing the constructor with a capacity of 10: " + formatOutput(pq3.getQueue()));
System.out.println("Size of queue after operation: " + pq3.size());
// testing the newArray method
System.out.println("Testing the newArray method with a capacity of 5: " + formatOutput(pq3.newArray(5)));
System.out.println("Trying to add 5 items");
Integer[] addFive = {1, 2, 3, 4, 5};
System.out.println("Able to add 5 items-> " + pq3.addNItems(addFive));
System.out.println("Size of queue after operation: " + pq3.size());
System.out.println("Array after adding 5 ints: " + formatOutput(pq3.getQueue()));
System.out.println("Trying to remove 4 items");
Object[] fourItemsRemoved = pq3.removeNItems(4);
System.out.print("Items removed: " + formatOutput(fourItemsRemoved));
System.out.println("Size of queue after operation: " + pq3.size());
System.out.println("Array after trying to remove four items: " + formatOutput(pq3.getQueue()));
}
}
---------------------------------------------------
OUTPUT
_____________________________________
Note: PQ3.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Testing the constructor with a capacity of 10: [null, null, null, null, null, null, null, null, null, null]
Size of queue after operation: 0
Testing the newArray method with a capacity of 5: [null, null, null, null, null]
Trying to add 5 items
Able to add 5 items-> true
Size of queue after operation: 5
Array after adding 5 ints: [1, 2, 3, 4, 5, null, null, null, null, null]
Trying to remove 4 items
Items removed: [null, null, null, null, 5, null, null, null, null, null]
Size of queue after operation: 1
Array after trying to remove four items: [null, null, null, null, 5, null, null, null, null, null]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.