Given a class called Circle in which there is a double type instance data called
ID: 3740109 • Letter: G
Question
Given a class called Circle in which there is a double type instance data called radius, a constructor that initializes the radius and a toString() returns the instance data. Write a class called Cone that is inherited from Circle with a constructor that will initialize its instance data, a method called computeVolume() in which it will compute the volume of cone using the formula as: 1/3*PI*radius * radius*height and return the result. It will also override toString() to return its instance data. Pay attention in code-reusability.
Explanation / Answer
public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double computeVolume() { return Math.PI * radius * radius; } @Override public String toString() { return "Circle of radius " + radius + " is has an area of " + computeVolume(); } } public class Cone extends Circle { private double height; public Cone(double radius, double height) { super(radius); this.height = height; } @Override public double computeVolume() { return super.computeVolume() * (1.0/3) * height; } @Override public String toString() { return "Cone of radius " + getRadius() + " and height " + height + " has an area of " + computeVolume(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.