Question
The language is Java, any help is appreciated, thank you very much in advance! WHERE TO START FROM: PROGRAM CODE: Part.java package simple; public class
The language is Java, any help is appreciated, thank you very much in advance!
WHERE TO START FROM:
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.
INSTRUCTIONS:
Make the Part in Part.java an abstract type, and remove the implementation of the Part.fail() method.
NEW: Add a new private final instance variable to the ConsumablePart called USES, which will hold the number of uses the ConsumablePart starts with.
Add a check for non-string values to the constructors and setters of the Part class. If the constructor notices a non-string, it should throw an IllegalArgumentException.
This may require other changes, because of the new throw statement? Can you see where or why? (Chapter 11)
What defines a "correct" input?
Name or Number: Name can be anything of type String
NCAGE: Any 5 character String (numbers or letters). Theoretically, it can't have any special characters, but we'll get to that when we start doing regular expressions in Chapter 14 or 15.
NIIN: Must be of the form NNNN-NN-NNN-NNNN. So for now, ensure that it's a String, and that it is 16 in length. We'll worry about the hyphen pattern with regular expressions later.
Make the required changes to PartTest.java to handle the Part objects polymorphicly.
Instead of instantiating Part objects, start creating ExpendableParts and ConsumableParts. If this requires you to create Constructor methods for these two classes, do so in their respective classes.
Add an instance variable to the ExpendablePart called toolsRequired, which is an ArrayList of Consumble parts.
Modify the ExpendablePart by adding a public void addTool() method that takes a ConsumbablePart and adds it to the toolsRequired instance variable.
Modify the ExpendablePart.fail() method to call the ConsumablePart.fail() method for each Consumable Part in its toolsRequiredArrayList.
Modify the ConsumablePart.fail() method. It now decrements the number of uses left for that part, but if the new number of uses isn't zero, it will not mention a cost to replace in the message. However, if the new number of uses is now zero, ensure that the cost to replace the ConsumablePart is in the fail method's message, and then reset the number of uses to USES.Examples of failure messages
Consumable Part XXXX has been used, and now has Y uses left.
Consumable Part WWWW has been used up, and will cost D.DD to replace.
Modify PartTest.java to run through the new functionality of the various Parts
Make a variable called system that is an ArrayList of ExpendablePart
Also, please Javadoc everything if possible, thanks.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started