3D bin packing in java (compare two codes)Java
I made some changes to the first code here I wrote and I want to know which one is more correct logically (before the changes or after the changes).
public boolean put3D(ItemsUnit item, int p,int n) {
//------if x still did not exceed the container's length
if(x<length){
//--------if we can put the new item next to the item packed in the extreme point of length
if(putL(item,p)) {
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if y still did not exceed the container's breadth
if(y<breadth) {
//--------if we can put the new item next to the item packed in the extreme point of breadth
if(putB(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if z still did not exceed the container's height
if(z<height){
//--------if we can put the new item next to the item packed in the extreme point of height
if(putH(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
return false; //return false if item cannot be packed in neither extreme point
}
//-----------adding the new item to the extreme point in length
private boolean putL(ItemsUnit item, int p) {
double minRemL=remainingLength[0]; //the minimum remaining length of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingLength.length; j++){
if ((remainingLength[j] != 0) && (minRemL >= remainingLength[j]) && (remainingLength[j] >= item.getLength())){
i=j; //storing the item next to which we should put the new packed item
minRemL=remainingLength[j]; //minimum length left
}
}
remainingLength[p]=remainingLength[i]-item.getLength(); //update the remaining length of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight(); //update the remaining height of the new item added
remainingLength[i]=0; //insert 0 to the remainingLength of the item next to which we put the new item (so that we don't consider its remaining length anymore)
x+=item.getLength(); //increment x by the length of the new packed item in the extreme point of length
return true;
}
//-----------adding the new item to the extreme point in breadth
private boolean putB(ItemsUnit item, int p) {
double minRemB=remainingBreadth[0]; //the minimum remaining breadth of all already packed items
int i=0;//to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingBreadth.length; j++){
if ((remainingBreadth[j] != 0) && (minRemB >= remainingBreadth[j]) && (remainingBreadth[j] >= item.getBreadth())){
i=j; //choosing the item to which we should put the new packed item next to
minRemB=remainingBreadth[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingBreadth[p]=remainingBreadth[i]-item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight();//update the remaining height of the new item added
remainingLength[p]-=item.getLength(); //update the remaining length of the new item added
remainingBreadth[i]=0; //insert 0 to the remainingBreadth of the item next to which we put the new item (so that we don't consider its remaining breadth anymore)
y+=item.getBreadth(); //increment y by the breadth of the new packed item in the extreme point of breadth
//x=length-remainingLength[p]; //update x to the position of the item next to which we put the new item
return true;
}
//-----------adding the new item to the extreme point in height
private boolean putH(ItemsUnit item, int p) {
double minRemH=remainingHeight[0]; //the minimum remaining height of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingHeight.length; j++){
if ((remainingHeight[j] !=0 )&&(minRemH >= remainingHeight[j]) && (remainingHeight[j] >= item.getHeight())){
i=j; //choosing the item to which we should put the new packed item next to
minRemH=remainingHeight[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingHeight[p]=remainingHeight[i]-item.getHeight(); //update the remaining height of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingLength[p]-=item.getLength();//update the remaining length of the new item added
remainingHeight[i]=0; //insert 0 to the remainingHeight of the item next to which we put the new item (so that we don't consider its remaining height anymore)
z+=item.getHeight(); //increment z by the height of the new packed item in the extreme point of height
return true;
}
public boolean put3D(ItemsUnit item, int p,int n) {
//------if x still did not exceed the container's length
if(x<length){
z=remainingHeight[p]; // update z to the position of the new item added
y=remainingBreadth[p];// update y to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of length
if(putL(item,p)) {
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if y still did not exceed the container's breadth
if(y<breadth) {
x=remainingLength[p]; // update x to the position of the new item added
z=remainingHeight[p]; // update z to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of breadth
if(putB(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if z still did not exceed the container's height
if(z<height){
x=remainingLength[p]; // update x to the position of the new item added
y=remainingBreadth[p];// update y to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of height
if(putH(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
return false; //return false if item cannot be packed in neither extreme point
}
//-----------adding the new item to the extreme point in length
private boolean putL(ItemsUnit item, int p) {
double minRemL=remainingLength[0]; //the minimum remaining length of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingLength.length; j++){
if ((remainingLength[j] != 0) && (minRemL >= remainingLength[j]) && (remainingLength[j] >= item.getLength())){
i=j; //storing the item next to which we should put the new packed item
minRemL=remainingLength[j]; //minimum length left
}
}
remainingLength[p]=remainingLength[i]-item.getLength(); //update the remaining length of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight(); //update the remaining height of the new item added
remainingLength[i]=0; //insert 0 to the remainingLength of the item next to which we put the new item (so that we don't consider its remaining length anymore)
x+=item.getLength(); //increment x by the length of the new packed item in the extreme point of length
return true;
}
//-----------adding the new item to the extreme point in breadth
private boolean putB(ItemsUnit item, int p) {
double minRemB=remainingBreadth[0]; //the minimum remaining breadth of all already packed items
int i=0;//to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingBreadth.length; j++){
if ((remainingBreadth[j] != 0) && (minRemB >= remainingBreadth[j]) && (remainingBreadth[j] >= item.getBreadth())){
i=j; //choosing the item to which we should put the new packed item next to
minRemB=remainingBreadth[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingBreadth[p]=remainingBreadth[i]-item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight();//update the remaining height of the new item added
remainingLength[p]-=item.getLength(); //update the remaining length of the new item added
remainingBreadth[i]=0; //insert 0 to the remainingBreadth of the item next to which we put the new item (so that we don't consider its remaining breadth anymore)
y+=item.getBreadth(); //increment y by the breadth of the new packed item in the extreme point of breadth
//x=length-remainingLength[p]; //update x to the position of the item next to which we put the new item
return true;
}
//-----------adding the new item to the extreme point in height
private boolean putH(ItemsUnit item, int p) {
double minRemH=remainingHeight[0]; //the minimum remaining height of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingHeight.length; j++){
if ((remainingHeight[j] !=0 )&&(minRemH >= remainingHeight[j]) && (remainingHeight[j] >= item.getHeight())){
i=j; //choosing the item to which we should put the new packed item next to
minRemH=remainingHeight[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingHeight[p]=remainingHeight[i]-item.getHeight(); //update the remaining height of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingLength[p]-=item.getLength();//update the remaining length of the new item added
remainingHeight[i]=0; //insert 0 to the remainingHeight of the item next to which we put the new item (so that we don't consider its remaining height anymore)
z+=item.getHeight(); //increment z by the height of the new packed item in the extreme point of height
return true;
}
I need to know which one is logically correct? Thank you.
3D bin packing in java (compare two codes)
I made some changes to the first code here I wrote and I want to know which one is more correct logically (before the changes or after the changes)
public boolean put3D(ItemsUnit item, int p,int n) {
//------if x still did not exceed the container's length
if(x<length){
//--------if we can put the new item next to the item packed in the extreme point of length
if(putL(item,p)) {
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if y still did not exceed the container's breadth
if(y<breadth) {
//--------if we can put the new item next to the item packed in the extreme point of breadth
if(putB(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if z still did not exceed the container's height
if(z<height){
//--------if we can put the new item next to the item packed in the extreme point of height
if(putH(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
return false; //return false if item cannot be packed in neither extreme point
}
//-----------adding the new item to the extreme point in length
private boolean putL(ItemsUnit item, int p) {
double minRemL=remainingLength[0]; //the minimum remaining length of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingLength.length; j++){
if ((remainingLength[j] != 0) && (minRemL >= remainingLength[j]) && (remainingLength[j] >= item.getLength())){
i=j; //storing the item next to which we should put the new packed item
minRemL=remainingLength[j]; //minimum length left
}
}
remainingLength[p]=remainingLength[i]-item.getLength(); //update the remaining length of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight(); //update the remaining height of the new item added
remainingLength[i]=0; //insert 0 to the remainingLength of the item next to which we put the new item (so that we don't consider its remaining length anymore)
x+=item.getLength(); //increment x by the length of the new packed item in the extreme point of length
return true;
}
//-----------adding the new item to the extreme point in breadth
private boolean putB(ItemsUnit item, int p) {
double minRemB=remainingBreadth[0]; //the minimum remaining breadth of all already packed items
int i=0;//to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingBreadth.length; j++){
if ((remainingBreadth[j] != 0) && (minRemB >= remainingBreadth[j]) && (remainingBreadth[j] >= item.getBreadth())){
i=j; //choosing the item to which we should put the new packed item next to
minRemB=remainingBreadth[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingBreadth[p]=remainingBreadth[i]-item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight();//update the remaining height of the new item added
remainingLength[p]-=item.getLength(); //update the remaining length of the new item added
remainingBreadth[i]=0; //insert 0 to the remainingBreadth of the item next to which we put the new item (so that we don't consider its remaining breadth anymore)
y+=item.getBreadth(); //increment y by the breadth of the new packed item in the extreme point of breadth
//x=length-remainingLength[p]; //update x to the position of the item next to which we put the new item
return true;
}
//-----------adding the new item to the extreme point in height
private boolean putH(ItemsUnit item, int p) {
double minRemH=remainingHeight[0]; //the minimum remaining height of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingHeight.length; j++){
if ((remainingHeight[j] !=0 )&&(minRemH >= remainingHeight[j]) && (remainingHeight[j] >= item.getHeight())){
i=j; //choosing the item to which we should put the new packed item next to
minRemH=remainingHeight[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingHeight[p]=remainingHeight[i]-item.getHeight(); //update the remaining height of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingLength[p]-=item.getLength();//update the remaining length of the new item added
remainingHeight[i]=0; //insert 0 to the remainingHeight of the item next to which we put the new item (so that we don't consider its remaining height anymore)
z+=item.getHeight(); //increment z by the height of the new packed item in the extreme point of height
return true;
}
public boolean put3D(ItemsUnit item, int p,int n) {
//------if x still did not exceed the container's length
if(x<length){
z=remainingHeight[p]; // update z to the position of the new item added
y=remainingBreadth[p];// update y to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of length
if(putL(item,p)) {
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if y still did not exceed the container's breadth
if(y<breadth) {
x=remainingLength[p]; // update x to the position of the new item added
z=remainingHeight[p]; // update z to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of breadth
if(putB(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if z still did not exceed the container's height
if(z<height){
x=remainingLength[p]; // update x to the position of the new item added
y=remainingBreadth[p];// update y to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of height
if(putH(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
return false; //return false if item cannot be packed in neither extreme point
}
//-----------adding the new item to the extreme point in length
private boolean putL(ItemsUnit item, int p) {
double minRemL=remainingLength[0]; //the minimum remaining length of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingLength.length; j++){
if ((remainingLength[j] != 0) && (minRemL >= remainingLength[j]) && (remainingLength[j] >= item.getLength())){
i=j; //storing the item next to which we should put the new packed item
minRemL=remainingLength[j]; //minimum length left
}
}
remainingLength[p]=remainingLength[i]-item.getLength(); //update the remaining length of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight(); //update the remaining height of the new item added
remainingLength[i]=0; //insert 0 to the remainingLength of the item next to which we put the new item (so that we don't consider its remaining length anymore)
x+=item.getLength(); //increment x by the length of the new packed item in the extreme point of length
return true;
}
//-----------adding the new item to the extreme point in breadth
private boolean putB(ItemsUnit item, int p) {
double minRemB=remainingBreadth[0]; //the minimum remaining breadth of all already packed items
int i=0;//to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingBreadth.length; j++){
if ((remainingBreadth[j] != 0) && (minRemB >= remainingBreadth[j]) && (remainingBreadth[j] >= item.getBreadth())){
i=j; //choosing the item to which we should put the new packed item next to
minRemB=remainingBreadth[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingBreadth[p]=remainingBreadth[i]-item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight();//update the remaining height of the new item added
remainingLength[p]-=item.getLength(); //update the remaining length of the new item added
remainingBreadth[i]=0; //insert 0 to the remainingBreadth of the item next to which we put the new item (so that we don't consider its remaining breadth anymore)
y+=item.getBreadth(); //increment y by the breadth of the new packed item in the extreme point of breadth
//x=length-remainingLength[p]; //update x to the position of the item next to which we put the new item
return true;
}
//-----------adding the new item to the extreme point in height
private boolean putH(ItemsUnit item, int p) {
double minRemH=remainingHeight[0]; //the minimum remaining height of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingHeight.length; j++){
if ((remainingHeight[j] !=0 )&&(minRemH >= remainingHeight[j]) && (remainingHeight[j] >= item.getHeight())){
i=j; //choosing the item to which we should put the new packed item next to
minRemH=remainingHeight[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingHeight[p]=remainingHeight[i]-item.getHeight(); //update the remaining height of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingLength[p]-=item.getLength();//update the remaining length of the new item added
remainingHeight[i]=0; //insert 0 to the remainingHeight of the item next to which we put the new item (so that we don't consider its remaining height anymore)
z+=item.getHeight(); //increment z by the height of the new packed item in the extreme point of height
return true;
}
I need to know which one is logically correct? Thank you.
3D bin packing in Java
I made some changes to the first code here I wrote and I want to know which one is more correct logically (before the changes or after the changes).
public boolean put3D(ItemsUnit item, int p,int n) {
//------if x still did not exceed the container's length
if(x<length){
//--------if we can put the new item next to the item packed in the extreme point of length
if(putL(item,p)) {
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if y still did not exceed the container's breadth
if(y<breadth) {
//--------if we can put the new item next to the item packed in the extreme point of breadth
if(putB(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if z still did not exceed the container's height
if(z<height){
//--------if we can put the new item next to the item packed in the extreme point of height
if(putH(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
return false; //return false if item cannot be packed in neither extreme point
}
//-----------adding the new item to the extreme point in length
private boolean putL(ItemsUnit item, int p) {
double minRemL=remainingLength[0]; //the minimum remaining length of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingLength.length; j++){
if ((remainingLength[j] != 0) && (minRemL >= remainingLength[j]) && (remainingLength[j] >= item.getLength())){
i=j; //storing the item next to which we should put the new packed item
minRemL=remainingLength[j]; //minimum length left
}
}
remainingLength[p]=remainingLength[i]-item.getLength(); //update the remaining length of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight(); //update the remaining height of the new item added
remainingLength[i]=0; //insert 0 to the remainingLength of the item next to which we put the new item (so that we don't consider its remaining length anymore)
x+=item.getLength(); //increment x by the length of the new packed item in the extreme point of length
return true;
}
//-----------adding the new item to the extreme point in breadth
private boolean putB(ItemsUnit item, int p) {
double minRemB=remainingBreadth[0]; //the minimum remaining breadth of all already packed items
int i=0;//to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingBreadth.length; j++){
if ((remainingBreadth[j] != 0) && (minRemB >= remainingBreadth[j]) && (remainingBreadth[j] >= item.getBreadth())){
i=j; //choosing the item to which we should put the new packed item next to
minRemB=remainingBreadth[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingBreadth[p]=remainingBreadth[i]-item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight();//update the remaining height of the new item added
remainingLength[p]-=item.getLength(); //update the remaining length of the new item added
remainingBreadth[i]=0; //insert 0 to the remainingBreadth of the item next to which we put the new item (so that we don't consider its remaining breadth anymore)
y+=item.getBreadth(); //increment y by the breadth of the new packed item in the extreme point of breadth
//x=length-remainingLength[p]; //update x to the position of the item next to which we put the new item
return true;
}
//-----------adding the new item to the extreme point in height
private boolean putH(ItemsUnit item, int p) {
double minRemH=remainingHeight[0]; //the minimum remaining height of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingHeight.length; j++){
if ((remainingHeight[j] !=0 )&&(minRemH >= remainingHeight[j]) && (remainingHeight[j] >= item.getHeight())){
i=j; //choosing the item to which we should put the new packed item next to
minRemH=remainingHeight[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingHeight[p]=remainingHeight[i]-item.getHeight(); //update the remaining height of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingLength[p]-=item.getLength();//update the remaining length of the new item added
remainingHeight[i]=0; //insert 0 to the remainingHeight of the item next to which we put the new item (so that we don't consider its remaining height anymore)
z+=item.getHeight(); //increment z by the height of the new packed item in the extreme point of height
return true;
}
public boolean put3D(ItemsUnit item, int p,int n) {
//------if x still did not exceed the container's length
if(x<length){
z=remainingHeight[p]; // update z to the position of the new item added
y=remainingBreadth[p];// update y to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of length
if(putL(item,p)) {
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if y still did not exceed the container's breadth
if(y<breadth) {
x=remainingLength[p]; // update x to the position of the new item added
z=remainingHeight[p]; // update z to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of breadth
if(putB(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if z still did not exceed the container's height
if(z<height){
x=remainingLength[p]; // update x to the position of the new item added
y=remainingBreadth[p];// update y to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of height
if(putH(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
return false; //return false if item cannot be packed in neither extreme point
}
//-----------adding the new item to the extreme point in length
private boolean putL(ItemsUnit item, int p) {
double minRemL=remainingLength[0]; //the minimum remaining length of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingLength.length; j++){
if ((remainingLength[j] != 0) && (minRemL >= remainingLength[j]) && (remainingLength[j] >= item.getLength())){
i=j; //storing the item next to which we should put the new packed item
minRemL=remainingLength[j]; //minimum length left
}
}
remainingLength[p]=remainingLength[i]-item.getLength(); //update the remaining length of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight(); //update the remaining height of the new item added
remainingLength[i]=0; //insert 0 to the remainingLength of the item next to which we put the new item (so that we don't consider its remaining length anymore)
x+=item.getLength(); //increment x by the length of the new packed item in the extreme point of length
return true;
}
//-----------adding the new item to the extreme point in breadth
private boolean putB(ItemsUnit item, int p) {
double minRemB=remainingBreadth[0]; //the minimum remaining breadth of all already packed items
int i=0;//to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingBreadth.length; j++){
if ((remainingBreadth[j] != 0) && (minRemB >= remainingBreadth[j]) && (remainingBreadth[j] >= item.getBreadth())){
i=j; //choosing the item to which we should put the new packed item next to
minRemB=remainingBreadth[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingBreadth[p]=remainingBreadth[i]-item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight();//update the remaining height of the new item added
remainingLength[p]-=item.getLength(); //update the remaining length of the new item added
remainingBreadth[i]=0; //insert 0 to the remainingBreadth of the item next to which we put the new item (so that we don't consider its remaining breadth anymore)
y+=item.getBreadth(); //increment y by the breadth of the new packed item in the extreme point of breadth
//x=length-remainingLength[p]; //update x to the position of the item next to which we put the new item
return true;
}
//-----------adding the new item to the extreme point in height
private boolean putH(ItemsUnit item, int p) {
double minRemH=remainingHeight[0]; //the minimum remaining height of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingHeight.length; j++){
if ((remainingHeight[j] !=0 )&&(minRemH >= remainingHeight[j]) && (remainingHeight[j] >= item.getHeight())){
i=j; //choosing the item to which we should put the new packed item next to
minRemH=remainingHeight[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingHeight[p]=remainingHeight[i]-item.getHeight(); //update the remaining height of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingLength[p]-=item.getLength();//update the remaining length of the new item added
remainingHeight[i]=0; //insert 0 to the remainingHeight of the item next to which we put the new item (so that we don't consider its remaining height anymore)
z+=item.getHeight(); //increment z by the height of the new packed item in the extreme point of height
return true;
}
I need to know which one is logically correct.
public boolean put3D(ItemsUnit item, int p,int n) {
//------if x still did not exceed the container's length
if(x<length){
z=remainingHeight[p]; // update z to the position of the new item added
y=remainingBreadth[p];// update y to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of length
if(putL(item,p)) {
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if y still did not exceed the container's breadth
if(y<breadth) {
x=remainingLength[p]; // update x to the position of the new item added
z=remainingHeight[p]; // update z to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of breadth
if(putB(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if z still did not exceed the container's height
if(z<height){
x=remainingLength[p]; // update x to the position of the new item added
y=remainingBreadth[p];// update y to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of height
if(putH(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
return false; //return false if item cannot be packed in neither extreme point
}
//-----------adding the new item to the extreme point in length
private boolean putL(ItemsUnit item, int p) {
double minRemL=remainingLength[0]; //the minimum remaining length of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingLength.length; j++){
if ((remainingLength[j] != 0) && (minRemL >= remainingLength[j]) && (remainingLength[j] >= item.getLength())){
i=j; //storing the item next to which we should put the new packed item
minRemL=remainingLength[j]; //minimum length left
}
}
remainingLength[p]=remainingLength[i]-item.getLength(); //update the remaining length of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight(); //update the remaining height of the new item added
remainingLength[i]=0; //insert 0 to the remainingLength of the item next to which we put the new item (so that we don't consider its remaining length anymore)
x+=item.getLength(); //increment x by the length of the new packed item in the extreme point of length
return true;
}
//-----------adding the new item to the extreme point in breadth
private boolean putB(ItemsUnit item, int p) {
double minRemB=remainingBreadth[0]; //the minimum remaining breadth of all already packed items
int i=0;//to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingBreadth.length; j++){
if ((remainingBreadth[j] != 0) && (minRemB >= remainingBreadth[j]) && (remainingBreadth[j] >= item.getBreadth())){
i=j; //choosing the item to which we should put the new packed item next to
minRemB=remainingBreadth[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingBreadth[p]=remainingBreadth[i]-item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight();//update the remaining height of the new item added
remainingLength[p]-=item.getLength(); //update the remaining length of the new item added
remainingBreadth[i]=0; //insert 0 to the remainingBreadth of the item next to which we put the new item (so that we don't consider its remaining breadth anymore)
y+=item.getBreadth(); //increment y by the breadth of the new packed item in the extreme point of breadth
//x=length-remainingLength[p]; //update x to the position of the item next to which we put the new item
return true;
}
//-----------adding the new item to the extreme point in height
private boolean putH(ItemsUnit item, int p) {
double minRemH=remainingHeight[0]; //the minimum remaining height of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingHeight.length; j++){
if ((remainingHeight[j] !=0 )&&(minRemH >= remainingHeight[j]) && (remainingHeight[j] >= item.getHeight())){
i=j; //choosing the item to which we should put the new packed item next to
minRemH=remainingHeight[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingHeight[p]=remainingHeight[i]-item.getHeight(); //update the remaining height of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingLength[p]-=item.getLength();//update the remaining length of the new item added
remainingHeight[i]=0; //insert 0 to the remainingHeight of the item next to which we put the new item (so that we don't consider its remaining height anymore)
z+=item.getHeight(); //increment z by the height of the new packed item in the extreme point of height
return true;
}
public boolean put3D(ItemsUnit item, int p,int n) {
//------if x still did not exceed the container's length
if(x<length){
//--------if we can put the new item next to the item packed in the extreme point of length
if(putL(item,p)) {
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if y still did not exceed the container's breadth
if(y<breadth) {
x=remainingLength[p]; // update x to the position of the new item added
z=remainingHeight[p]; // update z to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of breadth
if(putB(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if z still did not exceed the container's height
if(z<height){
x=remainingLength[p]; // update x to the position of the new item added
y=remainingBreadth[p];// update y to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of height
if(putH(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
return false; //return false if item cannot be packed in neither extreme point
}
//-----------adding the new item to the extreme point in length
private boolean putL(ItemsUnit item, int p) {
double minRemL=remainingLength[0]; //the minimum remaining length of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingLength.length; j++){
if ((remainingLength[j] != 0) && (minRemL >= remainingLength[j]) && (remainingLength[j] >= item.getLength())){
i=j; //storing the item next to which we should put the new packed item
minRemL=remainingLength[j]; //minimum length left
}
}
remainingLength[p]=remainingLength[i]-item.getLength(); //update the remaining length of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight(); //update the remaining height of the new item added
remainingLength[i]=0; //insert 0 to the remainingLength of the item next to which we put the new item (so that we don't consider its remaining length anymore)
x+=item.getLength(); //increment x by the length of the new packed item in the extreme point of length
return true;
}
//-----------adding the new item to the extreme point in breadth
private boolean putB(ItemsUnit item, int p) {
double minRemB=remainingBreadth[0]; //the minimum remaining breadth of all already packed items
int i=0;//to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingBreadth.length; j++){
if ((remainingBreadth[j] != 0) && (minRemB >= remainingBreadth[j]) && (remainingBreadth[j] >= item.getBreadth())){
i=j; //choosing the item to which we should put the new packed item next to
minRemB=remainingBreadth[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingBreadth[p]=remainingBreadth[i]-item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight();//update the remaining height of the new item added
remainingLength[p]-=item.getLength(); //update the remaining length of the new item added
remainingBreadth[i]=0; //insert 0 to the remainingBreadth of the item next to which we put the new item (so that we don't consider its remaining breadth anymore)
y+=item.getBreadth(); //increment y by the breadth of the new packed item in the extreme point of breadth
//x=length-remainingLength[p]; //update x to the position of the item next to which we put the new item
return true;
}
//-----------adding the new item to the extreme point in height
private boolean putH(ItemsUnit item, int p) {
double minRemH=remainingHeight[0]; //the minimum remaining height of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingHeight.length; j++){
if ((remainingHeight[j] !=0 )&&(minRemH >= remainingHeight[j]) && (remainingHeight[j] >= item.getHeight())){
i=j; //choosing the item to which we should put the new packed item next to
minRemH=remainingHeight[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingHeight[p]=remainingHeight[i]-item.getHeight(); //update the remaining height of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingLength[p]-=item.getLength();//update the remaining length of the new item added
remainingHeight[i]=0; //insert 0 to the remainingHeight of the item next to which we put the new item (so that we don't consider its remaining height anymore)
z+=item.getHeight(); //increment z by the height of the new packed item in the extreme point of height
return true;
}
public boolean put3D(ItemsUnit item, int p,int n) {
//------if x still did not exceed the container's length
if(x<length){
z=remainingHeight[p]; // update z to the position of the new item added
y=remainingBreadth[p];// update y to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of length
if(putL(item,p)) {
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if y still did not exceed the container's breadth
if(y<breadth) {
x=remainingLength[p]; // update x to the position of the new item added
z=remainingHeight[p]; // update z to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of breadth
if(putB(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
//------if z still did not exceed the container's height
if(z<height){
x=remainingLength[p]; // update x to the position of the new item added
y=remainingBreadth[p];// update y to the position of the new item added
//--------if we can put the new item next to the item packed in the extreme point of height
if(putH(item,p)){
packedItems.add(item); // if item fits add it to the packedItems into the container
return true;
}
}
return false; //return false if item cannot be packed in neither extreme point
}
//-----------adding the new item to the extreme point in length
private boolean putL(ItemsUnit item, int p) {
double minRemL=remainingLength[0]; //the minimum remaining length of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingLength.length; j++){
if ((remainingLength[j] != 0) && (minRemL >= remainingLength[j]) && (remainingLength[j] >= item.getLength())){
i=j; //storing the item next to which we should put the new packed item
minRemL=remainingLength[j]; //minimum length left
}
}
remainingLength[p]=remainingLength[i]-item.getLength(); //update the remaining length of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight(); //update the remaining height of the new item added
remainingLength[i]=0; //insert 0 to the remainingLength of the item next to which we put the new item (so that we don't consider its remaining length anymore)
x+=item.getLength(); //increment x by the length of the new packed item in the extreme point of length
return true;
}
//-----------adding the new item to the extreme point in breadth
private boolean putB(ItemsUnit item, int p) {
double minRemB=remainingBreadth[0]; //the minimum remaining breadth of all already packed items
int i=0;//to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingBreadth.length; j++){
if ((remainingBreadth[j] != 0) && (minRemB >= remainingBreadth[j]) && (remainingBreadth[j] >= item.getBreadth())){
i=j; //choosing the item to which we should put the new packed item next to
minRemB=remainingBreadth[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingBreadth[p]=remainingBreadth[i]-item.getBreadth(); //update the remaining breadth of the new item added
remainingHeight[p]-=item.getHeight();//update the remaining height of the new item added
remainingLength[p]-=item.getLength(); //update the remaining length of the new item added
remainingBreadth[i]=0; //insert 0 to the remainingBreadth of the item next to which we put the new item (so that we don't consider its remaining breadth anymore)
y+=item.getBreadth(); //increment y by the breadth of the new packed item in the extreme point of breadth
//x=length-remainingLength[p]; //update x to the position of the item next to which we put the new item
return true;
}
//-----------adding the new item to the extreme point in height
private boolean putH(ItemsUnit item, int p) {
double minRemH=remainingHeight[0]; //the minimum remaining height of all already packed items
int i=0; //to store the index of the item next to which we should put the new item
//--------choosing the point (position) where to put the new item----
for (int j=0; j<remainingHeight.length; j++){
if ((remainingHeight[j] !=0 )&&(minRemH >= remainingHeight[j]) && (remainingHeight[j] >= item.getHeight())){
i=j; //choosing the item to which we should put the new packed item next to
minRemH=remainingHeight[j]; //minimum length left
}
/*else {
return false; //------return false if all the positions cannot fit the new item
}*/
}
remainingHeight[p]=remainingHeight[i]-item.getHeight(); //update the remaining height of the new item added
remainingBreadth[p]-=item.getBreadth(); //update the remaining breadth of the new item added
remainingLength[p]-=item.getLength();//update the remaining length of the new item added
remainingHeight[i]=0; //insert 0 to the remainingHeight of the item next to which we put the new item (so that we don't consider its remaining height anymore)
z+=item.getHeight(); //increment z by the height of the new packed item in the extreme point of height
return true;
}