Question

In: Computer Science

Create a class called Sphere. The class will contain the following    Instance data double radius...

Create a class called Sphere. The class will contain the following

   Instance data

double radius

Methods

Constructor with one parameter which will be used to set the radius instance data

Getter and Setter for radius

            Area - calculate the area of the sphere (4 * PI * radius * radius)

            Volume - calculate and return the volume of the sphere (4/3 * PIE * radius * radius * radius)

toString - returns a string with the description of the class which includes the radius.

The class with main will create two instances of the Sphere class, with one call the area and volume and toString methods and display all of the returned data. With the second object call the set method to change the radius to a different value than what was provided as an argument to the constructor. Use the get method to display the radius. Call the area, volume and toString for that instance and display the returned values.

Solutions

Expert Solution

class Sphere {
   double radius;

   public Sphere() {

   }

   /**
   * @param r
   */
   public Sphere(double r) {
       radius = r;
   }

   /**
   * @return
   */
   public double getRadius() {
       return radius;
   }

   public void setRadius(double aRadius) {
       radius = aRadius;
   }

   public double getSurFaceArea() {
       return 4 * Math.PI * radius * radius;
   }

   public boolean checkRadius() {
       return radius > 0;
   }

   public double getVolume() {
       return 4.0 / 3 * Math.PI * (radius * radius * radius);
   }

}

public class TestSphere {
   public static void main(String[] args) {
       Sphere s1 = new Sphere(12);
       System.out.println("Surface Area: " + s1.getSurFaceArea());
       System.out.println("Volume: " + s1.getVolume());

   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

In java beginner coding language ,Write a class called Sphere that contains instance data that represents...
In java beginner coding language ,Write a class called Sphere that contains instance data that represents the sphere’s diameter. Define the Sphere constructor to accept and initialize the diameter, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface area of the sphere. Include a toString method that returns a one-line description of the sphere. Create a driver class called MultiSphere, whose main method instantiates and updates several Sphere objects.
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
Create a class called Dishwash with a double called CubicFeet, a string called color, and a...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a method called Washing. The Washing method should return a string to the main class with the text "Now washing dishes!" In the main class create an array of DishWasher size 3. Give each DishWasher a different color and CubicFeet. Use a foreach loop to display that info, call the Washing method, and display the text returned from the Washing method call. c#
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class Create another class called CourseSection Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of the methods required for a standard user defined...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance Variables Declare an ArrayList of BabyName objects. Constructor public BabyNamesDatabase () - instantiate the ArrayList of BabyName objects. You will not insert the items yet. This method will be one line of code. Mutator Methods public void readBabyNameData(String filename) - open the provided filename given as input parameter, use a loop to read all the data in the file, create a BabyName object for...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
Create a class called employee which has the following instance variables: Employee ID number Salary Years...
Create a class called employee which has the following instance variables: Employee ID number Salary Years at the company Your class should have the following methods: New employee which reads in an employee’s ID number, salary and years of service Anniversary which will up the years of service by 1 You got a raise which will read in how much the raise was (a percent) and then calculate the new salary You get a bonus which gives a yearly bonus...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows:...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows: student id, test1, test2, and test3. 2. Create one constructor with parameter values for all instance data fields. 3. Create getters and setters for all instance data fields. 4. Provide a method called calcAverage that computes and returns the average test score for an object to the driver program. 5. Create a displayInfo method that receives the average from the driver program and displays...
Create a class called Vehicle that includes four instance variables:      name, type,     tank size and...
Create a class called Vehicle that includes four instance variables:      name, type,     tank size and average petrol consumption. Provide 2 constructors, the first takes name and type as parameter, the second takes four parameters for the four instance variables. (2 pt) Provide also a method called distancePerTank that calculate the average distance that a vehicle can travel if the tank is full (multiplies the tank size by the average petrol consumption), then returns the value. (2 pt) Provide a...
Using C# Create a class called Artist that contains 4 pieces of information as instance variables:...
Using C# Create a class called Artist that contains 4 pieces of information as instance variables: name (datatype string), specialization – i.e., music, pottery, literature, paintings (datatype string), number of art items (datatype int), country of birth (datatype string). Create a constructor that initializes the 4 instance variables. The Artist class must contain a property for each instance variable with get and set accessors. The property for number should verify that the Artist contributions is greater than zero. If a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT