Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: The AssemblyLine class has a potential problem. Since the only way you

ID: 3912744 • Letter: Q

Question

Question:

The AssemblyLine class has a potential problem. Since the only way you can remove an object from the AssemblyLine array is when the insert method returns an object from the last element of the AssemblyLine's encapsulated array, what about those ManufacturedProduct objects that are "left hanging" in the array because they never got to the last element position? How do we get them out?

Lab 4B is to modify your project so you can deal with the above problem and return those objects.

Code:

public class AssemblyLine

{

ManufacturedProduct products[],store,inspectionVariable; //here I have taken a new variable to store the dequeued value.

int maxSize;

public AssemblyLine()

{

products= new ManufacturedProduct[5];

maxSize=0;

}

public void insert(ManufacturedProduct p){

inspectionVariable = p;

int rear=maxSize;

if(maxSize==5){

store=products[4];

}

System.out.print("printed array ");

while(rear>0)

{

if(rear==5)

rear--;

products[rear]=products[--rear];

System.out.print(products[rear].getProductId()+" ");

}

products[0]=p;

System.out.print(products[0].getProductId());

if(maxSize==5) {

System.out.print(", popped element : "+store.getProductId()+", "); //returned the dequeued object

System.out.println("inspection : "+inspectionVariable.inspection());

return;

}

else {

maxSize++;

System.out.print(" popped element : null, ");

System.out.println("inspection : "+inspectionVariable.inspection());

return ;

}

}

}

public class AssemblyLineTest
{
public static void main(String a[])
{
//KandelHeading.getHeader("Lab 4B"); //using my header
AssemblyLine l = new AssemblyLine();
l.insert(new ManufacturedProduct(1));
l.insert(new ManufacturedProduct(2));
l.insert(new ManufacturedProduct(3));
l.insert(new ManufacturedProduct(4));
l.insert(new ManufacturedProduct(5));
l.insert(new ManufacturedProduct(6));
l.insert(new ManufacturedProduct(7));
l.insert(new ManufacturedProduct(8));
l.insert(new ManufacturedProduct(9));
l.insert(new ManufacturedProduct(10));
l.insert(new ManufacturedProduct(11));
l.insert(new ManufacturedProduct(12));
l.insert(new ManufacturedProduct(13));
l.insert(new ManufacturedProduct(14));
l.insert(new ManufacturedProduct(15));
l.insert(new ManufacturedProduct(16));
l.insert(new ManufacturedProduct(17));
l.insert(new ManufacturedProduct(18));
l.insert(new ManufacturedProduct(19));
l.insert(new ManufacturedProduct(20));
}
}


import java.util.*;

public class ManufacturedProduct
{
private int productId;
private boolean passedInspection;


public ManufacturedProduct(int id)
{
productId=id;  
passedInspection= true;
}

int getProductId()
{
return productId;
}

public String inspection()
{
Random rand = new Random();
int n = rand.nextInt(19);
  
if(n==0) passedInspection=false;

return productId+" "+passedInspection;
  
}

}

Explanation / Answer

Assembly line class is modified such that everytime a value is added to products array it will be inserted as well as returned .so while inserting all the values each and every value will be returned therby solving the hanging objects problem .

package cheggjuly;

public class AssemblyLine

{

ManufacturedProduct products[];

int n;

public AssemblyLine()

{

products= new ManufacturedProduct[5];

n=0;

}

public ManufacturedProduct insert(ManufacturedProduct p)

{

int x=n;

  

while(x>0)

{

products[x]=products[--x];

}

products[0]=p;

n++;

ManufacturedProduct t= products[x];

if(t!=null) {

t.inspection();

}

return t;

}

//--------------------------------------------------------------

}

Tester:

package cheggjuly;

public class AssemblyLineTest
{
public static void main(String a[])
{
AssemblyLine l = new AssemblyLine();
System.out.println(l.insert(new ManufacturedProduct(1)));
System.out.println(l.insert(new ManufacturedProduct(2)));
System.out.println(l.insert(new ManufacturedProduct(3)));
System.out.println(l.insert(new ManufacturedProduct(4)));
System.out.println(l.insert(new ManufacturedProduct(5)));

  
  
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote