Please download the class IntQueue.class. The class QueueException.class is the same as for CharQueue.

Please 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.

Below is an updated version, as of 9/10/04


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:
	    
	    // create a IntQueue, an integer for a current item
	    
	    while(!packages.isEmpty()) {
	    	// take a package off the queue
		// CAREFULLY check if we can load the item
		// if we can, subtract item from the capacity
		
		// to be continued...
	    }
	    
	    
	    // 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.