In: Computer Science
1.
a.Include a Javadoc comment at the top of the class. The Javadoc comment should contain:
i.The name of the class and a (very) short description
ii.An @author tag followed by your name
iii.An @version tag followed by the version number.
b.Do not include a main method inside the class definition. Remember the main method is what gets executed first when we start our program. In this case, the Sphere class is not a complete program. It will simply be used to represent a Sphere concept. We will create our main method in a separate Driver class.
2.A Sphere is defined mathematically as the set of points in 3D space that are all at the same distance r (radius) from a given point. This suggests that a Sphere should have instance variables that represent the following:
a.X-coordinate
b.Y-coordinate
c.Z-coordinate 1
d.Radius.
3.The Sphere class needs a constructor which accepts a parameter for each of these four attributes, and assigns the value to the respective instance variable.
4.Create an accessor and a mutator for each instance variable.
5.Create a method that returns the surface area of the Sphere. The formula for the surface area A of a sphere of radius r =4휋푟!.
6.Create a method that returns the volume of the Sphere. The formula for the volume V of a sphere of radius r ="#휋푟#.
7.Create a toString()method which returns a String composed of the concatenation of the information in the Sphere. Customarily the toString() is the last method in the class
- Kindly upvote
if this helped
CODE:
- Please note that I have added a main method just for
testing. Please remove it as it is mentioned in the question not to
write main method in same java file
/**
 * @author Aman Kaushik - <Write you name here>
 * @version 1.1 <Give a version of your choice here>
 * @param request
 * @return gives a layout of the class Sphere and has methods to compute surface area and volume of sphere
 */
public class Sphere {
        double X , Y , Z , radius;
public double getX() {
                return X;
        }
        public void setX(double x) { // mutator
                X = x;
        }
        public double getY() { // accessor
                return Y;
        }
        public void setY(double y) {
                Y = y;
        }
        public double getZ() {
                return Z;
        }
        public void setZ(double z) {
                Z = z;
        }
        public double getRadius() {
                return radius;
        }
        public void setRadius(double radius) {
                this.radius = radius;
        }
Sphere(){
        X = 0;
        Y = 0;
        Z = 0;
        radius = 0;
}
Sphere(int X , int Y , int Z , int radius){
        this.X = X;
        this.Y = Y;
        this.Z = Z;
        this.radius = radius;
}
public double getSurfaceArea() {
        return 4 * Math.PI * radius * radius; // 4 pi r square
}
public double getVolume() {
        return (4/3) * (Math.PI * radius * radius * radius); // 4/3 pi r cube
}
public String toString() {
        return "Sphere with radius "+this.radius+" has volume = "+this.getVolume()+" and surface area = "+this.getSurfaceArea();
}
public static void main(String[] args) {
        Sphere s = new Sphere(10 , 20 ,20 , 11);
        System.out.print(s.toString());
}
}
Sample output