I am trying to create a basic airport simulation using two queues. They are supposed to alternate between taking off, and when one has no more 'planes' left, the NPE needs to be handled so that the other queue can continue going until the loop has finished and both queues have run out, as one has 7 entries and the other has 4. I thought if I tried to put an exception handler in the dequeue method it might fix it, but I have to use a generic class for it, so that didn't work properly. As of right now the for loop that is meant to dequeue runs until the 4th loop, which is when it stops, throws the NPE, and exits the program. So my question is, how can I make it so that when one queue runs out, it stops trying to loop it and instead continues looping the last queue?
Here is my main class where the loop is running:
public abstract class Airport<T> {
public static void main(String[] args) {
//creates separate queues
Driver<Integer> runway1 = new Driver<>();
Driver<Integer> runway2 = new Driver<>();
//adds 7 planes into runway 1
for (int i = 0; i < 7; i++) {
runway1.enqueue(i);
}
//adds 4 planes into runway 2
for (int i = 0; i < 4; i++) {
runway2.enqueue(i);
}
int size1 = runway1.length();
int size2 = runway2.length();
//run loop while either runway has planes left
while (size1 > 0 && size2 > 0) {
System.out.println("There are currently " + size1 + " planes waiting for takeoff in Runway 1.");
System.out.println("There are currently " + size2 + " planes waiting for takeoff in Runway 2.\n");
for (int lane = 0; lane < 7; lane++) {
int lane1, lane2;
if (runway1.isEmpty()){
System.out.println("Runway 1 is empty.");
}
else {
lane1 = runway1.getFront();
System.out.println("Plane " + lane1 + " has taken off from Runway 1.");
runway1.dequeue();
}
if (runway2.isEmpty()){
System.out.println("Runway 2 is empty.");
}
else{
lane2 = runway2.getFront(); //EDIT: npe occurs here
System.out.println("Plane " + lane2 + " has taken off from Runway 2.\n");
runway2.dequeue();
}
}
}
}
}
And here is the part of my driver class:
class Driver<T> implements Queue1<T> {
private static final int defaultSize = 10;
private int maxSize; // Maximum size of queue
private int front; // Index of front element
private int rear; // Index of rear element
private T[] listArray; // Array holding queue elements
/**
* Constructors
*/
Driver() {
this(defaultSize);
}
@SuppressWarnings("unchecked")
// For generic array
Driver(int size) {
maxSize = size + 1; // One extra space is allocated
rear = 0;
front = 1;
listArray = (T[]) new Object[maxSize]; // Create listArray
}
/**
* Put "it" in queue
*/
public void enqueue(T it) {
assert ((rear + 2) % maxSize) != front : "Queue is full";
rear = (rear + 1) % maxSize; // Circular increment
listArray[rear] = it;
}
/**
* Remove and return front value
**/
public T dequeue() {
assert length() != 0 : "Queue is empty";
T it = listArray[front];
front = (front + 1) % maxSize; // Circular increment
return it;
}
/**
* Return Front Value
**/
@Override
public T getFront() {
assert length() != 0 : "Queue is empty";
return listArray[front];
}
@Override
public T getBack() {
assert length() != 0 : "Queue is empty";
return listArray[rear];
}
@Override
public boolean isEmpty() {
if(listArray == null){
return true;
}
return false;
}
/**
* @return Queue length
**/
public int length() {
return ((rear + maxSize) - front + 1) % maxSize;
}
}
I've been scratching my head over this for days, so any help would be incredibly appreciated! Thank you!