In: Computer Science
Create a class called Sphere. The class will contain the following
Instance data
double radius
Methods
Constructor with one parameter which will be used to set the radius instance data
Getter and Setter for radius
Area - calculate the area of the sphere (4 * PI * radius * radius)
Volume - calculate and return the volume of the sphere (4/3 * PIE * radius * radius * radius)
toString - returns a string with the description of the class which includes the radius.
The class with main will create two instances of the Sphere class, with one call the area and volume and toString methods and display all of the returned data. With the second object call the set method to change the radius to a different value than what was provided as an argument to the constructor. Use the get method to display the radius. Call the area, volume and toString for that instance and display the returned values.
class Sphere {
double radius;
public Sphere() {
}
/**
* @param r
*/
public Sphere(double r) {
radius = r;
}
/**
* @return
*/
public double getRadius() {
return radius;
}
public void setRadius(double aRadius) {
radius = aRadius;
}
public double getSurFaceArea() {
return 4 * Math.PI * radius *
radius;
}
public boolean checkRadius() {
return radius > 0;
}
public double getVolume() {
return 4.0 / 3 * Math.PI * (radius
* radius * radius);
}
}
public class TestSphere {
public static void main(String[] args) {
Sphere s1 = new Sphere(12);
System.out.println("Surface Area: "
+ s1.getSurFaceArea());
System.out.println("Volume: " +
s1.getVolume());
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me