Answer to Question #123903 in Java | JSP | JSF for Shouzab

Question #123903
Design an abstract class GeometricObject with lineColor as data member. GeometricObject must ensure that its children implement calcArea() method. Design Rectangle and Circle classes as children of GeometricObject class with overridden toString() method to return “ Rectangle with ‘w’ width and ‘h’ height is drawn” OR “Circle with ‘r’ radius is drawn”.
- The attribute of Rectangle are length and width
- The attribute of Circle is radius

*Hint:
Area of Circle= πr2
Area of Rectangle=w*l
1
Expert's answer
2020-06-29T08:20:31-0400
public static abstract class GeometricObject
{
    private String lineColor;
    public abstract double calcArea();

    public void setLineColor(String lineColor)
    {
        this.lineColor = lineColor;
    }
    public String getLineColor()
    {
        return lineColor;
    }
    public abstract String toString();
}

public static class Rectangle extends GeometricObject
{
    private double height;
    private double width;

    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;
    }


    @Override
    public double calcArea() {
        return width* height;
    }

    @Override
    public String toString() {
        return String.format( "Rectangle with %f width and %f height is drawn",width, height);
    }
}

public static class Circle extends GeometricObject
{
    private double radius;

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }


    @Override
    public double calcArea() {
        return Math.PI* Math.pow(radius,2);
    }

    @Override
    public String toString() {
        return String.format("Circle with %f radius is drawn",radius);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS