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

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; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!