Question: Using Java in netbeans The AssemblyLine class has a potential problem. Since the only way you can remove an object from the AssemblyLine array is
Using Java in netbeans
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:
Manufactured Product Class:
import java.util.Random;
public class ManufacturedProduct {
private int productId;
private boolean passedInspection;
// Constructor
public ManufacturedProduct(int productId) {
this.productId = productId;
// Set a default value for inspection (true or false)
this.passedInspection = inspectProduct();
}
// Getter for productId
public int getProductId() {
return productId;
}
// Inspection method
public void inspection() {
this.passedInspection = inspectProduct();
}
// toString method
@Override
public String toString() {
return "ManufacturedProduct{" +
"productId=" + productId +
", passedInspection=" + passedInspection +
'}';
}
// Private method to simulate inspection
private boolean inspectProduct() {
Random random = new Random();
int randomNumber = random.nextInt(20);
return randomNumber != 0;
}
}
AssemblyLine Class:
public class AssemblyLine {
private ManufacturedProduct[] assemblyLineArray;
// Constructor
public AssemblyLine() {
this.assemblyLineArray = new ManufacturedProduct[5];
}
// Insert method
public ManufacturedProduct insert(ManufacturedProduct newProduct) {
// Shift elements up
for (int i = assemblyLineArray.length - 1; i > 0; i--) {
assemblyLineArray[i] = assemblyLineArray[i - 1];
}
// Add the new product to the first position
assemblyLineArray[0] = newProduct;
// Return the last element of the array (element 4)
return assemblyLineArray[assemblyLineArray.length - 1];
}
}
AssemblyLineTest Class:
public class AssemblyLineTest {
public static void main(String[] args) {
AssemblyLine assemblyLine = new AssemblyLine();
// Insert 20 ManufacturedProduct objects into the AssemblyLine
for (int i = 1; i <= 20; i++) {
ManufacturedProduct product = new ManufacturedProduct(i);
assemblyLine.insert(product);
// Display a report on each object
System.out.println("Report for ManufacturedProduct #" + i + ":");
System.out.println(product);
System.out.println("--------------");
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
