Question

1.modify the first constructor to compute the diameter and the area.

2.modify the second constructor to setRadius and calcuate area without calling the setRadius

make two new methods in the class

i. findArea

ii. findDiameter.

modify testcircle to accommodate change.

    public class Circle 
{
private double radius, area, diameter;
    public Circle() 

    {
        radius = 1;
        diameter = radius * 2;
        area = Math.PI * radius * radius;
    }
    public Circle(double myradius) 

    {
        myradius = radius;
        area = Math.PI * radius * radius;
    }

    public void setRadius(double aRadius)
    {
        this.radius = aRadius;
        diameter = radius * 2;
        area = Math.PI * radius * radius;
    }

    public void CalArea(double aArea)
    {
        area = Math.PI * radius * radius;
    }

    public void CalDiameter(double aDiameter)
    {
        diameter = radius * 2;
    }

     public double getRadius()
    {
        return radius;
    }
    public double getDiameter()
    {
        return diameter;
    }
    public double getArea()
    {
       return area;
    } 
}        


    public class TestCircle {

public static void main(String[] args) 
{
    Circle circle1; //Declaring an object(instance) of the class Circle.
        double myRadius, myDiameter, myArea;

        circle1 = new Circle();  

        myRadius = circle1.getRadius();
        System.out.println(myRadius);

        circle1.setRadius(5);
        myRadius = circle1.getRadius();
        System.out.println(myRadius);

        myDiameter = circle1.getDiameter();
        System.out.println(myDiameter);

        myArea = circle1.getArea();
        System.out.println(myArea);

        Circle circle2;

        circle2 = new Circle(2.5);

        myRadius = circle2.getRadius();
        System.out.println(myRadius);

        myDiameter = circle2.getDiameter();
        System.out.println(myDiameter);

        myArea = circle2.getArea();
        System.out.println(myArea);       
     }
}

i've done the first task but calcuating area without calling the setRadius seems a bit tricky for me, or im going about it the wrong way. Would really appreciate the help/advice.

update

playing around with it i think i found my problem

    public class Circle 
    {
    private double radius, area, diameter;
    public Circle() 
    {
        radius = 1;
        diameter = radius * 2;
        area = Math.PI * radius * radius;
    }
    public Circle(double myradius) 
    {
        this.radius = myradius;
    }

    public void setRadius(double aRadius)
    {
        this.radius = aRadius;
        diameter = radius * 2;
        area = Math.PI * radius * radius;
    }

    public double findDiameter() 
      {
        return radius*2;
      }

    public double findArea() 
    {
        return Math.PI * radius * radius;
    }

     public double getRadius()
    {
        return radius;
    }
    public double getDiameter()
    {
        return diameter;
    }
    public double getArea()
    {
       return area;
    } 
    }

then i made the necessary changes to the main

Était-ce utile?

La solution

This homework exercise means to teach you about the states on an object, and how to avoid redundancy.

As I see it: the goal is to

  1. be able calculate diameter and area, and store it in the object
  2. get rid of the redundant states and calculate the values when needed

The circle has one free attribue (the radius) and two derived attributes (diameter and area) which can be calculated anytime when the radius is known. That means you can just store the radius, and implement the findArea/findDiameter methods such that the result is calculated from the radius and then returned:

public double findDiameter() {
    return radius*2;
}

public double findArea() {
    return Math.PI * radius * radius;
}

Note (naming conventions): the prefix get indicates that the value can be accessed locally and in an inexpensive way. When the value is expensive to be calculated or loaded, use a different prefix (that way you know about the costs when using the same value often, and can store it as an intermediate result). That's why the methods to be written in this example are prefixed by find (although I'd use a different prefix personally, that one looks like the data is retrieved by searching it, and in the concrete case here I'd consider the calcluation to be cheap enough to use a get prefix).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top