Question

In: Computer Science

Ch 5 Program 1: Sphere.java                Complete the implementation of the Sphere class based on the Diagram...

Ch 5 Program 1: Sphere.java               

  1. 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.

  2. 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

*/

Solutions

Expert Solution

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


Related Solutions

Complete the following class UML design class diagram by filling in all the sections based on...
Complete the following class UML design class diagram by filling in all the sections based on the information given below.         The class name is Boat, and it is a concrete entity class. All three attributes are private strings with initial null values. The attribute boat identifier has the property of “key.” The other attributes are the manufacturer of the boat and the model of the boat. Provide at least two methods for this class. Class Name: Attribute Names: Method...
Ch 4 Program 5: RockPaperScissors.java                                    &
Ch 4 Program 5: RockPaperScissors.java                                                  Please adhere to the Standards for Programming Assignments and the Java Coding Guidelines. In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows Rock beats scissors because a rock can break a part of scissors Scissors beats paper because scissors can cut paper...
For this assignment, create a complete UML class diagram of this program. You can use Lucidchart...
For this assignment, create a complete UML class diagram of this program. You can use Lucidchart or another diagramming tool. If you can’t find a diagramming tool, you can hand draw the diagram but make sure it is legible. Points to remember: • Include all classes on your diagram. There are nine of them. • Include all the properties and methods on your diagram. • Include the access modifiers on the diagram. + for public, - for private, ~ for...
Complete the implementation of the Die class. Note that the Die class uses an array to...
Complete the implementation of the Die class. Note that the Die class uses an array to represent the faces of a die. You need to complete the no-argument constructor and the two methods compareTo and equals. Code: import java.util.Arrays; import java.util.Objects; import java.util.Random; /* * NOTE TO STUDENTS: * The constructor that you need to complete can be found on line 47. * * The two methods you need to complete can be found at the end of this file....
9.11 LAB: Winning team (classes) Complete the Team class implementation. For the class method get_win_percentage(), the...
9.11 LAB: Winning team (classes) Complete the Team class implementation. For the class method get_win_percentage(), the formula is: team_wins / (team_wins + team_losses) Note: Use floating-point division. Ex: If the input is: Ravens 13 3 where Ravens is the team's name, 13 is the number of team wins, and 3 is the number of team losses, the output is: Congratulations, Team Ravens has a winning average! If the input is Angels 80 82, the output is: Team Angels has a...
“A complete and consistent class diagram is all that is required to implement and to understand...
“A complete and consistent class diagram is all that is required to implement and to understand an application”. Discuss the correctness or otherwise of this statement. In your answer, among other things, consider how class diagrams evolve and who has a stake in the understanding and implementation of an application.
“A complete and consistent class diagram is all that is required to implement and to understand...
“A complete and consistent class diagram is all that is required to implement and to understand an application”. Discuss the correctness or otherwise of this statement. In your answer, among other things, consider how class diagrams evolve and who has a stake in the understanding and implementation of an application.
“A complete and consistent class diagram is all that is required to implement and to understand...
“A complete and consistent class diagram is all that is required to implement and to understand an application”. Discuss the correctness or otherwise of this statement. In your answer, among other things, consider how class diagrams evolve and who has a stake in the understanding and implementation of an application.
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
Goal: Write a program that provides practice in class development and implementation of programs in multiple...
Goal: Write a program that provides practice in class development and implementation of programs in multiple separate files. Make sure you thoroughly read and understand the directions before you begin PART 1: Write a program that displays a temperature conversion chart. There should be 6 functions defined as part of a class. • print_introduction_message • get_conversion_table_specifications • print_message_echoing_input • fahrenheit_to_celsius • fahrenheit_to_kelvin • print_table A portion of the code is defined below but not using classes. Add the 4 missing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT