In Java, this program has been very difficulit.: A shipping carton has five fixe
ID: 3862841 • Letter: I
Question
In Java, this program has been very difficulit.: A shipping carton has five fixed compartments, each measuring 12” long by 6” wide by 8” high (inside dimensions). The carton weighs 2 pounds when it is empty. The following items were given to a packing robot to pack into the shipping carton: Item 1: 10” L, 4” W, 8” H, weight=5 lb. Item 2: 11” L, 5” W, 7” H, weight=6 lb. Item 3: 13” L, 4” W, 6” H, weight=4.5 lb. Item 4: 9” L, 4” W, 9” H, weight=6.5 lb. Item 5: 12” L, 6” W, 8” H, weight=4.8 lb. Item 6: 10” L, 4” W, 8” H, weight=7 lb. Item 7: 10” L, 4” W, 8” H, weight=7 lb. Item 8: 10” L, 4” W, 8” H, weight=5 lb. The robot will make sure each item fits before it is packed and set it aside if does not fit. If the shipping carton is full, the item will be set aside. Print a report showing how many items were packed and the total weight of the container. Getting started: class PackedItem { static final double LENGTH_LIMIT = 12; static final double WIDTH_LIMIT = 6; static final double HEIGHT_LIMIT = 8; static final int NUMBER_OF_COMPARTMENTS = 5; private static int numberPacked = 0; private static double totalWeight = 2; } Sample output: Item not packed: Too Big Item not packed: Too Big Item not packed: Carton Full Number of items packed = 5 / Total Weight = 31.8 pounds
Explanation / Answer
1. PackedItem.java:
public class PackedItem {
static final double LENGTH_LIMIT = 12;
static final double WIDTH_LIMIT = 6;
static final double HEIGHT_LIMIT = 8;
static final int NUMBER_OF_COMPARTMENTS = 5;
private static int numberPacked = 0;
private static double totalWeight = 2;
private static int lengthUsed = 0;
private static int breadthUsed = 0;
private static int heightUsed = 0;
private static double weightUsed = 0;
public static int getLengthUsed() {
return lengthUsed;
}
public static void setLengthUsed(int lengthUsed) {
PackedItem.lengthUsed = lengthUsed;
}
public static int getHeightUsed() {
return heightUsed;
}
public static void setHeightUsed(int heightUsed) {
PackedItem.heightUsed = heightUsed;
}
public static double getWeightUsed() {
return weightUsed;
}
public static void setWeightUsed(double weightUsed) {
PackedItem.weightUsed = weightUsed;
}
public static int getNumberPacked() {
return numberPacked;
}
public static void setNumberPacked(int numberPacked) {
PackedItem.numberPacked = numberPacked;
}
public static double getTotalWeight() {
return totalWeight;
}
public static void setTotalWeight(double totalWeight) {
PackedItem.totalWeight = totalWeight;
}
public boolean putBoxStyleOne(Box box){
lengthUsed = 0;
breadthUsed = 0;
int l = box.getLength();
int b = box.getWidth();
int h = box.getHeight();
double w = box.getWeight();
boolean setSuccess = false;
if(lengthUsed + l <= LENGTH_LIMIT){
lengthUsed = lengthUsed + l;
setSuccess = true;
}
else{
setSuccess = false;
}
if(breadthUsed + b <= WIDTH_LIMIT){
breadthUsed = breadthUsed + b;
setSuccess = true;
}
else{
setSuccess = false;
}
if(heightUsed + h <= HEIGHT_LIMIT){
heightUsed = heightUsed + h;
setSuccess = true;
}
else{
setSuccess = false;
}
if(setSuccess){
numberPacked++;
weightUsed = weightUsed + w;
totalWeight = totalWeight + weightUsed;
return true;
}
else{
return false;
}
}
}
2. Box.java:
public class Box {
private int length;
private int width;
private int height;
private double weight;
public Box(int length, int width, int height, double weight) {
super();
this.length = length;
this.width = width;
this.height = height;
this.weight = weight;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Length: " + length + "Width: " + width + "Height: " + height;
}
}
3. Driver.java:
import java.util.ArrayList;
import java.util.List;
public class Driver {
public static void main(String[] args) {
PackedItem packingBox = new PackedItem();
Box box1 = new Box(10, 4, 8, 5);
Box box2 = new Box(11, 5, 7, 6);
Box box3 = new Box(13, 4, 6, 4.5);
Box box4 = new Box(9, 4, 9, 6.5);
Box box5 = new Box(12, 6, 8, 4.8);
Box box6 = new Box(10, 4, 8, 7);
Box box7 = new Box(10, 4, 8, 7);
Box box8 = new Box(10, 4, 8, 5);
List<Box> boxList = new ArrayList<Box>();
boxList.add(box1);
boxList.add(box2);
boxList.add(box3);
boxList.add(box4);
boxList.add(box5);
boxList.add(box6);
boxList.add(box7);
boxList.add(box8);
List<Boolean> statusList = new ArrayList<Boolean>();
statusList.add(false);
statusList.add(false);
statusList.add(false);
statusList.add(false);
statusList.add(false);
statusList.add(false);
statusList.add(false);
statusList.add(false);
while (packingBox.getHeightUsed() < PackedItem.HEIGHT_LIMIT) {
for (int i = 0; i < statusList.size(); i++) {
if (statusList.get(i) == false) {
statusList.set(i, packingBox.putBoxStyleOne(boxList.get(i)));
}
}
}
packingBox.setHeightUsed(0);
packingBox.setLengthUsed(0);
packingBox.setTotalWeight(0);
packingBox.setLengthUsed(0);
while (packingBox.getHeightUsed() < PackedItem.HEIGHT_LIMIT) {
Box box = boxList.get(0);
if(packingBox.getHeightUsed() + box.getHeight() > PackedItem.HEIGHT_LIMIT){
break;
}
for (int i = 0; i < statusList.size(); i++) {
if (statusList.get(i) == false) {
statusList.set(i, packingBox.putBoxStyleOne(boxList.get(i)));
}
}
}
packingBox.setHeightUsed(0);
packingBox.setLengthUsed(0);
packingBox.setTotalWeight(0);
packingBox.setLengthUsed(0);
while (packingBox.getHeightUsed() < PackedItem.HEIGHT_LIMIT) {
Box box = boxList.get(0);
if(packingBox.getHeightUsed() + box.getHeight() > PackedItem.HEIGHT_LIMIT){
break;
}
for (int i = 0; i < statusList.size(); i++) {
if (statusList.get(i) == false) {
statusList.set(i, packingBox.putBoxStyleOne(boxList.get(i)));
}
}
}
packingBox.setHeightUsed(0);
packingBox.setLengthUsed(0);
packingBox.setTotalWeight(0);
packingBox.setLengthUsed(0);
while (packingBox.getHeightUsed() < PackedItem.HEIGHT_LIMIT) {
Box box = boxList.get(0);
if(packingBox.getHeightUsed() + box.getHeight() > PackedItem.HEIGHT_LIMIT){
break;
}
for (int i = 0; i < statusList.size(); i++) {
if (statusList.get(i) == false) {
statusList.set(i, packingBox.putBoxStyleOne(boxList.get(i)));
}
}
}
packingBox.setHeightUsed(0);
packingBox.setLengthUsed(0);
packingBox.setTotalWeight(0);
packingBox.setLengthUsed(0);
while (packingBox.getHeightUsed() < PackedItem.HEIGHT_LIMIT) {
Box box = boxList.get(0);
if(packingBox.getHeightUsed() + box.getHeight() > PackedItem.HEIGHT_LIMIT){
break;
}
for (int i = 0; i < statusList.size(); i++) {
if (statusList.get(i) == false) {
statusList.set(i, packingBox.putBoxStyleOne(boxList.get(i)));
}
}
}
int countSuccess = 0;
for (int i = 0; i < statusList.size(); i++) {
if (statusList.get(i) == false) {
System.out.println("Item not packed: Too Big");
}
else{
countSuccess++;
}
}
System.out.println("Full Number of items packed = "+countSuccess+" / 8");
System.out.println("Weight = "+ packingBox.getTotalWeight()+ " pounds");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.