Question

In: Computer Science

Write a Circle Class that has the following fields: • radius: a double • PI: a...

Write a Circle Class that has the following fields:

• radius: a double

• PI: a final double initialized with the value 3.14159

• Constructor. Accepts the radius of the circle as an argument

• Constructor. A no-arg constructor that sets the radius field to 0.0.

• setRadius. A mutator method for the radius field.

• getRadius. An accessor method for the radius field.

• getArea. Returns the area of the circle, which is calculated as area = PI * radius * radius.

• getDiameter. Returns the diameter of the circle, which is calculated as diameter = radius * 2.

• getCircumference. Returns the circumference of the circle, which is calculated as circumference = 2 * PI * radius.

Write a program that demonstrates the Circle class by asking the user for the radius of a circle, creating a circle object, and then reporting the circle’s area, diameter, and circumference. See Rectangle example in book.

Submit:

1. Algorithm

2. UML diagram

3. A complete Java program with comments/documentation

4. Output

Test Data: Radius =0 5

Solutions

Expert Solution

class Circle {
   private double radius;
   private final double PI = 3.14159;

   // constructors
   public Circle(double aRadius) {
       super();
       radius = aRadius;
   }

   // getters and setters
   public double getRadius() {
       return radius;
   }

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

   // returns area of circle
   public double getArea() {
       return PI * radius * radius;
   }

   // returns circumference of circle
   public double getCircumference() {
       return 2 * PI * radius;
   }

   // returns diameter
   public double getDiameter() {
       return 2 * radius;
   }

   // returns string representation of object
   public String toString() {
       return "Radius : " + getRadius() + " Diameter : " + getDiameter() + " Area : " + getArea() + " Circumference : "
               + getCircumference();

   }
}

public class Main {
   public static void main(String[] args) {
       Circle c = new Circle(5);
       System.out.println(c);

   }
}

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

Write a Circle class that has the following member variables: radius as a double PI as...
Write a Circle class that has the following member variables: radius as a double PI as a double initialized with 3.14159 The class should have the following member functions: Default constructor Sets the radius as 0.0 and pi as 3.14159 Constructor Accepts the radius of the circles as an argument setRadius A mutator getRadius An accessor getArea Returns the area of the circle getDiameter Returns the diameter of the circle getCircumference Returns the circumference of the circle Write a program...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the...
Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the Circle class, you should test this class from the main() method by passing objects of this class to a method “ public static void printAreas(Circle c, int times)” You should display the area of the circle object 5 times with different radii. 2 java.util.Random +Random() +Random(seed: long) +nextInt(): int +nextInt(n: int): int +nextLong(): long +nextDouble(): double +nextFloat(): float +nextBoolean(): boolean Constructs a Random object...
Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this...
Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this radius, the circumference of the circle, the area of the circle, the surface area of a sphere with that radius and the area of a sphere with that radius. Each of its computation functions returns its answers in call by reference parameters. Here are the formulas for computation: Circumference = 2 • π • r Circle Area = π • r2 Sphere Surface Area...
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...
Write c code program for the following Write a function, circle, which takes the radius of...
Write c code program for the following Write a function, circle, which takes the radius of a circle from the main function and assign the area of the circle to the variable, area, and the perimeter of the circle to the variable, perimeter. Hint: The function should have three variables as input. Since the contents of the variables are to be modified by a function, it is necessary to use pointers. Please type out the full usable program. Thank you.
A circle of radius r has area A = πr2. If a random circle has a...
A circle of radius r has area A = πr2. If a random circle has a radius that is uniformly distributed on the interval (0, 1), what are the mean and variance of the area of the circle? Change the distribution of the radius to an exponential distribution with paramter β = 2. Also find the probability that the area of the circle exceeds 3, if it is known that the area exceeds 2.
Write a C++ program that prompts the user for the radius of a circle and then...
Write a C++ program that prompts the user for the radius of a circle and then calls inline function circleArea to calculate the area of that circle. It should do it repeatedly until the user enters -1. Use the constant value 3.14159 for π Sample: Enter the radius of your circle (-1 to end): 1 Area of circle with radius 1 is 3.14159 Enter the radius of your circle (-1 to end): 2 Area of circle with radius 2 is...
Write a java program using the following information Design a LandTract class that has two fields...
Write a java program using the following information Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:  a constructor that accepts arguments for the two fields  a method that returns the tract’s area(i.e. length * width)  an equals method that accepts a LandTract object as an argument. If the argument object holds the same data (i.e. length and...
Write a convertToMetric class that has the following field: standard - holds a double, standard length...
Write a convertToMetric class that has the following field: standard - holds a double, standard length value in feet. The class should have the following methods: Constructor - that accepts a length in feet (as a double) and stores it in the standard field. setStandard - accepts a standard length in feet and stores it in standard. getStandard - returns the value of the standard field, as a length in feet, no conversion required. getMeters - returns the value of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT