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...
Write a Circle class that has the following member variables: • radius: a double • pi:...
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, which...
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 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...
Write a java class called circle that represents a circle. It should have following three fields:...
Write a java class called circle that represents a circle. It should have following three fields: int x (x-coordinate), int y (y-coordinate), double radius (radius of the circle). Your circle object should have following methods: public int getRadius () public int getX () public int getY () public double area () public double perimeter() public String toString() write a client program called CircleClient that creates objects of the circle class called c1 and c2. Assign values to the fields when...
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...
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 program in C that computes the area of a circle (Area = pi *...
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT