Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1. Write a class called Circle.java which has a single field of type double

ID: 3548318 • Letter: P

Question

                    Part 1. Write a class called Circle.java which has a single field of type double called radius.
                    Circle.java should include the following instance (non-static) methods:
                

                

                    
                

                

                    -A default (no argument) constructor public Circle()which constructs a (unit) circle of radius 1.
                    -An overloaded constructor public Circle(double newRadius)that accepts a value for the radius.
                    -An accessor public double getRadius()which returns the radius of the circle.
                    -A mutator public void setRadius(double newRadius)which accepts a value for the radius.
                    -An accessor public double calculateDiameter()which returns the diameter of the circle.
                    -An accessor public double calculateCircumference()which returns the circumference.
                    -An accessor public double calculateArea()which returns the area of the circle.
                    - A public String toString() method which returns a String representation of the circle such as

Explanation / Answer

Exactly the same way as specified with the exact output.....


CODE :

class Circle {


private double radius;

public Circle() {

radius = 1;

}

public Circle(double newRadius) {

this.radius = newRadius;

}

public double getRadius() {

return radius;

}

public void setRadius(double radius) {

this.radius = radius;

}

public double calculateDiameter(double radius){

if(radius < 0)throw new IllegalArgumentException();

double diameter;

diameter = radius * 2;

return diameter;

}

public double calculateCircumference(double radius){

if(radius < 0)throw new IllegalArgumentException();

double circumference;

circumference = Math.PI * radius * 2;

return circumference;

}

public double calculateArea(double radius){

if(radius < 0)throw new IllegalArgumentException();

double area;

area = Math.PI * Math.pow(radius, 2);

return area;

}

public String toString() {

double roundOffVal = Math.round(radius * 100.0) / 100.0;

String str = "Radius : " + roundOffVal;

return str;

}

public boolean equals(Circle c) {

if(this.radius == c.radius){

return true;

}else{

return false;

}

}

}


public class CircleMain {

public static void main(String[] args) {

Double radius;

introduction();

double rad = Integer.MAX_VALUE;

while (rad > 0){

try{

String str = getUserInput();

radius = Double.parseDouble(str);

if(radius != 0 && radius > 0){

System.out.println("Here is your new Circle : ");

Circle c = new Circle(radius);

System.out.println("Radius : " + radius);

System.out.println(" Diameter : " + Math.round((c.calculateDiameter(radius)) * 100.0) / 100.0 +

" Circumference : " + Math.round((c.calculateCircumference(radius)) * 100.0) / 100.0 +

" Area: " + Math.round((c.calculateArea(radius)) * 100.0) / 100.0);

rad = radius;

}else if(radius < 0){

System.out.println("The radius must be a positive number; please try again.");

continue;

}

else{

exitMessage();

System.exit(0);

}

}catch (NumberFormatException e) {

System.out.println("Not a number; try again.");

continue;

}

}

}


private static void exitMessage() {

System.out.println("Thank you for using the Circle program.");

}

private static String getUserInput(){

String line = null;

Scanner in = new Scanner(System.in);

System.out.println("Enter a radius (0 to quit) : ");

line = in.nextLine();

return line;

}

private static void introduction() {

System.out.println("This program will prompt for the radius of a circle" +

"and display the circle's diameter, circumference, and area." +

"The program will repeat until the user chooses to quit.");

}

}


OUTPUT :


This program will prompt for the radius of a circleand display the circle's diameter, circumference, and area.The program will repeat until the user chooses to quit.

Enter a radius (0 to quit) :

1

Here is your new Circle :

Radius : 1.0

Diameter : 2.0 Circumference : 6.28 Area: 3.14

Enter a radius (0 to quit) :

5.5

Here is your new Circle :

Radius : 5.5

Diameter : 11.0 Circumference : 34.56 Area: 95.03

Enter a radius (0 to quit) :

-4

The radius must be a positive number; please try again.

Enter a radius (0 to quit) :

hello

Not a number; try again.

Enter a radius (0 to quit) :

0

Thank you for using the Circle program.