Question

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 match the following. The already coded main() method should not need any modifications. {output file}

*** test enqueue() ***
1st...enqueued
2nd...enqueued
3rd...enqueued
4th...enqueued
5th...not enqueued
[1st, 2nd, 3rd, 4th]
...size: 4; capacity: 4; isFull(): true; isEmpty(): false

*** test front() and dequeue() ***
front(): 1st; dequeue(): 1st
front(): 2nd; dequeue(): 2nd
front(): 3rd; dequeue(): 3rd
front(): 4th; dequeue(): 4th
[]
...size: 0; capacity: 4; isFull(): false; isEmpty(): true

*** test front() when Q empty... passed ***

*** test clear() ***
before: [James, Gosling]
...size: 2; capacity: 4; isFull(): false; isEmpty(): false
after: []
...size: 0; capacity: 4; isFull(): false; isEmpty(): true

*** test split() odd size ***
before split(): [1, 2, 3, 4, 5, 6, 7]
...size: 7; capacity: 16; isFull(): false; isEmpty(): false
after split(): [1, 2, 3]
...size: 3; capacity: 16; isFull(): false; isEmpty(): false
new Q: [4, 5, 6, 7]
...size: 4; capacity: 16; isFull(): false; isEmpty(): false

*** test split() even size ***
before split(): [0, 1, 2, 3, 4, 5, 6, 7]
...size: 8; capacity: 16; isFull(): false; isEmpty(): false
after split(): [0, 1, 2, 3]
...size: 4; capacity: 16; isFull(): false; isEmpty(): false
new Q: [4, 5, 6, 7]
...size: 4; capacity: 16; isFull(): false; isEmpty(): false

*** test split() empty Q ***
before split(): []
...size: 0; capacity: 4; isFull(): false; isEmpty(): true
after split(): []
...size: 0; capacity: 4; isFull(): false; isEmpty(): true
new Q: []
...size: 0; capacity: 4; isFull(): false; isEmpty(): true

*** test split() size=1 Q ***
before split(): [foo]
...size: 1; capacity: 4; isFull(): false; isEmpty(): false
after split: [foo]
...size: 1; capacity: 4; isFull(): false; isEmpty(): false
new Q: []
...size: 0; capacity: 4; isFull(): false; isEmpty(): true

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

import java.util.LinkedList;

/**
 * class Q implements a Queue ADT using class LinkedList from 
 * the Java class library.
 
 */

public class Q {

   // all Q objects have the same default capacity...
   private final static int DFLT_CAPACITY = 4;

   // every Q object has the following instance variables...
   private LinkedList<Object> ll;
   private int capacity;

   /**
    * Constructs an empty Q having DFLT_CAPACITY.
    */
   public Q() {
      this(DFLT_CAPACITY);
   }

   /**
    * Constructs an empty Q having client supplied capacity.
    *
    * @param capacity switched to DFLT_CAPACITY if less than one
    */
   public Q(int c) {
      capacity = c < 1 ? DFLT_CAPACITY : c;
      ll = new LinkedList<Object>();
   }

   /**
    * TBI (To Be Implemented)
    *
    * Adds an object to the "end" of this Q.
    *
    * @param object to add 
    * @return true if queue content changed;
    *         false if param is null or queue is full
    */
   public boolean enqueue(Object o) {
   }

   /**
    * TBI (To Be Implemented)
    *
    * Removes and returns the "front" object from this Q.
    *
    * @return front of queue or null if queue is empty
    */
   public Object dequeue() {
   }

   /**
    * TBI (To Be Implemented)
    *
    * Returns, but does not remove, "front" object in this Q.
    *
    * @return front of queue or null if queue is empty
    */
   public Object front() {
   }

   /**
    * TBI (To Be Implemented)
    *
    * Returns number of objects in this Q.
    *
    * @return size of queue
    */
   public int size() {
   }

   /**
    * TBI (To Be Implemented)
    *
    * Returns the capacity of this Q.
    *
    * @return capacity of queue
    */
   public int capacity() {
   }

   /**
    * TBI (To Be Implemented)
    *
    * Q is full if size equals capacity.
    *
    * @return true if full
    */
   public boolean isFull() {
   }

   /**
    * TBI (To Be Implemented)
    *
    * Q is empty if size is zero.
    *
    * @return true if empty
    */
   public boolean isEmpty() {
   }

   /**
    * TBI (To Be Implemented)
    *
    * Removes all of the objects from this Q. 
    */
   public void clear() {
   }

   /**
    * Construct a string representation of this Q.
    *
    * @return string representation of this queue
    */
   public String toString() {
      return ll.toString() + "\n...size: " + ll.size() + 
             "; capacity: " + capacity + "; isFull(): " + isFull() +
             "; isEmpty(): " + isEmpty();
   }

