For this problem you need to download the class IntQueue.class.

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
	}
}


Your task is to write a program to solve the following problem: you are given a queue of packages that need to be sent from a warehouse to a factory. The packages must be sent in exactly the same order as they arrive to the warehouse. Packages have different weight. You have a truck that can carry n tons. Load the truck with the packages until you exceed its capacity. The remaining packages should stay at the warehouse in the same order as originally.

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);
	    }
	}
}


This is an example from CSci 2101 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.