Write a program to pack boxes with blobs. Write two classes and a driver program
ID: 3637469 • Letter: W
Question
Write a program to pack boxes with blobs. Write two classes and a driver program.
Note that the driver program may not use some of the methods or constructors. You should define them anyway.
The code must be completed how i asked to get full points. The code must include a comment for each method describing the method, a comment for each parameter describing what the parameter represents, and a comment for each variable declaration (including instance variables) describing what the variable represents.
Explanation / Answer
File 1 - BlobInterface.java:
public interface BlobInterface
{
public double getWeight();
}
File 2 - BoxInterface.java:
public interface BoxInterface {
// inserts a Blob into the box, returning true, or,
// if the Blob will not fit into the box, false
public boolean insert(BlobInterface b);
// returns the remaining capacity of the box.
public double remainingCapacity();
// prints out the weights of all of the Blobs in the
// Box, followed by the total weight
public void printContents();
}
File 3 - Blob.java:
public class Blob implements BlobInterface
{
private static final double DEFAULT_WEIGHT = 2; //The default weight to be used
private static final double MAX_WEIGHT = 4; //The max possible weight
private double weight; //the weight of the current blob
/**
* Default Constructor initializing the Blob with the default weight.
**/
public Blob()
{
this(DEFAULT_WEIGHT);
}
/**
* Constructor setting the weight of the blob to the weight passed in.
* If the weight is too small or too large, the default weight is used instead
*
* @param weight the weight to set the blob to.
**/
public Blob(double weight)
{
if ((weight > MAX_WEIGHT) || (weight < 1))
{
System.out.println("Weight must be between 1 and " + MAX_WEIGHT + ". Using " + DEFAULT_WEIGHT +" instead.");
this.weight = DEFAULT_WEIGHT;
}
else
{
this.weight = weight;
}
}
/**
* returns the weight of the blob.
**/
public double getWeight()
{
return weight;
}
}
File 4 - Box.java:
import java.util.*;
public class Box implements BoxInterface
{
private static final int DEFAULT_CAPACITY = 25; //The Default capacity of the Box
private double remainingCapacity; //How much capacity remains in the box
private Set<BlobInterface> blobs; //A set of all blobs in the box.
/**
*Default Constructor setting the Box to it's default capacity.
**/
public Box()
{
this(DEFAULT_CAPACITY);
}
/**
*Constructor initializing variables and setting the box to the capacity passed in.
*If the capacity is 0 or less, the default capacity is used.
*
*@param capacity the capacity to set the box to.
**/
public Box(int capacity)
{
if (capacity <= 0)
{
System.out.println("Capacity must be a positive number. Using " + DEFAULT_CAPACITY + " instead.");
this.remainingCapacity = DEFAULT_CAPACITY;
}
else
{
this.remainingCapacity = capacity;
}
this.blobs = new HashSet<BlobInterface>();
}
/**
*Tries to insert a BlobInterface into the box.
*If it fits (remaining capacity >= blob weight), the remaining capacity is decreased and true is returned.
*Else it is not added and false is returned.
**/
public boolean insert(BlobInterface b)
{
double blobWeight = b.getWeight(); //the weight of the Blob.
if (blobWeight <= remainingCapacity)
{
blobs.add(b);
remainingCapacity -= blobWeight;
return true;
}
else
{
return false;
}
}
/**
*returns the remaining capacity of the box.
**/
public double remainingCapacity()
{
return remainingCapacity;
}
/**
*Prints the contents of the box - each blob and it's weight along with the box's remaining capacity.
**/
public void printContents()
{
Iterator<BlobInterface> blobIterator = blobs.iterator(); //iterator of blobs used for looping through the Set of them.
while (blobIterator.hasNext())
{
BlobInterface blob = blobIterator.next(); //The next Blob to print the weight of...
System.out.println("Blob Weight: " + blob.getWeight());
}
System.out.println("Remaining Weight: " + remainingCapacity);
}
}
File 5- Driver.java:
import java.util.*;
public class Driver{
private static List<BoxInterface> boxes; //A List of all boxes created. An ordered List is used instead of a set so when we print, the filled boxes are shown first.
/**
*Main method.
**/
public static void main(String[] args) {
instantiateBoxes();
fillBoxes();
printBoxRecords();
}
/**
*Instantiates the box List and fills it with 5 boxes.
**/
private static void instantiateBoxes() {
boxes = new ArrayList<BoxInterface>();
for (int x = 0; x < 5; x++) {
BoxInterface box = new Box(); //A new box. The default constructor is used setting the weight to its default value (in this case, 25)
boxes.add(box);
}
}
/**
*Fills the boxes with blobs. Starts with the first Box and when it runs out of room, the next box is used, and so on.
**/
private static void fillBoxes() {
int boxNumber = 0; //The box number we are up to. Used for getting the Box from the list.
for (int x = 0; x < 30; x++) {
BoxInterface currentBox = boxes.get(boxNumber); //The box we are up to.
double weight = Math.random()*3+1; //The random weight of the blob to use.
Blob blob = new Blob(weight); //A new blob to be added to the box. The weight variable instantiated above us used
if (!currentBox.insert(blob)) {
boxNumber ++;
if (boxNumber < boxes.size()){
currentBox = boxes.get(boxNumber); //if the box is full, the next box is gotten.
currentBox.insert(blob);
}
}
}
}
/** *Iterates through the boxes and print the contents of each one. **/
private static void printBoxRecords() {
Iterator<BoxInterface> boxIterator = boxes.iterator(); //Iterator for looping through the box list.
int count = 0; //Count used for the label of the Box when printing.
while (boxIterator.hasNext()) {
count++;
System.out.println("Box " + count + ": ");
boxIterator.next().printContents();
System.out.println("");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.