In: Computer Science
Ch 5 Program 1: Sphere.java
Complete the implementation of the Sphere class based on the Diagram below. Use the DecimalFormat class in Sphere.java to format your output in the toString method.
Then download the tester application SphereTester.java from canvas to test your Sphere class. Do not change SphereTester.java.
Sphere |
private double radius //radius of the sphere object private static int numSpheres //static or class variable. All Sphere objects share it |
public Sphere() //constructor . Initialize Sphere objects’ instance variable radius to 0.0. It also increment numSpheres by 1. public Sphere(double radius) // overloaded constructor. Initialize Sphere objects’ instance variable radius(use the “this” keyword ). It also increments numSpheres by 1.. public void setRadius(double radius) //sets Sphere objects’ instance variable this.radius to radius public double getRadius() // returns the value of the Sphere objects’ instance variable radius public double calculateVolume() //computes and returns the volume of the Sphere object public double calculateArea() //computes and returns the area of the Sphere object public double calculateDiameter() //computes and returns the diameter of the Sphere object public String toString() // returns a String representation of the Sphere object along with the // the volume and area with formatting. |
Your output must look exactly as follow.
Basketball
----------
radius: 4.75 volume: 448.921 area: 283.529
numSpheres: 1
Eyeball
----------
radius: 0.5 volume: 0.524 area: 3.142
numSpheres: 2
Softball's radius: 0.0
Softball after setting radius to 18.4
------------------------------------
radius: 18.4 volume: 26094.085 area: 4254.47
numSpheres: 3
SphereTester.java
/** * * @ * Do not change the this driver class. * */ public class SphereTester { //----------------------------------------------------------------- // Creates and exercises some Sphere objects. //----------------------------------------------------------------- public static void main(String[] args) { //Creating a Sphere object with a radius of 4.75 inches Sphere basketBall = new Sphere(4.75); //in inches //println will implicitly/automaticlly call toString method of this Sphere object. System.out.println("Basketball\n----------\n" + basketBall); //explicitly calling toString() on eyeBall. Both will work Sphere eyeBall = new Sphere(0.5); System.out.println("Eyeball\n----------\n"+eyeBall.toString()); //explicitly calling toString(). Both will work //You can create an object in two statements as shown below Sphere softBall; // 1. Declare a reference variable "softBall" of type Sphere softBall = new Sphere(); //2.new operation creates object and assign its memory address to softBall System.out.println("Softball's radius: " + softBall.getRadius() ); softBall.setRadius(18.4); System.out.println("Softball after setting radius to " + softBall.getRadius()+"\n------------------------------------" ); System.out.println(softBall); } } /*Basketball ---------- radius: 4.75 volume: 448.921 area: 283.529 numSpheres: 1 Eyeball ---------- radius: 0.5 volume: 0.524 area: 3.142 numSpheres: 2 Softball's radius: 0.0 Softball after setting radius to 18.4 ------------------------------------ radius: 18.4 volume: 26094.085 area: 4254.47 numSpheres: 3 */
Code for your program is provided below. It is explained thoroughly in code comments. You can also refer to the screenshot of properly indented code in IDE that i have attached in the last. Output screenshot is also provided.
If you need any further clarification , please feel free to ask in comments. I would really appreciate if you would let me know if you are satisfied with the explanation provided.
##################################################################
CODE
Sphere.java
import java.text.DecimalFormat; //importing to use DecimalFormat
public class Sphere
{
private double radius; //radius of the sphere object
private static int numSpheres; //static or class variable. All Sphere objects share it
public Sphere() //constructor . Initialize Sphere objects’ instance variable radius to 0.0. It also increment numSpheres by 1.
{
radius=0.0; //initialkze radius
numSpheres++; //increment numSpheres
}
// overloaded constructor. Initialize Sphere objects’ instance variable radius(use the “this” keyword ). It also increments numSpheres by 1..
public Sphere(double radius)
{
this.radius=radius; //initialize radius
numSpheres++; //increment numSpheres
}
public void setRadius(double radius) //sets Sphere objects’ instance variable this.radius to radius
{
this.radius=radius; //set radius
}
public double getRadius() // returns the value of the Sphere objects’ instance variable radius
{
return radius; //return radius
}
public double calculateVolume() //computes and returns the volume of the Sphere object
{
return ((4.0/3.0)*Math.PI*radius*radius*radius); //calculate valume of sphere
}
public double calculateArea() //computes and returns the area of the Sphere object
{
return (4.0*Math.PI*radius*radius); //calckulate are of sphere
}
public double calculateDiameter() //computes and returns the diameter of the Sphere object
{
return 2.0*radius; //calculate diameter of sphere
}
// returns a String representation of the Sphere object along with the volume and area with formatting.
public String toString()
{
DecimalFormat numberFormat = new DecimalFormat("#########.###"); //setting format with 3 decimal places
return "radius: "+radius+" volume: "+numberFormat.format(calculateVolume())+" area: "+numberFormat.format(calculateArea())
+"\nnumSpheres: "+numSpheres; //return string
}
}
SphereTester.java
public class SphereTester
{
//-----------------------------------------------------------------
// Creates and exercises some Sphere objects.
//-----------------------------------------------------------------
public static void main(String[] args)
{
//Creating a Sphere object with a radius of 4.75 inches
Sphere basketBall = new Sphere(4.75); //in inches
//println will implicitly/automaticlly call toString method of this Sphere object.
System.out.println("Basketball\n----------\n" + basketBall);
//explicitly calling toString() on eyeBall. Both will work
Sphere eyeBall = new Sphere(0.5);
System.out.println("Eyeball\n----------\n"+eyeBall.toString()); //explicitly calling toString(). Both will work
//You can create an object in two statements as shown below
Sphere softBall; // 1. Declare a reference variable "softBall" of type Sphere
softBall = new Sphere(); //2.new operation creates object and assign its memory address to softBall
System.out.println("Softball's radius: " + softBall.getRadius() );
softBall.setRadius(18.4);
System.out.println("Softball after setting radius to " +
softBall.getRadius()+"\n------------------------------------" );
System.out.println(softBall);
}
}
##################################################################
OUTPUT
###############################################################
SCREENSHOT OF CODE