Any help is appreciated, language is Java. Thanks INSTRUCTIONS: Add a public met
ID: 3806445 • Letter: A
Question
Any help is appreciated, language is Java. Thanks
INSTRUCTIONS:
Add a public method to Part called fail() that will print out a generic failure message, identifying the part that has failed. fail() should return void. Add a comment to the Part.fail() method that mentions that this method will be abstract at some point soon.
Extend class Part with two classes in accordance with the descriptions below:
ExpendablePart
add a private instance variable of type int called failureRate, which will hold the average number of failures per operational hour of the part.
add a private instance variable of the type int called leadTime, which will hold the number of days it takes to replace the part in the supply system.
ConsumablePart (make picture)
add a private instance variable of type double called replacementCost, which is (you ready??) the cost to replace this item when it's used up
add a private instance variable of type int called usesLeft, which is the number of times the ConsumablePart can be used before it must be replaced.
Override fail() in both ExpendablePart and ConsumablePart
In ExpendablePart.fail(), the message (in addition to identifying the part) should include that the failure was an expendable part, and that it will take leadTime days to replace the part
In ConsumablePart.fail(), the message (in addition to identifying the part) should include that the failure was a consumable part, and that it will cost replacementCost dollars to replace the part.
WHERE TO START FROM:
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; }
}
/**
* The java test class PartTest that tests the toString
* and equals methods and print the results to console.
* */
//PartTest.java
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.");
}
}//end of class PartTest
ALSO, PLEASE JAVADOC IF YOU CAN. THANK YOU
Explanation / Answer
PROGRAM CODE:
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. It 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. It 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:
Name : Last, First , Number : AX-34R, NCAGE : MN34R, NIIN: ABCD-RF-WDE-KLJM
Name : Widget, purple , Number : 12345, NCAGE : OU812, NIIN: 1234-12-123-1234
part1 and part2 are not equal.
This failure is because of expendable part.
It will take 20 days to replace the part.
This failure is because of consumable part.
It will cost 23.0 dollars to replace the part.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.