Question

Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQ...

Complete the code below, test cases to use are at the bottom

package Labs;

public class ResizingQueue {

private final int INITIAL_SIZE = 10;  

private int [] A = new int[INITIAL_SIZE]; // array to hold queue: front is always at Q[0]

// and rear at A[next-1]

int next = 0; // location of next available unused slot

// interface methods

public void enqueue(int key) {

//TODO: push the key onto the back of the queue

// YOUR CODE HERE!

}

private void resize() {

// this method resizes an array A to twice as big:

// TODO: create a new array B twice as big as A, transfer everything from A over to the same slots

// in B (i.e. A[0] goes to B[0], A[1] goes to B[1] ect)

// YOUR CODE HERE!

}

public int dequeue() {

// TODO: remove the top integer and return it -- return 'Integer.MIN_VALUE' if queue is empty

//YOUR CODE HERE!

}

public boolean isEmpty() {

return (next == 0);

}

public int size() {

// TODO: how many integers in the queue

return next;

}

public String toString() {

String s = "[";

for(int i = 0; i < A.length; ++i) {

if(i == next)

s += " | " + A[i];

else if(i == 0)

s += A[i];

else

s += ", " + A[i];

}

s += "]";

return s;

}

public static void main(String[] args) {

}

}

Output of test cases should be the following:

public class lab8 {

public static void main(String[] args) {

ResizingQueue Q = new ResizingQueue();

System.out.println("\nTesting toString on empty queue....");

System.out.println("\n[1] Should be:\n[ | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\tnext = 0");

System.out.println(Q + "\tnext = 0");

System.out.println("\nTesting size and isEmpty() on empty queue....");

System.out.println("\n[2] Should be:\n0\ttrue");

System.out.println(Q.size() + "\t" + Q.isEmpty());

// UNCOMMENT THIS CODE TO TEST YOUR FUNCTIONS ENQUEUE, DEQUEUE, and RESIZE

/*

System.out.println("\nTesting enqueue...");

Q.enqueue(3);

Q.enqueue(5);

Q.enqueue(7);

System.out.println("\n[3] Should be:\n[3, 5, 7 | 0, 0, 0, 0, 0, 0, 0]\tnext = 3");

System.out.println(Q + "\tnext = " + Q.next);

System.out.println("\nTesting size and isEmpty on non-empty queue....");

System.out.println("\n[4] Should be:\n3\tfalse");

System.out.println(Q.size() + "\t" + Q.isEmpty());

System.out.println("\nTesting dequeue...");

int n = Q.dequeue();

System.out.println("\n[5] Should be:\n[5, 7 | 7, 0, 0, 0, 0, 0, 0, 0]\tnext = 2\tQ.dequeue() => 3");

System.out.println(Q + "\tnext = " + Q.next + "\tQ.dequeue() => " + n);

n = Q.dequeue();

n = Q.dequeue();

System.out.println("\n[6] Should be:\n[ | 7, 7, 7, 0, 0, 0, 0, 0, 0, 0]\tnext = 0\tQ.dequeue() => 7");

System.out.println(Q + "\tnext = " + Q.next + "\tQ.dequeue() => " + n);

System.out.println("\nTesting resizing...");

for(int i = 10; i <= 25; ++i)

Q.enqueue(i);

System.out.println("\n[7] Should be:\n[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 | 0, 0, 0, 0]");

System.out.println(Q);

for(int i = 0; i < 13; ++i)

Q.dequeue();

System.out.println("\n[8] Should be:\n[23, 24, 25 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 0, 0, 0, 0]");

System.out.println(Q);

*/

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//package Labs;

public class ResizingQueue {

private final int INITIAL_SIZE = 10;

private int [] A = new int[INITIAL_SIZE]; // array to hold queue: front is always at Q[0]

// and rear at A[next-1]

int next = 0; // location of next available unused slot
private int CAPACITY = INITIAL_SIZE;
// interface methods

public void enqueue(int key) {

//TODO: push the key onto the back of the queue

// YOUR CODE HERE!
  
  
   if(this.next < CAPACITY)
   {
//A[next++] = key;
   }
   else
   {
       resize();
      
   }
   A[next++] = key;
}

private void resize() {

// this method resizes an array A to twice as big:

// TODO: create a new array B twice as big as A, transfer everything from A over to the same slots

// in B (i.e. A[0] goes to B[0], A[1] goes to B[1] ect)

// YOUR CODE HERE!
   int NEW_INITIAL_SIZE = CAPACITY *2;
if(A.length > 0)
{
   int [] B = new int[NEW_INITIAL_SIZE];
  
   int [] temp = new int[NEW_INITIAL_SIZE];
   for(int i = 0;i < A.length; i++)
   {
       temp[i] = A[i];
   }
   A = null;
   A = new int[NEW_INITIAL_SIZE];
   for(int i = 0;i < temp.length; i++)
   {
       A[i] = temp[i];
   }
   CAPACITY = NEW_INITIAL_SIZE;
}
/*else
{
  
}
*/
}


public int dequeue() {

// TODO: remove the top integer and return it -- return 'Integer.MIN_VALUE' if queue is empty

//YOUR CODE HERE!
   int [] B = new int[A.length];
   int val = 0;
  
   if(next > 0)
   {
       //int [] B = new int[A.length-1];
       //val = A[next-1];
       val = A[0];
       for(int j = 0, k = 0;j < B.length && k<=this.next -2; j++,k++)
       {
           B[j] = A[k+1];
       }
       A = null;
       A = B;
       this.next--;
   }
   else
   {
       val = Integer.MIN_VALUE;
   }
  
return val;
}

public boolean isEmpty() {

return (next == 0);

}

public int size() {

// TODO: how many integers in the queue

return next;
  

}

public String toString() {

String s = "[";

for(int i = 0; i < A.length; ++i) {

if(i == next)

s += " | " + A[i];

else if(i == 0)

s += A[i];

else

s += ", " + A[i];

}

s += "]";

return s;

}
/*
public static void main(String[] args) {

}
*/
}

public class lab8 {

public static void main(String[] args) {

ResizingQueue Q = new ResizingQueue();

System.out.println("\nTesting toString on empty queue....");

System.out.println("\n[1] Should be:\n[ | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\tnext = 0");

System.out.println(Q + "\tnext = 0");

System.out.println("\nTesting size and isEmpty() on empty queue....");

System.out.println("\n[2] Should be:\n0\ttrue");

System.out.println(Q.size() + "\t" + Q.isEmpty());

// UNCOMMENT THIS CODE TO TEST YOUR FUNCTIONS ENQUEUE, DEQUEUE, and RESIZE

System.out.println("\nTesting enqueue...");

Q.enqueue(3);

Q.enqueue(5);

Q.enqueue(7);

System.out.println("\n[3] Should be:\n[3, 5, 7 | 0, 0, 0, 0, 0, 0, 0]\tnext = 3");

System.out.println(Q + "\tnext = " + Q.next);

System.out.println("\nTesting size and isEmpty on non-empty queue....");

System.out.println("\n[4] Should be:\n3\tfalse");

System.out.println(Q.size() + "\t" + Q.isEmpty());

System.out.println("\nTesting dequeue...");

int n = Q.dequeue();

System.out.println("\n[5] Should be:\n[5, 7 | 7, 0, 0, 0, 0, 0, 0, 0]\tnext = 2\tQ.dequeue() => 3");

System.out.println(Q + "\tnext = " + Q.next + "\tQ.dequeue() => " + n);

n = Q.dequeue();

n = Q.dequeue();

System.out.println("\n[6] Should be:\n[ | 7, 7, 7, 0, 0, 0, 0, 0, 0, 0]\tnext = 0\tQ.dequeue() => 7");

System.out.println(Q + "\tnext = " + Q.next + "\tQ.dequeue() => " + n);

System.out.println("\nTesting resizing...");

for(int i = 10; i <= 25; ++i)

Q.enqueue(i);

System.out.println("\n[7] Should be:\n[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 | 0, 0, 0, 0]");

System.out.println(Q);

for(int i = 0; i < 13; ++i)

Q.dequeue();

System.out.println("\n[8] Should be:\n[23, 24, 25 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 0, 0, 0, 0]");

System.out.println(Q);

}
}

//Output of test cases should be the following:

Add a comment
Know the answer?
Add Answer to:
Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQ...
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
  • Design and implement a class Q that uses Q.java as a code base. The queue ADT...

    Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • lab 11 Do not change main.cpp, i need c++ code for queue.h and queue.cpp Given the...

    lab 11 Do not change main.cpp, i need c++ code for queue.h and queue.cpp Given the complete main() function, partial queue class header queue.h, and queue.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor enqueue() dequeue() You may elect to create the following helper functions: isFull() isEmpty() A description of these ADT operations are available in this Zybook and in the textbook's chapter 17. Example: If the input is: 3 Led Zepplin...

  • In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods a...

    In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods also listed below: Data Members: Declare and initialize, as needed, the data item(s) you will need to manage a queue of integers. You may only use arrays and primitives for your instance and/or static variables (I,e You can’t use Java defined Queue / Stack / List...

  • How do I pass values to this function? class DynIntQueue { struct QueueNode { int value;...

    How do I pass values to this function? class DynIntQueue { struct QueueNode { int value; QueueNode *next; QueueNode(int value1, QueueNode *next1 = nullptr) { value = value1; next = next1; } }; // These track the front and rear of the queue QueueNode *front; QueueNode *rear; public: // Constructor and Destructor DynIntQueue(); ~DynIntQueue(); // Member functions void enqueue(int); void dequeue(int &); bool isEmpty() const; void clear(); }; main #include <iostream> #include "DynIntQueue.h" using namespace std; int main() {DynIntQueue list;...

  • how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 }...

    how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...

  • public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private...

    public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private int tail; Private int count;   } public void enqueue(E item) { if(isFull()){ return; } count++; elements[tail] = item; tail = (tail + 1) % size; } public E dequeue() { if(isEmpty()) return null; int ct = count-1; E cur = elements[head]; int index = 0; for(i=1;ct-->0;i++) { if(cur.compareTo(elements[head+i)%size])<0) cur = elements[(head+i)%size]; index = i; } } return remove((head+index%size); public E remove(int index) { E...

  • JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test...

    JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please! MyQueue.java Implement a queue using the MyStack.java implementation as your data structure.  In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item): inserts item into the queue dequeue(): returns and deletes the first element in the queue isEmpty(): returns true or false...

  • ------------------------------------------------------------------------------------------------------------ CODE ALREADY HAVE BELOW--------------------------------------------------------------------------------------- public class LinkedQueue<T>

    ------------------------------------------------------------------------------------------------------------ CODE ALREADY HAVE BELOW--------------------------------------------------------------------------------------- public class LinkedQueue<T> implements QueueADT<T> {    private int count;    private LinearNode<T> head;    private LinearNode<T> tail;               public LinkedQueue()    {        count = 0;        head = null;        tail = null;    }    @Override    public void enqueue(T element)    {        LinearNode<T> node = new LinearNode<T> (element);        if(isEmpty())            head = node;        else           ...

  • The class pictured below is designed to implement an integer queue using two stacks. Assume the...

    The class pictured below is designed to implement an integer queue using two stacks. Assume the stack methods all work as desired (though their implementations are not shown). .(a) Trace what happens in the following situation, showing intermediate steps (values of variables, what is in the stacks, and what is in the queue at various points in the methods, not just the results of the methods). • Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue twice....

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
ADVERTISEMENT