Write two subclasses of Shape called Circle and Rectangle, as shown in the class diagram.
a)    The Circle class contains:
·        An instance variable radius (double).
·        Three constructors as shown. The no-arg constructor initializes the radius to 1.0.
·        Getter and setter for the instance variable radius.
·        Methods getArea() and getPerimeter().
·        Override the inherited toString() method, to return "A Circle with radius=xxx, which is a subclass of yyy", where yyy is the output of the toString() method from the superclass.
import java.util.*;
abstract class Shape {
private String color;
private boolean filled;
public Shape() {
this.color = "green";
this.filled = true;
}
public Shape(String color, boolean filled) {
this.color = color;
this.filled = filled;
}
public abstract double getArea();
public abstract double getPerimeter();
public String toString() {
String filledStr = "filled";
if (!filled) {
filledStr = "Not filled";
}
return "A Shape with color of " + color + " and " + filledStr;
}
/**
* @return the color
*/
public String getColor() {
return color;
}
/**
* @param color the color to set
*/
public void setColor(String color) {
this.color = color;
}
/**
* @return the filled
*/
public boolean isFilled() {
return filled;
}
/**
* @param filled the filled to set
*/
public void setFilled(boolean filled) {
this.filled = filled;
}
}
class Circle extends Shape {
private double radius;
public Circle() {
this.radius = 1.0;
}
public Circle(double radius) {
this.radius = radius;
}
public Circle(double radius, String color, boolean filled) {
super(color, filled);
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
public String toString() {
return "A Circle with radius= " + radius + ", which is a subclass of " + super.toString();
}
/**
* @return the radius
*/
public double getRadius() {
return radius;
}
/**
* @param radius the radius to set
*/
public void setRadius(double radius) {
this.radius = radius;
}
}
class Rectangle extends Shape {
private double width;
private double length;
public Rectangle() {
this.width = 1.0;
this.length = 1.0;
}
public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}
public Rectangle(double width, double length, String color, boolean filled) {
super(color, filled);
this.width = width;
this.length = length;
}
@Override
public double getArea() {
return width * length;
}
@Override
public double getPerimeter() {
return 2 * (width + length);
}
public String toString() {
return "A Rectangle with width=" + width + " and length=" + length + ", which is a subclass of "
+ super.toString();
}
/**
* @return the width
*/
public double getWidth() {
return width;
}
/**
* @param width the width to set
*/
public void setWidth(double width) {
this.width = width;
}
/**
* @return the length
*/
public double getLength() {
return length;
}
/**
* @param length the length to set
*/
public void setLength(double length) {
this.length = length;
}
}
class App {
public static void main(String[] args) {
}
}
Comments
Leave a comment