Name
Name

Reputation: 15

Variable doesn't change it's value despite being reset

I honestly have no idea what the problem is. This same issue happened in another part of the code which has not been fixed yet. The project is to merge two all-ready sorted queues based on the arrival time of a customer object which takes 3 integers. The three integers are Arrival Time, Service Time, and Quitting Time.
It just prints 917 7 times. Here is the code.

public static Queue2<Customer> Question3(Queue2<Customer> A, Queue2<Customer> B) {

    Queue2<Customer> C = new Queue2<Customer>(); 


    if (A.getSize() == 0 && B.getSize() == 0) { 

        System.out.println("Sorry Bro");  

    } else if (A.getSize() == 0) { 

        while (B.getSize() > 0) {

            C.enqueue(A.dequeue()); 

        }

    } else if (B.getSize() == 0) { 

        while (A.getSize() > 0) { 

            C.enqueue(B.dequeue()); 

        }

    } else { 
        Customer c1 = A.dequeue(); 
        Customer c2 = B.dequeue(); 
        int n = A.getSize() + B.getSize(); 

        for (int x = 0; x < n; x++) { 

            if (A.getSize() == 0) {

                C.enqueue(B.dequeue()); 

            } else if (B.getSize() == 0) { 

                C.enqueue(A.dequeue()); 

            } else { 

                if (c1.getArrival() > c2.getArrival()) { 

                    C.enqueue(c2); 
                    c2 = B.dequeue(); 
                    System.out.println(c2.getArrival()); 
                } else { 

                    C.enqueue(c1); 
                    c1 = A.dequeue(); 
                    System.out.println(c1.getArrival()); 
                }

            }

        }

    } 

     return C; 
} 

Here is the main method:

 Queue2<Customer> ctest = new Queue2<Customer>(); 
    ctest.enqueue(new Customer(915,10,0)); 
    ctest.enqueue(new Customer(916,6,0)); 
    ctest.enqueue(new Customer(918,7,0)); 
    ctest.enqueue(new Customer(925,3,0)); 

    Queue2<Customer> ctest1 = new Queue2<Customer>(); 
    ctest1.enqueue(new Customer(917,10,0)); 
    ctest1.enqueue(new Customer(922,3,0)); 
    ctest1.enqueue(new Customer(924,2,0)); 

    Queue2<Customer> ctest3 = Question3(ctest,ctest1); 
    Customer c4 = ctest3.dequeue(); 

    while(ctest3.getSize() > 0) { 

       System.out.println(c4.getArrival()); 
       c4 = ctest3.dequeue(); 
       System.out.println("Printing"); 

    }
    System.out.println(c4.getArrival()); 

Upvotes: 0

Views: 72

Answers (2)

Skrelp
Skrelp

Reputation: 189

I think your logic on the size checks if off.

When you are checking if A is empty with A.getSize() == 0 you then dequeue from A (which is empty and then check if the size of B changed.

Unless A and B are the same queue, dequeuing from A won't change the size of B.

Upvotes: 0

prime
prime

Reputation: 15574

Not clear about your original question, If we have the implementation of the Queue2 it would be helpful.

You have few things to consider regarding the code,

else if (A.getSize() == 0) { 

        while (B.getSize() > 0) {

            C.enqueue(A.dequeue()); 
            // A is already empty, why you dequeue it ? 
            // same goes with the other else (may be you wanted to do C.enqueue(B.dequeue())

        }
    }

And,

Customer c1 = A.dequeue(); // dequeue usually reduce the size of the queue
Customer c2 = B.dequeue(); 
int n = A.getSize() + B.getSize(); // This `n` might not be the one you expected 

Upvotes: 1

Related Questions