JAVA Part 1: Develop the abstract class (GeoFigure.java) which will have the fol
ID: 3920077 • Letter: J
Question
JAVA
Part 1: Develop the abstract class (GeoFigure.java) which will have the following variables: height, width, figureType and area. The class should have the appropriate constructors, getters, setters, toSting and equals methods and an abstract method named area which will calculate the are of the figure.
Part 2: Develop an interface (SidedObject.java) containing a method displaySides() which displays the number of sides of an object.
Part 3: Develop two classes (Square.java and Triangle.java) which extend the class GeoFigure.java and implement the interface SidedObject.java.
Part 4: Develop an application (DemoGeoFigure.java) that demonstrates both subclasses.
Submit all 5 programs.
Explanation / Answer
public abstract class GeoFigure { private double height, width; private String figureType; private double area; public GeoFigure() { } public GeoFigure(double height, double width, String figureType) { this.height = height; this.width = width; this.figureType = figureType; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public String getFigureType() { return figureType; } public void setFigureType(String figureType) { this.figureType = figureType; } public double getArea() { return area; } public void setArea(double area) { this.area = area; } public abstract void area(); } public interface SidedObject { public void displaySides(); } public class Square extends GeoFigure implements SidedObject { public Square(double height, double width, String figureType) { super(height, width, figureType); } @Override public void area() { setArea(getWidth() * getHeight()); } @Override public void displaySides() { System.out.println("4 sides"); } } public class Triangle extends GeoFigure implements SidedObject { public Triangle(double height, double width, String figureType) { super(height, width, figureType); } @Override public void area() { setArea(0.5 * getHeight() * getWidth()); } @Override public void displaySides() { System.out.println("3 sides"); } } public class DemoGeoFigure { public static void main(String[] args) { Square square = new Square(10, 5, "Square"); square.displaySides(); square.area(); System.out.println("Square Area = " + square.getArea()); Triangle triangle = new Triangle(20, 9, "Triangle"); triangle.displaySides(); triangle.area(); System.out.println("Triangle Area = " + triangle.getArea()); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.