File GeometricObject.java:
public abstract class GeometricObject {
protected String lineColor;
public GeometricObject() {
this.lineColor = "black";
}
protected GeometricObject(String lineColor) {
this.lineColor = lineColor;
}
// Subclasses should implement this method.
public abstract float calcArea();
}
File Circle.java:
public class Circle extends GeometricObject {
private int radius;
public Circle(int radius) {
this.radius = radius;
}
public Circle(int radius, String lineColor) {
super(lineColor);
this.radius = radius;
}
@Override
public float calcArea() {
return (float) (Math.PI * radius * radius);
}
@Override
public String toString() {
return "Circle with ‘" + radius + "’ radius is drawn";
}
}
File Rectangle.java:
public class Rectangle extends GeometricObject {
private int length;
private int width;
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
public Rectangle(int length, int width, String lineColor) {
super(lineColor);
this.length = length;
this.width = width;
}
@Override
public float calcArea() {
return width * length;
}
@Override
public String toString() {
return "Rectangle with ‘" + width + "’ width and ‘" + length + "’ height is drawn";
}
}
Comments
Dear Osama, the question was done according to initial requirements
Sir where is the main function of this program in main class that is a geometric object class how we call other classes.
Leave a comment