Home > front end >  I need help Returning a string representation of my circular queue
I need help Returning a string representation of my circular queue

Time:03-08

I am trying to returns a string representation of my circular queue heres what I have so far. (Im not allowed to use/import other libraries in my code)

public class A2KeyesOwenQueue {
    private int front;
    private int rear;
    private int numItems;
    private int[] myArray;

    public A2KeyesOwenQueue() {
        // the default size of the queue is 10
        myArray = new int[10];
        numItems = 0;
        front = 0;
        rear = 0;
    }

    // Adds one element to the rear of the queue. Returns true if successful, false
    // otherwise
    public boolean enqueue(int elements) {
        if (numItems < 10) {
            myArray[rear] = elements;
            rear = (rear   1) % myArray.length;
            numItems  ;
            return true;
        }
        else
            return false;
    }

    // Removes and returns the element at the front of the queue
    public int dequeue() {
        int temp;
        if (numItems == 0) {
            return -9999;
        }
        else
            temp = myArray[front];
        front = (front   1) % myArray.length;
        numItems--;
        return temp;
    }

    // Returns without removing the element at the front of the queue
    public int peek() {
        if (numItems == 0) {
            return -9999;
        }
        else
            return myArray[front];

    }

    // Returns true if the queue contains no elements
    public boolean isEmpty() {
        if (numItems == 0) {
            return true;
        }
        else
            return false;
    }

    // Returns the number of elements in the queue
    public int size() {
        return numItems;
    }

    // Returns a string representation of the queue
    public String toString() {
        String arr;
        arr = "[";
        if (numItems > 0) {
            for (int i = 0; i < 10 && i < numItems; i  ) {
                arr = arr   myArray[i]   ", ";
            }
            arr = arr   myArray[0]   "]";
        }
        else {
            arr = arr   "]";
        }
        return arr;
    }
}

My public String toString( ) doesn't return the right numbers on my test driver. I think it has something to do with it not circling the array.

public String toString( ){
String arr;
arr = "[";
if (numItems > 0){
  for (int i = 0; i < 10 && i < numItems ;i  ) {
   
    arr = arr   myArray[i] ", ";
  }

  arr = arr   myArray[0]   "]";
}
else{
  arr = arr   "]";
 }return arr;
}

And my driver:

/*
 * This class performs test cases for queue
 * 
 * */
class DriverTest2{

