Hello, I need to correct this code with this feedback and add The inheritance semantics is incorrect
Question:
Hello, I need to correct this code with this feedback and add The inheritance semantics is incorrect because Triangle IS-NOT AreaCalculation but a Shape.
Likewise, Square IS-NOR AreaCalculation but a Shape.
So, may be a better hierarchy of classes would be Shape as superclass and Triangle and Square as its subclasses
Please add a different Exception to the program: Also please break down each class separate for better understanding with explanation as to what was done. Thank you
IllegalShapeDimensionArgumentException
public class IllegalShapeDimensionArgumentException extends IllegalArgumentException {
private String output;
public IllegalShapeDimensionArgumentException(double width, double height) {
if (width == 0 || height == 0)
output = "Value must be a non-zero.";
}
public String toString() {
return this.getClass().getSimpleName() + " : " + output;
}
}
AreaCalc
public class AreaCalc {
// These attributes are shared among the subclasses of this super (parent) class.
private double width, height;
// Overloading constructors based on if shape is straight-edged or curved.
public AreaCalc(double width, double height) {
if (width <= 0 || height <= 0) { // if width or height or both is invalid, throw exception
throw new IllegalShapeDimensionArgumentException(width, height);
}
this.width = width;
this.height = height;
}
// get methods
public double getHeight() {
return this.height;
}
public double getWidth() {
return this.width;
}
// set methods
public void setHeight(double height) {
this.height = height;
}
public void setWidht(double width) {
this.width = width;
}
// display methods with overriding.
@Override
public String toString() {
return "This shape has width " + this.width + " and height " + this.height + ".";
}
//This method is shared among the subclasses of this super class.
public double calcArea() {
return this.getWidth() * this.getHeight();
}
}
AreaOfShapes
class AreaOfShapes {
public static void main(String[] args) {
// Creating new object.
// Testing to see if exception can be caught.
try {
Square mySquare = new Square(4, 3);
// Invoking inheritance traits from super class to subclass.
System.out.println(mySquare.toString());
// Inserting white space between text.
System.out.println();
// Creating Triangle object.
Triangle myTriangle = new Triangle(4, 0);
// Invoking inheritance traits from super class to subclass.
System.out.println(myTriangle.toString());
}
catch (IllegalShapeDimensionArgumentException o) {
System.out.println(o.toString());
}
}
}
Triangle
public class Triangle extends AreaCalc {
//Giving unique attribute to Triangle subclass
double totalAreaT;
// constructor
public Triangle(double height, double width) {
super(height, width);
this.totalAreaT = this.calcArea() / 2;
}
// get method
public double getTotalAreaT() {
return this.totalAreaT;
}
// set method
public void setTotalAreaT(double totalAreaT) {
this.totalAreaT = totalAreaT;
}
// Override string method from parent class.
public String toString() { // unique display method, checks whether object is square
return "This shape has width " + this.getWidth() + " and height " + this.getHeight() + ".\nThis shape is a triangle with total area " + this.totalAreaT + ".";
}
}
Square
public class Square extends AreaCalc {
//Giving unique attribute to subclass Square.
private double totalArea; // shape's total area
// constructor
public Square(double height, double width) {
super(height, width);
this.totalArea = this.calcArea();
}
// get method
public double getTotalArea() {
return this.totalArea;
}
// set method
public void setTotalArea(double totalArea) {
this.totalArea = totalArea;
}
// Overriding string method from parent class.
public String toString() {
// unique display method, checks whether object is square
if (this.getHeight() == this.getWidth()) {
return "This shape has width " + this.getWidth() + " and height " + this.getHeight() + ".\nThis shape is a square with total area " + this.totalArea + ".";
}
else {
return "This shape has width " + this.getWidth() + " and height " + this.getHeight() + ".\nThis shape is a rectangle with total area " + this.totalArea + ".";
}
}
}
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill