Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

\"Design and implement a class called \'Sphere\' that contains instance data tha

ID: 3652926 • Letter: #

Question

"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, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface area of the sphere. Included 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"

Explanation / Answer

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()); } }