The following program demonstrates the queue of integers:
//the program demonstrates a queue used to store integers (int)
public class ShowIntQueue {
public static void main(String [] args) {
// creating a new queue
IntQueue iq = new IntQueue();
// a variable to store integers taken off the queue
int n;
iq.enqueue(2);
iq.enqueue(3);
n = iq.dequeue();
System.out.println("n = " + n);
iq.enqueue(4);
iq.enqueue(5);
while( !iq.isEmpty()) {
n = iq.dequeue();
System.out.print(n + " ");
}
System.out.println(); // new line
}
}
Fill in the code in the program below. You have a queue of packages (the numbers respresent weight) and the capacity of the truck. Test your program with different number and sizes of packages and different truck capacity.
public class Packages {
public static void main(String [] args) {
// creating a new queue
IntQueue packages = new IntQueue();
// storing some elements
packages.enqueue(2);
packages.enqueue(1);
packages.enqueue(5);
packages.enqueue(3);
packages.enqueue(4);
packages.enqueue(2);
int capacity = 10;
// your code goes here:
// At the end print the queue to test
while (!packages.isEmpty()){
int item = packages.dequeue();
System.out.println(item);
}
}
}