   /**
    * TBI (To Be Implemented)
    *
    * Constructs and returns a new Q that contains that the second
    * "half" of this Q. New Q capacity equals this Q capacity. In
    * addition, the last half of this Q is removed.
    * 
    * Examples...
    *
    * this Q before split:  1, 3, 4, 8
    *  this Q after split:  1, 3
    *               new Q:  4, 8
    *
    * this Q before split:  1, 3, 4, 8, 14
    *  this Q after split:  1, 3
    *               new Q:  4, 8, 14
    *
    * this Q before split:  13
    *  this Q after split:  13
    *               new Q:  empty (i.e. size is 0)
    *
    * @return new Q object
    */
   public Q split() {
   }
         

   /**
    * The main() method is used to test class Q.
    */
   public static void main(String[] argv) {
      String[] items = { "1st", "2nd", "3rd", "4th", "5th", };
      Q q = new Q();
      System.out.println("*** test enqueue() ***");
      for (int i = 0; i < items.length; i++) {
         System.out.print(items[i] + "...");
         if (!q.enqueue(items[i]))
            System.out.print("not ");
         System.out.println("enqueued");
      }
      System.out.println(q);
      System.out.println("\n*** test front() and dequeue() ***");
      for (int i = 0, j = q.size(); i < j; i++) {
         System.out.print("front(): " + q.front());
         System.out.println("; dequeue(): " + q.dequeue());
      }
      System.out.println(q);
      if (q.front() == null)
         System.out.println("\n*** test front() when Q empty... passed ***");
      q.enqueue("James");
      q.enqueue("Gosling");
      System.out.println("\n*** test clear() ***");
      System.out.println("before: " + q);
      q.clear();
      System.out.println("after: " + q);
      System.out.println("\n*** test split() odd size ***");
      test_split(0);
      System.out.println("\n*** test split() even size ***");
      test_split(1);
      q = new Q();
      System.out.println("\n*** test split() empty Q ***");
      System.out.println("before split(): " + q);
      Q n = q.split();
      System.out.println("after split(): " + q);
      System.out.println("new Q: " + n);
      q = new Q();
      q.enqueue("foo");
      System.out.println("\n*** test split() size=1 Q ***");
      System.out.println("before split(): " + q);
      n = q.split();
      System.out.println("after split: " + q);
      System.out.println("new Q: " + n);

   }

   private static void test_split(int x) {
      final int NEW_Q = 8;
      Q q = new Q(NEW_Q * 2);
      for (int i = 1 - x; i < NEW_Q; i++)
         q.enqueue(new Integer(i));
      System.out.println("before split(): " + q);
      Q newq = q.split();
      System.out.println("after split(): " + q);
      System.out.println("new Q: " + newq);
   }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.LinkedList;

/**

* class Q implements a Queue ADT using class LinkedList from

* the Java class library.

*/

public class Q {

   // all Q objects have the same default capacity...

   private final static int DFLT_CAPACITY = 4;

   // every Q object has the following instance variables...

   private LinkedList<Object> ll;

   private int capacity;

   /**

* Constructs an empty Q having DFLT_CAPACITY.

*/

   public Q() {

this(DFLT_CAPACITY);

   }

   /**

* Constructs an empty Q having client supplied capacity.

*

* @param capacity switched to DFLT_CAPACITY if less than one

*/

   public Q(int c) {

capacity = c < 1 ? DFLT_CAPACITY : c;

ll = new LinkedList<Object>();

   }

   /**

* TBI (To Be Implemented)

*

* Adds an object to the "end" of this Q.

*

* @param object to add

* @return true if queue content changed;

* false if param is null or queue is full

*/

   public boolean enqueue(Object o) {

if(o==null || ll.size()==capacity)

return false;

ll.addLast(o);

return true;

   }

   /**

* TBI (To Be Implemented)

*

* Removes and returns the "front" object from this Q.

*

* @return front of queue or null if queue is empty

*/

   public Object dequeue() {

if(ll.size()==0)

return null;

return ll.removeFirst();

   }

   /**

* TBI (To Be Implemented)

*

* Returns, but does not remove, "front" object in this Q.

*

* @return front of queue or null if queue is empty

*/

   public Object front() {

if(ll.size()==0)

return null;

return ll.get(0);

   }

   /**

* TBI (To Be Implemented)

*

* Returns number of objects in this Q.

*

* @return size of queue

*/

   public int size() {

return ll.size();

   }

   /**

* TBI (To Be Implemented)

*

* Returns the capacity of this Q.

*

* @return capacity of queue

*/

   public int capacity() {

return capacity;

   }

   /**

* TBI (To Be Implemented)

*

* Q is full if size equals capacity.

*

* @return true if full

*/

   public boolean isFull() {

return ll.size()==capacity;

   }

   /**

* TBI (To Be Implemented)

*

* Q is empty if size is zero.

*

* @return true if empty

*/

   public boolean isEmpty() {

return ll.size()==0;

   }

   /**

* TBI (To Be Implemented)

*

* Removes all of the objects from this Q.

*/

