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

1. Complete the following tasks: a. Design a class named Circle with ?elds named

ID: 3528490 • Letter: 1

Question

1. Complete the following tasks: a. Design a class named Circle with ?elds named radius, area, and diameter. Include a constructor that sets the radius to 1. Include get methods for each ?eld, but include a set method only for the radius. When the radius is set, do not allow it to be zero or a negative number. When the radius is set, calculate the diameter (twice the radius) and the area (the radius squared times pi, which is approximately 3.14). Create the class diagram and write the pseudocode that de?nes the class. b. Design an application that declares two Circles. Set the radius of one manually, but allow the other to use the default value supplied by the constructor. Then, display each Circle

Explanation / Answer


public class Circle {

float radius;
float diameter;
float area;

public Circle() {
this.radius = 1;
}

public Circle(float radius) {
this.radius = radius;
}

public float getRadius() {
return radius;
}


public void setRadius(float radius) {
if(radius > 0) {
this.radius = radius;
this.diameter = radius * 2;
this.area = (float) 3.14 * radius * radius;
}
else
System.out.println("Enter a positive value for radius");
}
public float getDiameter() {
return diameter;
}

public float getArea() {
return area;
}


}

class TestCircle {
public static void main (String[] args) throws Exception {
Circle cir1 = new Circle(); //default radius = 1;

Circle cir2 = new Circle();
cir2.setRadius(2);
System.out.println("Circle -1 : Radius : "+cir1.getRadius()+" Diameter : "+cir1.getDiameter()+" Area: "+cir1.getArea());
System.out.println("Circle -2 : Radius : "+cir2.getRadius()+" Diameter : "+cir2.getDiameter()+" Area: "+cir2.getArea());
}
}