Question

In: Computer Science

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 * radius)

- calcDiameter (calculates and returns diameter of circle: diameter = radius * 2)

- calcCircumference (calculates and returns circumference of circle: circumference = 2* PI * radius)

Write a test program (TestCircle.java) that will ask user for the radius, creates the circle object, and then displays the area, diameter, and circumference.

Make sure to create 2 circle object instances - one with the radius as an argument, one without but sets the radius with the setRadius() method.

Thank you so much for your help!!

Solutions

Expert Solution

// Screenshot of the code & output

// Code to copy

Circle.java

public class Circle {
  
   private double radius;
   final double PI = 3.1415;
  
   // default-constructor
   public Circle ()
   {
       this.radius=0.0;  
   }
  
   //argument-constructor
   public Circle (double r)
   {
       this.radius=r;  
   }

   public double getRadius()
   {
       return radius;
   }
  
   public void setRadius(double radius)
   {
       this.radius = radius;
   }
   // calcArea method to calculate Circle Area
   public double calcArea() {
       double area = PI * radius * radius;
       return area;
   }
   // calcDiameter method to calculate Circle Diameter
   public double calcDiameter() {
       double diameter = radius * 2;
       return diameter;
   }
   // calcCircumference method to calculate Circle Circumference
   public double calcCircumference() {
       double circumference = 2* PI * radius;
       return circumference;
   }
}

TestCircle.java

import java.util.Scanner;
import java.text.DecimalFormat;
public class TestCircle {

   public static void main(String[] args) {
       DecimalFormat df = new DecimalFormat("0.00");
       double radius;
       Scanner input=new Scanner(System.in);      
System.out.print("Enter radius of circle :");
radius=input.nextDouble();
// circle object with out radius
Circle c1=new Circle ();
c1.setRadius(5);
System.out.println("\nCircle-1 object with out radius ");
System.out.println(df.format(c1.calcArea()));
System.out.println(df.format(c1.calcDiameter()));
System.out.println(df.format(c1.calcCircumference()));
  
// circle object with radius
       Circle c2=new Circle (radius);
       System.out.println("\nCircle-2 object with radius ");
System.out.println(df.format(c2.calcArea()));
System.out.println(df.format(c2.calcDiameter()));
System.out.println(df.format(c2.calcCircumference()));
// closing scanner object
input.close();
  
   }

}


Related Solutions

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 *...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT