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 explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads in a sequence of strings from standard input using StdIn.readString() , and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. You may assume that 0 k N , where N is the number of string on standard input. The running time of the program must be linear in the size of...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
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];            ...
C Programming 1. Provide an equivalent of the java class in C class Example { public...
C Programming 1. Provide an equivalent of the java class in C class Example { public static int[][] question4(int n) { int[][] result = new int [n][n]; for(int i=1; i<=n; i+=1) for (int j=1; j<=n; j+=1) result[i][j] = (i*n)+j; return result; } public static void main(String[] args) { int[][] my array = question4(4); } } 2. Rewrite the following function using no loops, and only tail call recursion int min (int n, int[] input) { int i; int result; for...
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...
<<<<<<<< I need only the UML diagram for ALL classes.Java???????????? public class House {    private...
<<<<<<<< I need only the UML diagram for ALL classes.Java???????????? public class House {    private int houseNumber;    private int bedrooms;    private int sqFeet;    private int year;    private int cost;    public House(int houseNumber,int bedrooms,int sqFeet, int year, int cost)    {        this.houseNumber = houseNumber;        this.bedrooms = bedrooms;        this.sqFeet = sqFeet;        this.year = year;        this.cost = cost;    }    public int getHouseNumber()    {        return houseNumber;    }   ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT