Question

In: Computer Science

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.

Solutions

Expert Solution

class Sphere{
   private double diameter;

   public Sphere(double aDiameter) {
       super();
       diameter = aDiameter;
   }

   public double getDiameter() {
       return diameter;
   }

   public void setDiameter(double aDiameter) {
       diameter = aDiameter;
   }
   // returns the area using 4 / * PI r^2
   public double getArea(){
       double r = diameter/2;
       return 4 * Math.PI * r * r;
   }
   // returns the volumes using 4 /3 * PI r^3
   public double getVolume(){
       double r = diameter/2;
       return 4 / 2* Math.PI * r * r * r;
      
   }
  
   public String toString(){
       return "Area : "+getArea()+" Volume : "+getVolume();
   }
  
}
public class MultiSphere {
public static void main(String[] args) {
       Sphere s1 = new Sphere(5);
       Sphere s2 = new Sphere(7);
       Sphere s3 = new Sphere(9);
       System.out.println(s1);
       System.out.println(s2);
       System.out.println(s3);
      
   }
  
}


Related Solutions

Java Write a class called Car that contains instance data that represents the make, model, and...
Java Write a class called Car that contains instance data that represents the make, model, and year of the car. Define the Car constructor to initialize these values Include getter and setter methods for all instance data and a toString method that returns a one-line description of the car. Add a method called isAntique that returns a boolean indicating if the car is an antique (if it is more than 45 years old). Create a driver class called CarTest, whose...
in java Design and implement a class called Dog that contains instance data that represents the...
in java Design and implement a class called Dog that contains instance data that represents the dog’s name and age. Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age. Include a method to compute and return the age of the dog in “person years” (seven times the dog’s age). Include a toString method that returns a one-line description of the dog. Create a Tester class called Kennel, whose main...
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...
**Please write in Java, in a very simple/beginner coding style/language - thank you!** Directions: Given a...
**Please write in Java, in a very simple/beginner coding style/language - thank you!** Directions: Given a factorial n!. Find the sum of its digits, and the number of trailing zeros (ie: the number of zeros at the end of the factorial). Input: integer nn, a non-negative integer where n≤20n≤20 Output: integer xyxy, the concatenation of x and y, where x is the sum of the digits in n! and y is the number of the zeros in n!) Note, 0≤x,y0≤x,y....
JAVA Design and implement a class called Boxthat represents a 3-dimensional box.1.Instance DataYour Box class will...
JAVA Design and implement a class called Boxthat represents a 3-dimensional box.1.Instance DataYour Box class will need variables to store the size of the box. All of these variables will be of type ‘private double’.•width•height•depthYour Boxclass will also need a variable to keep track of whether or not the box is full. This will be of type ‘private boolean’.•full2.ConstructorDeclaration: public Box( double width, double height, double depth)•Your Boxconstructor will accept the width, height,and depthof the box.•In your constructor, initialize the...
Write a class called Pen that contains the following information: Private instance variables for the price...
Write a class called Pen that contains the following information: Private instance variables for the price of the pen (float) and color of the pen (String). A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {
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
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters,...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters, setters, and getters for “year manufactured” and “make” and “model” (e.g. 2016 Honda Civic) 2. Create a class called CarLot which has an array of 5 Car references. There are two constructors: (a) One constructor takes no parameters and simply populates the array with these cars: cars[0] = new Car(2016, “honda”, “civic”); cars[2] = new Car(2017, “Lamborghini”, “aventador”); cars[3] = new Car(2000, null, “caravan”);...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that stores the int value represented by this object. • A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice? • A constructor that creates a MyInteger object for a specified int value. • A getter method, valueOf(), that returns value. • A setter method,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT