JAVA. This project contains four parts. I\'ve completed the first 3 parts and ha
ID: 3806603 • Letter: J
Question
JAVA. This project contains four parts. I've completed the first 3 parts and had it checked over. Here is my code so far:
https://github.com/selango2017/Part-Project/tree/master
I need help with this 4th part. Any hints or suggestions is appreciated. Thank you.
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.
Explanation / Answer
PROGRAM CODE:
Part.java
package simple;
/**
* This class is an object that has 4 values in it:
* name - A textual name
* number - an alphanumeric sequence that may contain hyphens
* ncage - A five character alphanumeric code for a business of the form NNNNN
* niin - a 13 digit code of the form XXXX-XX-XXX-XXXX
*
* @version 0.3
*/
public class Part {
//member of the class
private String Name, Number, nCage, nIin;
//default constructor
public Part(String Name, String Number, String nCage, String nIin)
{
this.Name = Name;
this.Number = Number;
this.nCage = nCage;
this.nIin = nIin;
}
//assuming all parameters but nIin is given, default is called
public Part(String Name, String Number, String nCage)
{
this(Name, Number, nCage, null);
}
//assuming all parameters but nIin and nCage is given, default is called
public Part(String Name, String Number)
{
this(Name, Number, null, null);
}
//assuming only name is given, default is called
public Part(String Name)
{
this(Name, null, null, null);
}
//assuming NO parameters is given, default is called
public Part()
{
this(null, null, null, null);
}
//formatting toString method
@Override
public String toString()
{
return String.format("PART:%s,PIN:%s,NCAGE:%s,NIIN:%s", Name, Number, nCage, nIin);
}
//compares two objects of parts. two parts are only equal if they have the same number
//ncage and niin
public boolean equals(Part part)
{
super.equals(part);
if ((this.Number.equals(part.Number)) && (this.nCage.equals(part.nCage)) && (this.nIin.equals(part.nIin)))
{
return true;
}
else
return false;
}
//Accessor(getter) functions */
//gets name
public String getName()
{
return Name;
}
//gets number
public String getNumber()
{
return Number;
}
//gets five character alphanumeric code
public String getnCage()
{
return nCage;
}
//gets a 13 digit code
public String getnIin()
{
return nIin;
}
//Mutator(setter functions)
//method to set name
public void setName(String nameStr)
{
this.Name = nameStr;
}
//method to set Number
public void setNumber(String num)
{
this.Number = num;
}
//method to set cage
public void setnCage(String cage)
{
this.nCage = cage;
}
//method to set niin
public void setnIin(String niin)
{
this.nIin = niin;
}
/**
* prints a generic failure message
*/
//This will be an abstract method later
public void fail()
{
System.out.println("Something went wrong!");
}
}
PartTest.java
package simple;
/**
*This test class does basic testing on the Part object
*It calls the constructors and prints out members with accessors
* @author
* @version 0.2
*/
public class PartTest {
public static void main(String args[])
{
//create an object to Part class by passing initial values to constructor
Part partObj = new Part("Pen", "0B23F345", "AFT06", "1234-56-789-1234");
Part partObj2 = new Part("Eraser", "0B23F345","AFT06", "1234-56-789-1234");
//compares partObj2 and parobj
System.out.println(partObj2.equals(partObj));
//print the PartObj using toString() method
System.out.println("Initial values using constructor: " + partObj.toString());
//set values using setter func;tions
partObj.setName("Eraser");
partObj.setNumber("0DU1234");
partObj.setnIin("9084-12-753-6789");
//print the partObj using toString() method
System.out.println(" Values after using set functions: " + partObj.toString());
//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();
}
}
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.");
}
}
OUTPUT:
true
Initial values using constructor:
PART:Pen,PIN:0B23F345,NCAGE:AFT06,NIIN:1234-56-789-1234
Values after using set functions:
PART:Eraser,PIN:0DU1234,NCAGE:AFT06,NIIN:9084-12-753-6789
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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.