Question: please fix my code i am receving an error. Design and implement a class called Sphere that contains instance data that represents the sphere's diameter.

please fix my code i am receving an error.

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.

//Sphere.java public class Sphere { private double diameter;

public Sphere(double diameter) { this.diameter = diameter; }

public double getDiameter() { return diameter; }

public void setDiameter(double diameter) { this.diameter = diameter; }

public double getVolume(){ double r = diameter/2; return (4.0/3.0)*Math.PI*r*r*r; }

public double getArea(){ double r = diameter/2; return 4.0*Math.PI*r*r; }

public String toString() { return "Sphere{" + "diameter=" + diameter + '}'; } }

//MultiSphere.java public class MultiSphere { public static void main(String args[]) { Sphere sphere1 = new Sphere(5); System.out.println(sphere1.getArea()); System.out.println(sphere1.getVolume()); sphere1.setDiameter(10); System.out.println(sphere1.getArea()); System.out.println(sphere1.getVolume());

Sphere sphere2 = new Sphere(15); System.out.println(sphere2); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!