Can someone help me sovle this problem? Problem #3: Clothes (40 Points) For this
ID: 3711768 • Letter: C
Question
Can someone help me sovle this problem?
Problem #3: Clothes (40 Points)
For this problem, you will be creating a derived class, exception class and an interface. No Driver is required, but I have attached one to test with. I have also attached a base class for you to use. A UML diagram for this problem has been provided to assist you.
Jacket Class: Jacket should be its own class and should inherit from Clothing and implement Insulatable. Jacket should have:
Attribute:
Thickness of Jacket
Methods:
get and set methods for attribute
toString(): returns a String describing the Jacket
constructor that takes in the color of the Jacket
constructor that takes in the color of the Jacket and the thickness of the Jacket
insulateRating(): returns 12.3 * thickness of the Jacket
------------------------------------------------------------------
Insulatable Interface: Insulatable should be its own interface which has a:
Method header:
insulateRating(): returns the rating for how well the Insulatable object insulates
------------------------------------------------------------------
NegativeThicknessException: This exception class should:
have a default constructor which set's it own message which relates to the problem the exception is warning us about
have a constructor which takes in a String as input and append that String to the exception's own message (which relates to the problem the exception is warning us about)
be thrown by the method in Jacket where we are trying to set the thickness of the Jacket. The exception should only be thrown if there is an attempt to set the thickness of the Jacket to a negative value
Grading Breakdown:
10 -- Correctly creating your Interface
10 -- Correctly creating your Derived Object
10 -- Correctly creating your Exception
10 -- Correctly throwing your Exception
Explanation / Answer
I cannot see any base class and UML diagram but going with Clothing as base class based on the understanding of the question.
public class Clothing {
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
return "Clothing [color=" + color + "]";
}
public Clothing(String color) {
this.color = color;
}
public Clothing() {
super();
}
}
public interface Insulatable {
public double insulateRating();
}
public class Jacket extends Clothing implements Insulatable{
private double thickness;
public double getThickness() {
return thickness;
}
public void setThickness(double thickness) throws NegativeThicknessException {
if(thickness>0) {
this.thickness = thickness;
}
else {
throw new NegativeThicknessException("Cannot set negative value to thickness");
}
}
public String toString() {
return "Jacket [thickness=" + thickness + "]";
}
public Jacket(String color, double thickness) throws NegativeThicknessException {
super(color);
setThickness(thickness);
}
public Jacket(double thickness) throws NegativeThicknessException {
setThickness(thickness);
}
public double insulateRating() {
return (12.5*thickness);
}
}
public class NegativeThicknessException extends Exception{
NegativeThicknessException(){
System.out.println("Sorry... Negative thickness is invalid !");
}
NegativeThicknessException(String message){
System.out.println(message);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.