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?
So I need to edit my project.
Here is my AssemblyLine class:
import java.util.Random;
public class AssemblyLine
{
private ManufacturedProduct elements[];
public AssemblyLine()
{
elements = new ManufacturedProduct[5];
for(int i=0;i {
elements[i] = null;
}
}
public ManufacturedProduct insert(ManufacturedProduct prod)
{
ManufacturedProduct result = null;
if(elements[0] != null)
{
result = elements[4];
for(int i=elements.length-1; i>0 ; i--)
{
elements[i] = elements[i-1];
}
}
elements[0] = prod;
return result;
}
}
Here is my ManufacturedProduct class:
import java.util.Random;
public class ManufacturedProduct
{
private int ID;
private boolean passedInspection;
public ManufacturedProduct(int ID)
{
this.ID = ID;
passedInspection = false;
}
public int getProductId()
{
return ID;
}
public void inspection()
{
Random random = new Random();
int value = random.nextInt(20);
if(value == 0)
{
passedInspection = false;
}
else
{
passedInspection = true;
}
}
public String toString()
{
return "Product ID: "+ID+" Inspection passed:
"+passedInspection;
}
}
Here is my AssemblyLineTest driver class:
public class AssemblyLineTest {
public static void main(String[] args) {
AssemblyLine line = new AssemblyLine();
ManufacturedProduct prod, result;
for(int i=1;i<=20;i++)
{
prod = new ManufacturedProduct(i);
result = line.insert(prod);
if(result != null)
{
result.inspection();
System.out.println(result);
}
}
}
}
The code is present below in code blocks. Three different classes are present below. I have added a new method in AssemblyLine to remove a product which could be present in between.
public class AssemblyLineTest {
public static void main(String[] args) {
AssemblyLine line = new AssemblyLine();
ManufacturedProduct prod, result;
for(int i=1;i<=20;i++)
{
prod = new ManufacturedProduct(i);
result = line.insert(prod);
if(result != null)
{
result.inspection();
System.out.println(result);
}
}
System.out.println(line.remove(new ManufacturedProduct(16)));
System.out.println(line.remove(new ManufacturedProduct(5)));
}
}
import java.util.Objects;
import java.util.Random;
public class ManufacturedProduct
{
private int ID;
private boolean passedInspection;
public ManufacturedProduct(int ID)
{
this.ID = ID;
passedInspection = false;
}
public int getProductId()
{
return ID;
}
public void inspection()
{
Random random = new Random();
int value = random.nextInt(20);
if(value == 0)
{
passedInspection = false;
}
else
{
passedInspection = true;
}
}
public String toString()
{
return "Product ID: "+ID+" Inspection passed: "+passedInspection;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ManufacturedProduct that = (ManufacturedProduct) o;
return ID == that.ID &&
passedInspection == that.passedInspection;
}
}
import java.util.Random;
public class AssemblyLine
{
private ManufacturedProduct elements[];
public AssemblyLine()
{
elements = new ManufacturedProduct[5];
for(int i=0;i <5; ++i) {
elements[i] = null;
}
}
public ManufacturedProduct insert(ManufacturedProduct prod)
{
ManufacturedProduct result = null;
if(elements[0] != null)
{
result = elements[4];
for(int i=elements.length-1; i>0 ; i--)
{
elements[i] = elements[i-1];
}
}
elements[0] = prod;
return result;
}
public ManufacturedProduct remove(ManufacturedProduct product) {
int index = -1;
// no value is present.
if(elements[0] != null) {
// find the element in the array.
for(int i = 0; i < 5; ++i) {
if(elements[i].equals(product)) {
index = i;
}
}
// -1, in case no value is found.
if(index == -1) {
return null;
}
// shift the values left.
for(int i = index;i < 4; ++i) {
elements[i] = elements[i+1];
}
return product;
}
return null;
}
}
Screenshot of the code working.
Get Answers For Free
Most questions answered within 1 hours.