223k views
2 votes
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.

User Eydrian
by
4.5k points

1 Answer

2 votes

Answer:

See explaination

Step-by-step explanation:

//Cylinder.java

public class Cylinder extends Circle {

private double height;

public Cylinder(double h, double r) {

super(r);

setHeight(h);

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

public boolean equals(Object o) {

if (this == o) return true;

if (!(o instanceof Cylinder)) return false;

Cylinder cylinder = (Cylinder) o;

return Double.compare(cylinder.height, height) == 0 && Double.compare(cylinder.radius, radius) == 0;

}

}

User Muqito
by
4.4k points