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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started