1. Design and implement a class called Sphere that contains instance data that r
ID: 3528104 • Letter: 1
Question
1. Design and implement a class called Sphere that contains instance data that represents the sphere's diameter. ? Define the Sphere constructor to accept and initialize the diameter, ? include getter and setter methods for the diameter. ? Include methods that calculate and return the volume and surface area of the sphere ? Create a driver class called MultiSphere, whose main method instantiates and updates several Sphere objects. You should implement the following methods: ? public Sphere(double theDiameter) ? public void setDiameter(double theDiameter) ? public double getDiameter() ? public double getVolume() ? public double getSurfaceArea() ? public String toString()Explanation / Answer
If I succeeded in helping you then please rate me 5 stars first (before you rate anyone else)
class Sphere {
// what does a sphere need to have to define it ?
// a Diameter so lets define it
double diameter;
// now we need to define a constructor to define our sphere object
Sphere(double d) {
diameter = d;
}
// a getter method to get the Sphere diameter
double getDiameter() {
return diameter;
}
// now that our spere exists lets have methods to get Volume and Area
// Volume: formula if I remember well from my high school is 4/3 Pi * R3
double getVolume() {
double radius = diameter / 2.0;
double volume = 4.0 / 3.0 * Math.PI * radius * radius * radius;
return volume;
}
// Surface: formula if I remember weel from my hight choole is 4 Pi * R2
double getSurface() {
double radius = diameter / 2.0;
double surface = 4.0 * Math.PI * radius * radius;
return surface;
}
// now a main method to test everyrhing
public static void main(String[] arg) {
// lets create 2 sphere objects by "instantiating" the class Sphere
Sphere sphere1 = new Sphere(1.0);
Sphere sphere2 = new Sphere(2.0);
// display the surface and the volume
System.out.println("Sphere 1: Diameter: " + sphere1.getDiameter() + " Surface: " + sphere1.gerSurface() + " Volume: " + sphere1.getVolume());
System.out.println("Sphere 2: Diameter: " + sphere2.getDiameter() + " Surface: " + sphere2.getSurface() + " Volume: " + sphere2.getVolume());
}
}
Report Abuse
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.