In: Computer Science
In java beginner coding language ,Write a class called Sphere that contains instance data that represents the sphere’s diameter. Define the Sphere constructor to accept and initialize the diameter, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface area of the sphere. Include a toString method that returns a one-line description of the sphere. Create a driver class called MultiSphere, whose main method instantiates and updates several Sphere objects.
class Sphere{
private double diameter;
public Sphere(double aDiameter) {
super();
diameter = aDiameter;
}
public double getDiameter() {
return diameter;
}
public void setDiameter(double aDiameter) {
diameter = aDiameter;
}
// returns the area using 4 / * PI r^2
public double getArea(){
double r = diameter/2;
return 4 * Math.PI * r * r;
}
// returns the volumes using 4 /3 * PI r^3
public double getVolume(){
double r = diameter/2;
return 4 / 2* Math.PI * r * r *
r;
}
public String toString(){
return "Area : "+getArea()+" Volume
: "+getVolume();
}
}
public class MultiSphere {
public static void main(String[] args) {
Sphere s1 = new Sphere(5);
Sphere s2 = new Sphere(7);
Sphere s3 = new Sphere(9);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}