Question

The AssemblyLine class has a potential problem. Since the only way you can remove an object...

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

}

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
In Java is there a way I can put a value in a linkList = array[...
In Java is there a way I can put a value in a linkList = array[ ] string (I don't know how to word it here what I have so far with my code) * Example object 51 I want to equal Array [0]; // Project code below public String getCardRank() { String values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; return "RRR"; } // this part is in another .java file private...
By JAVA programming language public void expand(Object a) {     // assume element has enough capacity...
By JAVA programming language public void expand(Object a) {     // assume element has enough capacity     for (int i = size - 1; i >= 0; i--) {         element[4 * i + 3] = a;         element[4 * i + 2] = a;         element[4 * i + 1] = a;         element[4 * i] = element[i];      }      size = 4 * size; } element is a one-dimensional array that stores elements of the type Object....
Given the following code: public void expand(Object a) {     // assume element has enough capacity...
Given the following code: public void expand(Object a) {     // assume element has enough capacity     for (int i = size - 1; i >= 0; i--) {         element[4 * i + 3] = a;         element[4 * i + 2] = a;         element[4 * i + 1] = a;         element[4 * i] = element[i];      }      size = 4 * size; } element is a one-dimensional array that stores elements of the type Object....
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
In Java Let’s say we’re designing a Student class that has two data fields. One data...
In Java Let’s say we’re designing a Student class that has two data fields. One data field, called id, is for storing each student’s ID number. Another data field, called numStudents, keeps track of how many Student objects have been created. For each of the blanks, indicate the appropriate choice. id should be   public/private   and   static/not static . numStudents should be   public/private   and   static/not static . The next three questions use the following class: class Counter {     private int...
So I would like to multiply a first digit by 2. I have to randomize either...
So I would like to multiply a first digit by 2. I have to randomize either 51-55 but multiply that first digit by 2 whichever the number may come out to be. I also need to keep the string. I need this to be in JAVA but so far Here is my code: import java.util.Random; public class Randoms {    public static void main(String[] args) {    Random rng = new Random();    String x;    int max = 55;...
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial Scores: 10, 20, 30, 40 Scores after loop: 30, 50, 70, 40 Import. java.util.Scanner; public class StudentScores { public static void main (String [] args){ Scanner scnr = new Scanner (System.in); final int SCORES_SIZE = 4; int [] bonusScores = new int[SCORES_SIZE]; int...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT