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 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 increments numSpheres by 1. public Sphere(double radiusInit) // overloaded constructor. Initialize Sphere objects’ instance variable radius. It also increments numSpheres by 1. public void setRadius(double aRadius) //sets Sphere objects’ instance variable 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 static int getNumSpheres() //returns the number of sphere objects 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
/**
*
*
* Do not change the tester.
*
*/
public class SphereTester
{
//-----------------------------------------------------------------
// Creates and exercises some Sphere objects.
//-----------------------------------------------------------------
public static void main(String[] args)
{
Sphere basketBall = new Sphere(4.75); //in inches
System.out.println("Basketball\n----------\n" + basketBall);
//println will implicitly/automaticlly call toString method of the Sphere object.
Sphere eyeBall = new Sphere(0.5);
System.out.println("Eyeball\n----------\n"+eyeBall.toString()); //explicitly calling toString()
Sphere softBall; // 1. Declare a reference "softBall" to the Sphere object
softBall = new Sphere(); //2. Create the object using new operation
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
*/JAVA PROGRAM :-
class Sphere {
private double radius;
//radius of the sphere object
private static int numSpheres = 0; //static or class variable. All
Sphere objects share it
//constructor . Initialize
Sphere objects’ instance variable radius to 0.0. It also increments
numSpheres by 1.
public Sphere(){
this.radius = 0.0;
numSpheres = numSpheres + 1;
}
// overloaded constructor.
Initialize Sphere objects’ instance variable radius. It also
increments numSpheres by 1.
public Sphere(double radiusInit){
this.radius = radiusInit;
numSpheres = numSpheres + 1;
}
//sets Sphere objects’
instance variable radius
public void setRadius(double aRadius){
this.radius = aRadius;
}
// returns the value of the
Sphere objects’ instance variable radius
public double getRadius(){
return this.radius;
}
//computes and returns the
volume of the Sphere object
public double calculateVolume(){
double temp;
temp = (double)4.0/3.0;
temp = temp * (22.0/7.0);
temp = temp * this.radius * this.radius * this.radius;
return temp;
}
//computes and returns the
area of the Sphere object
public double calculateArea(){
double temp;
temp = (double)4.0;
temp = temp * (22.0/7.0);
temp = temp * this.radius * this.radius;
return temp;
}
//computes and returns the
diameter of the Sphere object
public double calculateDiameter(){
return (2.0*this.radius);
}
//returns the number of sphere
objects
public static int getNumSpheres(){
return numSpheres;
}
// returns a String
representation of the Sphere object along with the
// the volume and area with formatting.
public String toString(){
String str1 = "The sphere with radius
"+Double.toString(this.radius)+" with volume
"+Double.toString(calculateVolume())+" and area
"+Double.toString(calculateArea());
return str1;
}
}
public class
SphereTester
{
//-----------------------------------------------------------------
// Creates and exercises some Sphere objects.
//-----------------------------------------------------------------
public static void main(String[] args)
{
Sphere basketBall = new Sphere(4.75); //in inches
System.out.println("Basketball\n----------\n" + basketBall);
//println will implicitly/automaticlly call toString method of the
Sphere object.
Sphere eyeBall = new Sphere(0.5);
System.out.println("Eyeball\n----------\n"+eyeBall.toString());
//explicitly calling toString()
Sphere softBall; // 1. Declare a reference "softBall" to the Sphere
object
softBall = new Sphere(); //2. Create the object using new
operation
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 :-

Complete the implementation of the Sphere class based on the Diagram below. Use the DecimalFormat class...
Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume of rectangular objects. First, write an abstract super class RectangularShape, then write a subclass Rectangle that inherits from the super class to compute areas of rectangles, including squares if there is only one data. Finally, write another subclass Cuboid that inherits from its super class Rectangle above to compute surface areas and volumes of cuboids, including 3 equal sided cube. Must apply code-reusability in...
Write a java class definition for a circle object. The object should be capable of setting radius, and computing its area and circumference. Use this to create two Circle objects with radius 10 and 40.5, respectively. Print their areas and circumference. Here is the Java class file (Circle.java). Compile it. public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { } //get method (Accessor Methods ) public double getRadius (...
PLEASE HELP this is the last part of my lab and I need help ): add comments please (: if it is too difficult you do not have to do part 3 but it would be greatly appreciated if you do ! Obtain example code files Circle.java, Shape.java, CircleShape2.java, Sphere.java, and CircleShapeApp.java from the downloaded files in Ch8. Compile and execute the example and understand the polymorphism it performs. (I have copied and pasted all of these files below) Modify...
Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An instance variable named x of type int. An instance variable named y of type int. Declare a method named toString() Returns a string representation of the point. Constructor that accepts values of all data members as arguments. Define a Sub class named Circle. A Circle object stores a radius (double) and inherit the (x, y) coordinates of its center from its super class Point....
****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...
A general shape class is shown below. Shape has a dimension. It also defines constructors, getters, setters and a toString method. class Shape{ int dimension; public Shape(){} public Shape(int newDimension){ dimension = newDimension; } public int getDimension(){ return dimension; } public void setDimension(int newDimension){ dimension = newDimension; } public String toString(){ return "Shape has dimension "+dimension; } } a. Define classes Circle and Square, which inherit from Shape class. b. Both classes must have two constructors similar to Shape class....
Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...
The following code computes the radius of a circle. Using static methods from the Math class complete the method that computes the radius of a circle using the formula r2=(x-h)2 +(y-k)2 , given the (x,y) coordinates of one point on its circumference and the (h,k) coordinates of its center. public class Circle { public static void main(String[] C) { double x1 =14.25; double y1 =13.68; double xCenter = 25.678; double yCenter...
Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain { public static void main(String [] args) throws CloneNotSupportedException { /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5); ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone(); System.out.println(obj1.toString()); System.out.println(obj1 == obj2); //false System.out.println(obj2.toString());*/ Scanner...
This question is about java program. Please show the output and the detail code and comment.And the output (the test mthod )must be all the true! Thank you! And the question is contain 7 parts: Question 1 Create a Shape class with the following UML specification: (All the UML "-" means private and "+" means public) +------------------------------------+ | Shape | +------------------------------------+ | - x: double | | - y: double | +------------------------------------+ | + Shape(double x, double y) | |...