Write your answer in the space provided. 11) Write the definition for a child cl
ID: 3737389 • Letter: W
Question
Write your answer in the space provided. 11) Write the definition for a child class of Circle named Cylinder. The cylinder should have a double attribute named height. The constructor of the child class should take parameters to initialize the attributes. The class Cylinder should also include getter and setter method as well as an equals method that returns true if two cylinder objects have same dimensions and false otherwise. Remember to override method where needed. public class Circle protected double radius; public Circle (double r) radius r public void setRadius (double r) radius r public double getRadius) ( return radius public double area) return Math.PI radius * radius; public String tostring) return "Circle:Radius radius; IExplanation / Answer
Cylinder.java
public class Cylinder extends Circle{
private double height;
public Cylinder(double r, double h) {
super(r);
height = h;
}
public void setHeight(double h) {
height = h;
}
public double getHeight() {
return height;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Cylinder other = (Cylinder) obj;
if (Double.doubleToLongBits(height) != Double
.doubleToLongBits(other.height))
return false;
return true;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.