Design the class named Circle that extends the class GeometricObjects . The Circ
ID: 3735691 • Letter: D
Question
Design the class named Circle that extends the class GeometricObjects. The Circle class contains:
A double data field called radius , with default values of 1.0, to denote the radius of the circle
A no-arg constructor that creates a default circle.
A full-arg constructor that creates a circle with the specified radius, color and filled properties.
A getter and setter method to get the instance variable radius
A method named getPerimeter() that returns the perimeter of the circle. Perimeter of the circle can be calculated as 2*radius*3.14.
A method named getArea() that returns the area of the circle. Area of the circle can be calculated as 3.14*radius*radius.
A method named toString() that returns a String description of circle as follows:
super.toString() + “ Circle : radius = “ + radius + “ area is : “ + getArea() + “ perimeter is : “ + getPerimeter();
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CODE FOR GEOMETRIC OBJECTS:
public class GeometricObjects{
private String color;
private Boolean filled;
public GeometricObjects(){
this.color = "white";
this.filled = false;
}
/*Construct Geometric Object with specified color and filled value*/
public GeometricObjects(String color, boolean filled){
this.color = color;
this.filled = filled;
}
/* Return Color*/
public String getColor(){
return color;
}
/*Return filled. since filled is boolean we name it isFilled*/
public boolean isFilled(){
return filled;
}
/*Set new color*/
public void setColor(String color) {
this.color = color;
}
/*Set new filled*/
public void setFilled(boolean filled){
this.filled = filled;
}
/* toString method that returns the string representation of object. This method also fetches the values of color and filled--- i.e. works like a getter too*/
public String toString(){
return "Object color is: " + this.getColor() + " object filled is: " + this.isFilled() ;
}
}
Explanation / Answer
public class Circle extends GeometricObjects { private double radius; public Circle(double radius, String color, boolean filled) { super(color, filled); this.radius = radius; } public Circle() { super(); this.radius = 1; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double getPerimeter() { return 2 * Math.PI * radius; } public double getArea() { return Math.PI * radius * radius; } @Override public String toString() { return super.toString() + " object radius is " + radius + " object area is " + getArea() + " object perimeter is " + getPerimeter(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.