    public static void main(String[] args) { 
        int myMark = 0;
        // TEST 1 
        // (1 mark) A queue is empty on construction
        A2KeyesOwenQueue  q = new A2KeyesOwenQueue();
        if (q.isEmpty()) {
            System.out.println("\nTest 1 successfull:  1");
            myMark=myMark   1;
        }
        else {
            System.out.println("\nTest 1 failed********************");
        }
        // TEST 2
        // (1 mark) A queue has size 0 on construction
        if (q.size()==0) {
            System.out.println("\nTest 2 successfull:  1");
            myMark=myMark   1;
        }
        else {
            System.out.println("\nTest 2 failed********************");
        }
        // TEST 3
        // (3 mark)After n enqueues to an empty queue, n > 0,
        // the queue is not empty and its size is n
        q = new A2KeyesOwenQueue (); // reset queue
        for (int i = 100; i < 150; i =10) {
            System.out.print ("enqueuing "   i);
            if (q.enqueue(i)) {
                System.out.print("... success");
            }
            else {
                System.out.print("... failed");
            }
            System.out.println ();
        }
        if (!q.isEmpty() && q.size()==5) {
            System.out.println("\nTest 3 successfull:  3");
            myMark=myMark   3;
        }
        else {
            System.out.println("\nTest 3 failed********************");
        }
        // TEST 4
        // (2 mark) If one enqueues x then peeks, the value returned is x, but the size stays the same
        q = new A2KeyesOwenQueue (); // reset queue
        // insert 5 elements 1000 900 800 700 600
        for (int i = 1000; i > 500; i-=100) {
            System.out.print ("Trying to enqueue :"   i);
            if (q.enqueue(i)) {
                System.out.print("... success");
            }
            else {
                System.out.print("... failed");
            }
            System.out.println();
        }
        q.enqueue(100);
        int myPeek = q.peek();
        if (q.size()==6) {
            System.out.println("\nTest 4 part 1 successfull:  1");
            myMark=myMark   1;
        }
        else {
            System.out.println("\nTest 4 part 1 failed********************");
        }
        if (myPeek == 1000) {
            System.out.println("\nTest 4 part 2 successfull:  1");
            myMark = myMark   1;
        }
        else {
            System.out.println("\nTest 4 part 2 failed********************");
        }

        // TEST 5
        // If one enqueues x then dequeues, the value dequeued
        // is x.
        q = new A2KeyesOwenQueue(); // reset queue
        q.enqueue(3);
        int temp = q.dequeue();
        if (temp == 3) {
            System.out.println("\nTest 5 successfull:  1");
            myMark = myMark   1;
        }
        else {
            System.out.println("\nTest 5 failed********************");
        }
        // TEST 6
        // (2 mark) If the size is n, then after
        // n dequeues, the stack is empty and has a size 0
        // reset the queue
        q = new A2KeyesOwenQueue();
        // insert 5 elements 1000 900 800 700 600
        for (int i = 1000; i > 500; i -= 100) {

            System.out.print("Trying to enqueue :"   i);
            if (q.enqueue(i)) {
                System.out.print("... success");
            }
            else {
                System.out.print("... failed");
            }
            System.out.println();
        }
        for (int i = 0; i < 5; i  ) {
            q.dequeue();
        }
        if (q.size() == 0) {
            System.out.println("\nTest 6 successfull:  2");
            myMark = myMark   2;
        }
        else {
            System.out.println("\nTest 6 failed********************");
        }
        // TEST 7
        // (1 mark) call peek() with one item
        // reset the queue
        q = new A2KeyesOwenQueue();
        q.enqueue(50);
        int temp7 = q.peek();
        if (temp7 == 50) {
            System.out.println("\nTest 7 successfull:  1");
            myMark = myMark   1;
        }
        else {
            System.out.println("\nTest 7 failed********************");
        }

        // TEST 8
        // (1 mark) call peek() from an empty queue
        // returns -9999 representing no such element
        q = new A2KeyesOwenQueue();
        int temp8 = q.peek();
        if (temp8 == -9999) {
            System.out.println("\nTest 8 successfull:  1");
            myMark = myMark   1;
        }
        else {
            System.out.println("\nTest 8 failed********************");
        }
        // TEST 9
        // (1 mark) call dequeue() from an empty queue
        // returns -9999 representing no such element
        q = new A2KeyesOwenQueue();
        int temp9 = q.dequeue();
        if (temp9 == -9999) {
            System.out.println("\nTest 9 successfull:  1");
            myMark = myMark   1;
        }
        else {
            System.out.println("\nTest 9 failed********************");
        }
        // TEST 10
        // (1 mark) call dequeue() on an queue with one item.
        q = new A2KeyesOwenQueue();
        q.enqueue(50);
        int temp10 = q.dequeue();
        if (temp10 == 50) {
            System.out.println("\nTest 10 successfull:  1");
            myMark = myMark   1;
        }
        else {
            System.out.println("\nTest 10 failed********************");
        }
        q = new A2KeyesOwenQueue(); // reset queue
        // TEST 11 enqueue elements until full plus one more element
        boolean t11 = false;
        for (int i = 100; i < 200; i  = 10) {

            System.out.print("\nTrying to enqueue :"   i);
            if (q.enqueue(i)) {
                System.out.print("... success");
            }
        }
        if (!q.enqueue(210)) {
            System.out.println("... failed");
            System.out.println("\nTest 11 successfull:  1");
            t11 = true;
            myMark = myMark   1;
        }
        if (!t11) {
            System.out.println("\nTest 11 failed********************.");
        }
        // TEST 12
        // (2 mark) If one enqueues the values 1 through 10, in order,
        // into an empty queue, then if 10 dequeues are done the values
        // dequeues are 1 through 10.

        // reset the queue
        System.out.println("\nTest 12.");
        q = new A2KeyesOwenQueue();
        int i;
        // insert 10 elements 1 through 10
        for (i = 1; i <= 10; i  ) {

            System.out.print("Trying to enqueue :"   i);
            if (q.enqueue(i)) {
                System.out.print("... success");
            }
            else {
                System.out.print("... failed");
            }
            System.out.println();
        }
        boolean t12 = true;
        for (i = 1; i < 10; i  ) {
            if (i == q.dequeue()) {
                System.out.println("Dequeued expected element");
            }
            else {
                System.out.println("The element expected was not what was recieved");
                t12 = false;
            }
        }
        if (t12) {
            System.out.println("\nTest 12 successfull:  2");
            myMark = myMark   2;
        }
        else {
            System.out.println("\nTest 12 failed********************");
        }

        // TEST 13
        // (1 mark) print all elements in queue when front < rear
        // reset the queue
        q = new A2KeyesOwenQueue();
        // insert 7 elements 50 51 52 53 54 55
        for (i = 50; i < 56; i  = 1) {

            System.out.print("Trying to enqueue :"   i);
            if (q.enqueue(i)) {
                System.out.println("... success");
            }
            else {
                System.out.println("... failed");
            }

        }
        int stringOutput = 0;
        System.out.println("\nTEST 13"); // (1 mark)
        String myQ, yourQ;
        System.out.println("The elements in queue according to Mr. Corea:");
        myQ = "[50, 51, 52, 53, 54, 55]";
        yourQ = q.displayQueue(); //toString();
        System.out.println(myQ);
        System.out.println("The elements in queue according to You:");
        System.out.println(yourQ);
        System.out.println("MATCH: "   myQ.equals(yourQ));
        if (myQ.equals(yourQ)) {
            System.out.println("\nTest 13 successfull:  1");
            stringOutput  = 1;
        }
        else {
            System.out.println("Test 13 failed********************");
        }
        // TEST 14 print all elements in queue when rear < front
        // reset the queue
        q = new A2KeyesOwenQueue();
        // insert 10 elements 50 51 52 53 54 55 56 57 58 59
        for (i = 50; i < 60; i  = 1) {

            System.out.print("Trying to enqueue :"   i);
            if (q.enqueue(i)) {
                System.out.print("... success");
            }
            else {
                System.out.print("... failed");
            }
            System.out.println();
        }

        // dequeue 5 elements
        q.dequeue();
        q.dequeue();
        q.dequeue();
        q.dequeue();
        q.dequeue();

        // enqueueing 3 more
        q.enqueue(60);
        q.enqueue(61);
        q.enqueue(62);
        System.out.println("\nTEST 14"); // (2 mark)
        System.out.println("The elements in queue according to Mr. Corea:");
        myQ = "[55, 56, 57, 58, 59, 60, 61, 62]";
        yourQ = q.displayQueue(); //toString();
        System.out.println(myQ);
        System.out.println("The elements in queue according to You:");
        System.out.println(yourQ);
        System.out.println("MATCH: "   myQ.equals(yourQ));
        if (myQ.equals(yourQ)) {
            System.out.println("\nTest 14 successfull:  2");
            stringOutput  = 2;
        }
        else {
            System.out.println("Test 14 failed********************");
        }

        // dequeue 2 elements
        System.out.println(q.dequeue());
        System.out.println(q.dequeue());
        System.out.println(q.dequeue());
        System.out.println(q.dequeue());
        System.out.println(q.dequeue());
        System.out.println(q.dequeue());
        System.out.println(q.dequeue());
        System.out.println(q.dequeue());
        System.out.println(q.dequeue());
        System.out.println(q.dequeue());
        System.out.println("\nTEST 15"); // (2 mark) print an empty queue
        System.out.println("The elements in queue according to Mr. Corea:");
        myQ = "[]";
        yourQ = q.displayQueue(); //toString();
        System.out.println(myQ);
        System.out.println("The elements in queue according to You:");
        System.out.println(yourQ);
        System.out.println("MATCH: "   myQ.equals(yourQ));
        if (myQ.equals(yourQ)) {
            System.out.println("\nTest 15 successfull:  2");
            stringOutput  = 2;
        }
        else {
            System.out.println("Test 15 failed********************");
        }
        System.out.println("TEST RESULTS: "   myMark   " /17 plus "   stringOutput
                  "/5 for correct toString()");

    }
}

Help would be appreciated

CodePudding user response:

The code appends myArray[0] twice, this should fix the your issue

public String toString() {

    StringBuilder arr;
    arr = new StringBuilder("[");
    arr.append(myArray[0]);
    if (numItems > 0) {
        for (int i = 1; i < 10 && i < numItems; i  ) {

            arr.append(", ").append(myArray[i]);
        }

        arr.append("]");
    } else {
        arr.append("]");
    }
    return arr.toString();
}

CodePudding user response:

Here is the body of method toString() that returns the correct result.
(Notes after the code.)

String arr = "[";
if (numItems > 0) {
    if (rear == front) {
        arr  = myArray[front];
    }
    else {
        int limit;
        if (rear > front) {
            limit = rear;
        }
        else {
            limit = 10;
        }
        boolean first = true;
        for (int i = front; i < limit; i  ) {
            if (first) {
                first = false;
            }
            else {
                arr  = ", ";
            }
            arr  = myArray[i];
        }
        if (rear < front) {
            for (int i = 0; i < rear; i  ) {
                arr  = ", ";
                arr  = myArray[i];
            }
        }
    }
}
arr  = "]";
return arr;

Since the queue is circular, the start of the queue starts at front and extends to rear.

If rear is equal to front, that means that the queue contains only a single element.

If rear is greater that front, that means the the queue is not full, i.e. less than ten elements have been added to the queue.

If rear is less than front, it means that myArray is full, i.e. contains ten elements, and so you need to print all the elements from front to the last element of myArray and then you need to post the first elements of myArray starting from the first element up to rear.

For example if myArray is as follows:

[60, 61, 62, 63, 54, 55, 56, 57, 58, 59]

and front equals 5 and rear equals 4, then rear is less than front so the first for loop in the above code will append the values from 55 to 59 and the second for loop will append the values from 60 to 63.

Note that I only changed method toString. I did not change any of the other code in your question. When I ran your test cases, this was the last line of the output.

TEST RESULTS: 17 /17 plus 5/5 for correct toString()
  • Related