4. Write a Circle class that has the following variables: • radius: a double • p
ID: 3662354 • Letter: 4
Question
4. Write a Circle class that has the following variables: • radius: a double • pi: a double initialized with the value 3.1416 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. Return the area of the circle, which is calculated as area = pi * radius * radius • getDiameter. Return the diameter of the circle, which is calculated as diameter = radius *2 • getCircumference. Return the circumference of the circle, which is calculated as circumference = 2 * pi * radius. Write a program the demonstrate the Circle class by asking the user for the circle's radius, creating a Circle object, and then reporting the circle's area, diameter, and circumference.
Explanation / Answer
Circle.java
package com.chegg.test;
/**
* This class is used to calculate the area,diameter&circumference of a circle
*
* @author Your Name
*
*/
public class Circle {
double radius;
double pi = 3.1416;
/**
* Default Constructor to initialize the radius with 0.0
*/
Circle() {
this.radius = 0.0;
}
/**
* Parameterized constructor to create circle object
*
* @param radius
*/
Circle(double radius) {
setRadius(radius);
}
/**
* @return the radius
*/
public double getRadius() {
return radius;
}
/**
* @param radius
* the radius to set
*/
public void setRadius(double radius) {
this.radius = radius;
}
/**
* Method to return the area of a circle
*
* @return area
*/
public double getArea() {
double tempRadius = getRadius();
double area = pi * tempRadius * tempRadius;
return area;
}
/**
* Method to calculate the diameter of a circle
*
* @return diameter
*/
public double getDiameter() {
double tempRadius = getRadius();
double diameter = tempRadius * 2;
return diameter;
}
/**
* Method to calculate the circumference of a circle
*
* @return circumference
*/
public double getCircumference() {
double tempRadius = getRadius();
double circumference = 2 * pi * tempRadius;
return circumference;
}
}
CircleTest.java
package com.chegg.test;
import java.util.InputMismatchException;
import java.util.Scanner;
public class CircleTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the radius of the circle");
// Reading input radius from the user
double rad = 0.0;
try {
rad = sc.nextDouble();
} catch (InputMismatchException ime) {
System.out.println("Enter the radius in numeric format only");
System.exit(0);
}
// Creating the object of a circle
Circle circle = new Circle(rad);
// Getting the area of a circle
System.out.println("The area of the circle:" + circle.getArea());
// Getting the diameter of a circle
System.out
.println("The diameter of the circle:" + circle.getDiameter());
// Getting the circumference of a circle
System.out.println("The circumference of the circle:"
+ circle.getCircumference());
// Closing the scanner.
sc.close();
}
}
Output:
Enter the radius of the circle
3
The area of the circle:28.2744
The diameter of the circle:6.0
The circumference of the circle:18.8496
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.