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

Define a class to model an abstract circle. A circle is defined by the value of

ID: 3545975 • Letter: D

Question


Define a class to model an abstract circle. A circle is defined by the value of its radius. Your class should

have one instance variable of type double. The name of the class is Circle. In addition to writing

your own default, parameterless constructor, include a constructor with a single argument that is used

to set the instance variable of an object of the Circle class. Note that the radius of a circle must be

greater than zero and your class should uphold this restriction.


Define accessor(getter) and mutator (setter)methods. In addition, define the following accessor

methods:

Explanation / Answer

//Circle.java

public class Circle {
    private double r;
   
    public Circle() {
        r = 1.0;
    }
   
    public Circle(double r) {
        if (r <= 0)
            throw new IllegalArgumentException("Radius must be greater than zero");
        this.r = r;
    }
   
    public double getRadius() {
        return r;
    }
   
    public void setRadius(double r) {
        if (r <= 0)
            throw new IllegalArgumentException("Radius must be greater than zero");
        this.r = r;
    }
   
    public double getDiameter() {
        return 2*r;
    }
   
    public double getCircumference() {
        return 2*Math.PI*r;
    }
   
    public double getArea() {
        return Math.PI*r*r;
    }
   
    public String toString() {
        return "Circle radius: " + r;
    }
}



//CircleDriver.java

public class CircleDriver {
    public static void main(String[] args) {
        Circle[] circles = new Circle[10];
        try{
            circles[1] = new Circle();
            circles[2] = new Circle(2.0);
            circles[3] = new Circle(3.5);
            circles[4] = new Circle(5.0);
            circles[5] = new Circle(0.000001);
            circles[6] = new Circle(15);
            circles[7] = new Circle(25);
            circles[8] = new Circle(-7);
            circles[9] = new Circle(-10.0);
        }
        catch (IllegalArgumentException ex){
            System.out.println("******** " + ex.getMessage() + " *********");
        }
        System.out.println("Initial call to toString():");
        for(Circle c : circles)
            System.out.println(" " + c);
       
        System.out.println("Call to getRadius (should be same as above):");
        for(Circle r : circles)
            if(r != null)
                System.out.println(" " + r.getRadius());
       
        System.out.println("Call to getDiameter (should be twice the value shown above):");
        for(Circle d : circles)
            if(d != null)
                System.out.println(" " + d.getDiameter());
       
        System.out.println("Calls to getCircumference:");
        System.out.println(" Should be 2 * PI: " + circles[1].getCircumference());
        System.out.println(" Should be approximately 0.000006: " + circles[5].getCircumference());
       
        System.out.println(" Call to getArea:");
        System.out.println(" Should be PI: " + circles[1].getArea());
        System.out.println(" Should be approximately 3.14159E-12: " + circles[5].getArea());
       
        System.out.println("Testing out the setRadius method:");
        for(int i = 0; i < circles.length/2; i++)
            if(circles[i] != null)
                circles[i].setRadius(i);
       
        System.out.println("Call to toString after setting the first half of the objects:");
        for(Circle c : circles)
            System.out.println(" " + c);
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote