Question: 1. You will create a Java Class called Sphere. The class should have the following: Constructor: New() Zero parameter. Creates a Sphere with a radius

1. You will create a Java Class called Sphere. The class should have the following:

Constructor: New() Zero parameter. Creates a Sphere with a radius of zero

Constructor: New(double) Creates a Sphere with the initial radius specified. If the radius specified is less than zero, the Sphere should be constructed with a radius of Zero

Methods:

double volume() Should return the volume of the Sphere

double surfaceArea() Should return the surface area of the Sphere

setRadius( double NewRadius) Should set the radius of the Sphere to the NewRadius provided NewRadius >= 0. If NewRadius <0, no change to the Sphere.

double getRadius( ) Should return the radius of the Sphere

String toString() should return the String representation of the Sphere

Sphere needs to implement the Comparable interface. The comparison should be based upon the size of the Radius.

You must include a main method with appropriate test(s). Remember what an appropriate level of testing includes.

Your Sphere must also include javadoc comments and tags for the class, and all methods and constructors.

here.java file:

public class Sphere implements Comparable{ private double radius ;

public Sphere() { this.radius = 0 ; }

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

public double getRadius() { return radius; }

public void setRadius(double radius) { this.radius = radius; } public double volume() { return (4.0/3.0) * Math.PI * this.radius * this.radius * this.radius ; } public double surfaceArea() { return 4 * Math.PI * this.radius * this.radius ; } public String toString() { return "Sphere [radius=" + radius + "]"; } @Override public int compareTo(Object o) { Sphere other = (Sphere) o; if(this.radius > other.radius) return 1 ; else if(this.radius < other.radius) return -1 ; return 0; } }

TestSphere.java file:

public class TestSphere {

public static void main(String[] args) {

Sphere s1 = new Sphere(5); System.out.println("Sphere1 : " + s1); System.out.println("Volume : " + s1.volume()); System.out.println("Surface area : " + s1.surfaceArea());

}

}

Please do this with the "New" constructors thank you.

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!