   public void clear() {

ll.clear();

   }

   /**

* Construct a string representation of this Q.

*

* @return string representation of this queue

*/

   public String toString() {

return ll.toString() + "\n...size: " + ll.size() +

   "; capacity: " + capacity + "; isFull(): " + isFull() +

   "; isEmpty(): " + isEmpty();

   }

   /**

* TBI (To Be Implemented)

*

* Constructs and returns a new Q that contains that the second

* "half" of this Q. New Q capacity equals this Q capacity. In

* addition, the last half of this Q is removed.

*

* Examples...

*

* this Q before split: 1, 3, 4, 8

* this Q after split: 1, 3

* new Q: 4, 8

*

* this Q before split: 1, 3, 4, 8, 14

* this Q after split: 1, 3

* new Q: 4, 8, 14

*

* this Q before split: 13

* this Q after split: 13

* new Q: empty (i.e. size is 0)

*

* @return new Q object

*/

   public Q split() {

Q ret = new Q(this.capacity());

if(ll.size()==0 || ll.size()==1)

return new Q(this.capacity());

else {

for(int i=ll.size()/2; i<ll.size();) {

ret.ll.addLast(ll.get(i));

ll.remove(i);

}

return ret;

}

   }

   /**

* The main() method is used to test class Q.

*/

   public static void main(String[] argv) {

String[] items = { "1st", "2nd", "3rd", "4th", "5th", };

Q q = new Q();

System.out.println("*** test enqueue() ***");

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

   System.out.print(items[i] + "...");

   if (!q.enqueue(items[i]))

System.out.print("not ");

   System.out.println("enqueued");

}

System.out.println(q);

System.out.println("\n*** test front() and dequeue() ***");

for (int i = 0, j = q.size(); i < j; i++) {

   System.out.print("front(): " + q.front());

   System.out.println("; dequeue(): " + q.dequeue());

}

System.out.println(q);

if (q.front() == null)

   System.out.println("\n*** test front() when Q empty... passed ***");

q.enqueue("James");

q.enqueue("Gosling");

System.out.println("\n*** test clear() ***");

System.out.println("before: " + q);

q.clear();

System.out.println("after: " + q);

System.out.println("\n*** test split() odd size ***");

test_split(0);

System.out.println("\n*** test split() even size ***");

test_split(1);

q = new Q();

System.out.println("\n*** test split() empty Q ***");

System.out.println("before split(): " + q);

Q n = q.split();

System.out.println("after split(): " + q);

System.out.println("new Q: " + n);

q = new Q();

q.enqueue("foo");

System.out.println("\n*** test split() size=1 Q ***");

System.out.println("before split(): " + q);

n = q.split();

System.out.println("after split: " + q);

System.out.println("new Q: " + n);

   }

   private static void test_split(int x) {

final int NEW_Q = 8;

Q q = new Q(NEW_Q * 2);

for (int i = 1 - x; i < NEW_Q; i++)

   q.enqueue(new Integer(i));

System.out.println("before split(): " + q);

Q newq = q.split();

System.out.println("after split(): " + q);

System.out.println("new Q: " + newq);

   }

}

============================================================
SEE OUTPUT


Thanks, PLEASE UPVOTE, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Design and implement a class Q that uses Q.java as a code base. The queue ADT...
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
  • 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! }...

  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • Create a program named NonZeroNumQueue.java then copy the contents from NonZeroNumQueue.txt. Then implement enqueue method. To...

    Create a program named NonZeroNumQueue.java then copy the contents from NonZeroNumQueue.txt. Then implement enqueue method. To get credit, DO NOT add or alter any data members/methods except the enqueue(int num). The NonZeroNumQueue will NOT accept zero, which mean you can enqueue any whole numbers EXCEPT 0(zero). You have already noticed that the queue is NEVER full, so the isFull() always returns false. given NonZeroNumQueue.txt public class NonZeroNumQueue{ private int[] data; private int total; private int front; private int tail; public...

  • // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations // ...

    // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations //   - (i.e. queue_t* create_queue(unsigned int _capacity) should not have additional arguments) // - You should not have any 'printf' statements in your queue functions. //   - (You may consider using these printf statements to debug, but they should be removed from your final version) // ==================================================...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class....

    CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class. A queue class is a First-In-First-Out data structure. To understand a queue, think of a checkout line at a grocery store: the person at the front is served first (removed), and people are added to the line from the back end. class queue : private unorderedArrayListType { public: bool isEmpty() const; // test whether queue is empty // Post: returns true if queue is...

  • JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity...

    JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...

  • in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...

    in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...

  • 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....

  • Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for...

    Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for the following ADT: 3) Queue ADT that uses an array internally (call it AQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! Make sure your classes implement the corresponding interfaces. Put your classes in a package called cse11. Try to make the code robust and try to use generics. The Queue ADT The Queue ADT...

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