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

Instructions Write a Circle class (Circle.java) that has the following fields: - radius (double) - PI...
Instructions Write a Circle class (Circle.java) that has the following fields: - radius (double) - PI (final double initialized to 3.1415) // no need to have setters or getters for this Include the following methods: - Constructor (accepts radius of circle as an argument) - Constructor (no argument that sets radius to 0.0) - setRadius (set method for radius field) - getRadius (get method for radius field) - calcArea (calculates and returns area of circle: area = PI * radius...
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...
Write a class named Car that has the following fields: • yearModel: The yearModel field is...
Write a class named Car that has the following fields: • yearModel: The yearModel field is an int that holds the car's year model. • make: The make field is a String object that holds the make of the car. • speed: The speed field is an int that holds the car's current speed. In addition, the class should have the following methods: • Constructor: The constructor should accept the car's year model and make as arguments. These values should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT