Question

Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

Language is Java, any help is appreciated. Thank you!

WHERE TO START FROM:

import java.util.ArrayList;

//PartTest.java
//package simple;
public class PartTest
{
public static void main(String[] args)
{
ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();  
// creating Part Object
Part part1 = new ExpendablePart("Last, First");
part1.setNumber("AX-34R");
part1.setNcage("MN34R");
part1.setNiin("ABCD-RF-WDE-KLJM");
// printing information
System.out.println(part1.toString());
  
  
//Create a part2 object of class Part
Part part2=new ConsumablePart("Widget, purple");
part2.setNumber("12345");
part2.setNcage("OU812");
part2.setNiin("1234-12-123-1234");
  
// printing information
System.out.println(part2.toString());
  
//checking equality of two Part class objects
if(part1.equals(part2))
System.out.println("part1 and part2 are equal.");
else
System.out.println("part1 and part2 are not equal.");
//testing expendable part
ExpendablePart expPart = new ExpendablePart();
expPart.setFailureRate(3);
expPart.setLeadTime(20);

//testing consumable part
ConsumablePart conPart = new ConsumablePart();
conPart.setReplacementCost(23);
conPart.setUsesLeft(6);

//adding to expPart and calling fail method
expPart.addTool(conPart);
expPart.fail();

}
}//end of class PartTest

--------------------------------------------------

import java.util.ArrayList;

//ExpendablePart.java
//package simple;
public class ExpendablePart extends Part{
  
private int failureRate; //holds the average number of failures per operational hour of the part
private int leadTime; //holds the number of days it takes to replace the part in the supply system
  
private ArrayList<ConsumablePart> toolsRequired=new ArrayList<ConsumablePart>();
public ExpendablePart()
{
     
}

public ExpendablePart(String string) {
   // TODO Auto-generated constructor stub
   super(string);
}

public void addTool(ConsumablePart c)
{
   toolsRequired.add(c);
}

/**
*
* @return failure rate
*/
public int getFailureRate() {
return failureRate;
}
/**
*
* @param failureRate
*/
public void setFailureRate(int failureRate) {
this.failureRate = failureRate;
}
/**
*
* @return lead time
*/
public int getLeadTime() {
return leadTime;
}
/**
*
* @param leadTime
*/
public void setLeadTime(int leadTime) {
this.leadTime = leadTime;
}
/**
* Prints the failure reason and time to replace the part
*/
@Override
public void fail() {
  
for(ConsumablePart c:toolsRequired)
{
     
   c.fail();
}
}}

--------------------------------------------

//ConsumablePart.java

public class ConsumablePart extends Part{
private double replacementCost;
private int usesLeft;
  
private static final int USES=10;


public ConsumablePart() {
       // TODO Auto-generated constructor stub
   }

public ConsumablePart(String string) {
   // TODO Auto-generated constructor stub
super(string);
}
/**
*
* @return replacement cost
*/
public double getReplacementCost() {
return replacementCost;
}
/**
* sets the replacement cost
* @param replacementCost
*/
public void setReplacementCost(double replacementCost) {
this.replacementCost = replacementCost;
}
/**
*
* @return uses left
*/
public int getUsesLeft() {
return usesLeft;
}
/**
* sets the uses left
* @param usesLeft
*/
public void setUsesLeft(int usesLeft) {
this.usesLeft = usesLeft;
}
/**
* Prints the failure reason and time to replace the part
*/
@Override
public void fail() {
usesLeft--;

if(usesLeft!=0)
{
System.out.println(" consumable part "+getName()+" has been used and now has "+usesLeft+" uses left");

}
else
{
     
System.out.println(String.format(" consumable part %s has been used up and will cost %.2f",getName(),replacementCost));

usesLeft=USES;
}

}
}

--------------------------------------------

//Part.java

package simple;

public class Part

{

private String name;

private String number; // this is sequence of alphanumeric

private String ncage; // this is also 5 character sequence

private String niin; // 13 character code

/**

* Default constructor

*/

public Part()

{

this("", "", "", ""); // calling full argument constructor

}

/**

* @param name

*/

public Part(String name)

{

this(name, "", "", ""); // calling full argument constructor

}

/**

* @param name

* @param number

* @param ncage

* @param niin

*/

public Part(String name, String number, String ncage, String niin)

{

this.name = name;

this.number = number;

this.ncage = ncage;

this.niin = niin;

}

  

/**

* The method toString that returns the string

* representation of the instance variables of the part

* object as string .

* */

public String toString()

{

return String.format("Name : %s , Number : %s, NCAGE : %s, NIIN: %s", name,number,ncage,niin);

}

  

  

/**

* The method equals takes an Object type

* and typecast the object to part object

* and returns true if number,ncage and niin

* are equal .otherwise returns false.

* */

public boolean equals(Object obj)

{

Part other=(Part)obj;

return number.equals(other.getName())

&& ncage.equals(other.getNcage())

&&niin.equals(other.getNiin());

}

/**

* @param name

*/

public void setName(String name) { this.name = name; }

/**

* @return name

*/

public String getName() { return name; }

/**

* @param number

*/

public void setNumber(String number) { this.number = number; }

/**

* @return number

*/

public String getNumber() { return number; }

/**

* @param ncage

*/

public void setNcage(String ncage) { this.ncage = ncage; }

/**

* @return ncage

*/

public String getNcage() { return ncage; }

/**

* @param niin

*/

public void setNiin(String niin) { this.niin = niin; }

/**

* @return niin

*/

public String getNiin() { return niin; }

/**

* prints a generic failure message

*/

//This will be an abstract method later

public void fail()

{

System.out.println("Something went wrong!");

}

}

/**

* The java test class PartTest that tests the toString

* and equals methods and print the results to console.

* */

INSTRUCTIONS:

Make changes as follows:

Add a regular expression-based check to ensure that the input to setNIIN() and the full argument constructors for all Parts (which is every Class in the project) is correct.

Remember, a NIIN must be of the form NNNN-NN-NNN-NNNN, where each N is a digit ( e.g. a 0,1,2,3,4,5,6,7,8, or 9)

Add a regular expression-based check to ensure that the input to setNcage() and the full argument constructors for all Parts (which is every Class in the project) is correct.

Remember, a NCAGE must be of the form NNNNN, where each N is a number or capital letter

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Part.java

package simple;

public class Part

{

private String name;

private String number; // this is sequence of alphanumeric

private String ncage; // this is also 5 character sequence

private String niin; // 13 character code

/**

* Default constructor

*/

public Part()

{

this("", "", "", ""); // calling full argument constructor

}

/**

* @param name

*/

public Part(String name)

{

this(name, "", "", ""); // calling full argument constructor

}

/**

* @param name

* @param number

* @param ncage

* @param niin

*/

public Part(String name, String number, String ncage, String niin)

{

this.name = name;

this.number = number;

this.ncage = ncage;

this.niin = niin;

}

  

/**

* The method toString that returns the string

* representation of the instance variables of the part

* object as string .

* */

public String toString()

{

return String.format("Name : %s , Number : %s, NCAGE : %s, NIIN: %s", name,number,ncage,niin);

}

  

  

/**

* The method equals takes an Object type

* and typecast the objt to part object

* and returns true if number,ncatge and niin

* are equal .otherwise returns false.

* */

public boolean equals(Object obj)

{

Part other=(Part)obj;

return number.equals(other.getName())

&& ncage.equals(other.getNcage())

&&niin.equals(other.getNiin());

}

/**

* @param name

*/

public void setName(String name) { this.name = name; }

/**

* @return name

*/

public String getName() { return name; }

/**

* @param number

*/

public void setNumber(String number) { this.number = number; }

/**

* @return number

*/

public String getNumber() { return number; }

/**

* @param ncage

*/

public void setNcage(String ncage) { this.ncage = ncage; }

/**

* @return ncage

*/

public String getNcage() { return ncage; }

/**

* @param niin

*/

public void setNiin(String niin) { this.niin = niin; }

/**

* @return niin

*/

public String getNiin() { return niin; }

/**

* prints a generic failure message

*/

//This will be an abstract method later

public void fail()

{

System.out.println("Something went wrong!");

}

}

/**

* The java test class PartTest that tests the toString

* and equals methods and print the results to console.

* */

//ExpendablePart.java

package simple;

public class ExpendablePart extends Part{

  

private int failureRate; //holds the average number of failures per operational hour of the part

private int leadTime; //holds the number of days it takes to replace the part in the supply system

  

/**

*
* @return failure rate

*/

public int getFailureRate() {

return failureRate;

}

/**

*

* @param failureRate

*/

public void setFailureRate(int failureRate) {

this.failureRate = failureRate;

}

/**

*

* @return lead time

*/

public int getLeadTime() {

return leadTime;

}

/**

*

* @param leadTime

*/

public void setLeadTime(int leadTime) {

this.leadTime = leadTime;

}

/**

* Prints the failure reason and time to replace the part

*/

@Override

public void fail() {

System.out.println("This failure is because of expendable part.\nIt will take "

+ leadTime + " days to replace the part.");

}

}

//ConsumablePart.java

package simple;

public class ConsumablePart extends Part{

private double replacementCost;

private int usesLeft;

  

/**

*

* @return replacement cost

*/

public double getReplacementCost() {

return replacementCost;

}

/**

* sets the replacement cost

* @param replacementCost

*/

public void setReplacementCost(double replacementCost) {

this.replacementCost = replacementCost;

}

/**

*

* @return uses left

*/

public int getUsesLeft() {

return usesLeft;

}

/**

* sets the uses left

* @param usesLeft

*/

public void setUsesLeft(int usesLeft) {

this.usesLeft = usesLeft;

}

/**

* Prints the failure reason and time to replace the part

*/

@Override

public void fail() {

System.out.println("This failure is because of consumable part.\nIt will cost "

+ replacementCost + " dollars to replace the part.");

}

}

//PartTest.java

package simple;

public class PartTest

{

public static void main(String[] args)

{

// creating Part Object

Part part1 = new Part("Last, First");

part1.setNumber("AX-34R");

part1.setNcage("MN34R");

part1.setNiin("ABCD-RF-WDE-KLJM");

// printing information

System.out.println(part1.toString());

  

  

//Create a part2 object of class Part

Part part2=new Part("Widget, purple");

part2.setNumber("12345");

part2.setNcage("OU812");

part2.setNiin("1234-12-123-1234");

  

// printing information

System.out.println(part2.toString());

  

//checking equality of two Part class objects

if(part1.equals(part2))

System.out.println("part1 and part2 are equal.");

else

System.out.println("part1 and part2 are not equal.");

//testing expendable part

ExpendablePart expPart = new ExpendablePart();

expPart.setFailureRate(3);

expPart.setLeadTime(20);

expPart.fail();

//testing consumable part

ConsumablePart conPart = new ConsumablePart();

conPart.setReplacementCost(23);

conPart.setUsesLeft(6);

conPart.fail();

}

}//end of class PartTest

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in...

    Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author fenghui */ public class Course {     private String cName;     private ArrayList<Subject> cores;     private ArrayList<Major> majors;     private ArrayList<Subject> electives;     private int cCredit;          public Course(String n, int cc){         cName = n;         cCredit = cc;         cores = new ArrayList<Subject>(0);         majors =...

  • Below are the Car class and Dealership class that I created In the previous lab import...

    Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...

  • JAVA 3files seprate 1.Classroom.java 2.ClassroomTester.java (provided) 3.Student.java(provided) Use the following files: ClassroomTester.java import java.util.ArrayList; /** *...

    JAVA 3files seprate 1.Classroom.java 2.ClassroomTester.java (provided) 3.Student.java(provided) Use the following files: ClassroomTester.java import java.util.ArrayList; /** * Write a description of class UniversityTester here. * * @author (your name) * @version (a version number or a date) */ public class ClassroomTester { public static void main(String[] args) { ArrayList<Double> grades1 = new ArrayList<>(); grades1.add(82.0); grades1.add(91.5); grades1.add(85.0); Student student1 = new Student("Srivani", grades1); ArrayList<Double> grades2 = new ArrayList<>(); grades2.add(95.0); grades2.add(87.0); grades2.add(99.0); grades2.add(100.0); Student student2 = new Student("Carlos", grades2); ArrayList<Double> grades3 = new...

  • CS HELP Use sets to solve this problem package log; import java.io.IOException; import java.io.Reader; import java.util.ArrayList;...

    CS HELP Use sets to solve this problem package log; import java.io.IOException; import java.io.Reader; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class LogParser { /** * Returns a list of SuspectEntries corresponding to the CSV data supplied by the given Reader. * * The data contains one or more lines of the format: * * Marc,413-545-3061,1234567890 * * representing a name, phone number, and passport number. * * @param r an open Reader object * @return a list of SuspectEntries...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

  • Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so...

    Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so they throw exceptions when the following errors occur. - The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. - The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. - The ProductionWorker class should thrown anexception named InvalidPayRate when it receives a negative number...

  • java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

    java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date {    private String month;    private String day;    private String year;    public Date(String month, String day, String year) {       this.month = month;       this.day = day;       this.year = year;    }    public String getMonth() {       return month;    }    public String getDay() {       return day;    }    public String...

  • In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams...

    In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams and print it to the terminal via the driver class: import java.util.*; public class Racer implements Comparable { private final String name; private final int year; private final int topSpeed;    public Racer(String name, int year, int topSpeed){    this.name = name; this.year = year; this.topSpeed = topSpeed;    }    public String toString(){    return name + "-" + year + ", Top...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT