Circle Class Input Validation: Do not accept a negative value for the radius. Wr
ID: 3595681 • Letter: C
Question
Circle Class Input Validation: Do not accept a negative value for the radius. 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. You are given two files "HoursWorked.txt" which contains employee ID and hours worked for the week and "HourlyRate.txt" which contain employee ID and hourly rate. Write a program to produce a file and report which will list only employees who worked, their hourly rate and total hours worked for the week and pay for the week. The report and output file should contain one record per employee who worked for the week. setRadius. A mutator function for the radius variable getRadius. An accessor function for the radius variable 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 circle's radius, creating a Circle object, and then reporting the circle's area, diameter, and circumference.Explanation / Answer
import java.util.Scanner;
public class Circle {
private double radius;
private final double PI = 3.14;
public Circle() {
super();
this.radius = 0.0;
}
public Circle(double radius) {
super();
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return PI*radius*radius;
}
public double getDiamemeter() {
return 2*radius;
}
public double getCircumference() {
return 2*PI*radius;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter radius , which should a positive number ");
double radius ;
radius = sc.nextDouble();
if(radius < 0) {
System.out.println("radius is negative number ");
return;
}
Circle circle = new Circle(radius);
if(radius >= 0) {
System.out.println("Area of circle is "+ circle.getArea());
System.out.println("Diameter of circle is "+ circle.getDiamemeter() );
System.out.println("Circumference of circle is "+ circle.getCircumference());
}
}
}
Output:
Enter radius , which should a positive number
45.3
Area of circle is 6443.562599999999
Diameter of circle is 90.6
Circumference of circle is 284.484
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.