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

These are three solutions for packaging problem: two from from Fall 2004 and one from Spring 2005.
A solution with no nested loops:



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 = 5;

	    // create a IntQueue, an integer for a current item
	    IntQueue helper = new IntQueue();
	    int item;
	    
	    while(!packages.isEmpty()) {
	    	// take a package off the queue
		item = packages.dequeue();
		// subtract the item from the capacity
		capacity = capacity - item;
		
		if (capacity >= 0) {
		    // if the result is >+ 0, then load the item
		    System.out.println("Loading " + item + " on the truck");
		} else {
		    // otherwise the item has to be put back on the queue,
		    // store it in the helper queue for now
		    helper.enqueue(item);
		}		
	    }
	    System.out.println("Done loading");
	    
	    // move the items from the helper queue to the original one
	    while (!helper.isEmpty()) {
		item = helper.dequeue();
		packages.enqueue(item);
	    }

	    // print the queue
	    while (!packages.isEmpty()){
		item = packages.dequeue();
		System.out.println(item);
	    }
	}
}

Rob's and Jason's solution, or somethng close to it (I was writing it from memory, so there may be minor differences):

public class Packages2 {
	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;
	    IntQueue helper = new IntQueue();

	    while(!packages.isEmpty()) {
		int item = packages.dequeue();
		if (item < capacity) {
		    System.out.println("Loading " + item + " on the truck");
		    capacity = capacity - item;
		}
		else {
		    System.out.println("done loading");
		    capacity = 0; // to stop the loop

		    helper.enqueue(item);

		    // move the rest of the items to another queue
		    while (!packages.isEmpty()) {
			item = packages.dequeue();
			helper.enqueue(item);
		    }
		}
	    }

	    // move the other items back to the original queue
	    while (!helper.isEmpty()) {
		int temp = helper.dequeue();
		packages.enqueue(temp);
	    }

	    // print the queue
	    while (!packages.isEmpty()){
		int item = packages.dequeue();
		System.out.println(item);
	    }
	}
}

Another solution with nested loops (Fall 2004). Note a trick to reduce the number of enqueue/dequeue operations: the first item that doesn't fit on the truck is never put added to the helper queue. This requires that both extra while loops are nested. Why is the condition changed in the outer loop? Do you think that saving on one enqueue and one dequeue operation is worth the price of adding an extra condition?


public class Packages1 {
	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 = 20;

	    while(capacity > 0  && !packages.isEmpty()) {
		int item = packages.dequeue();
		if (item < capacity) {
		    System.out.println("Loading " + item + " on the truck");
		    capacity = capacity - item;
		}
		else {
		    System.out.println("done loading");
		    capacity = 0; // to stop the loop

		    // put the item in the front of the queue:

		    // move the rest of the items to another queue
		    IntQueue helper = new IntQueue();
		    while (!packages.isEmpty()) {
			int temp = packages.dequeue();
			helper.enqueue(temp);
		    }
		    
		    // put the item back to the original queue
		    packages.enqueue(item);

		    // move the other items back to the original queue
		    while (!helper.isEmpty()) {
			int temp = helper.dequeue();
			packages.enqueue(temp);
		    }
		}
	    }

	    // print the queue
	    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.