开发者

Circle implementation - How to model in order to fetch this information

开发者 https://www.devze.com 2023-02-02 22:53 出处:网络
1. Find circle diameter from radius 2. Find circle diameter from perimeter 3. Find circle diameter from area
1. Find circle diameter from radius
2. Find circle diameter from perimeter
3. Find circle diameter from area

4. Find circle perimeter from diameter
5. Find circle perimeter from radius

6. Find circle radius from diameter
7. Find circle radius from perimeter
8. Find circle radius from area

Currently our model class is implemented like this..

class Circle {
  double radius;

  Circle (double r) {
  }

// Solves 6,7,8
      double getDiameter() {}
      double getPerimeter() {}
      double getArea() {}

// static functions to solve 1-开发者_如何学Python5
// e.g. public static double getPermiter(double diameter) {..}
    }

Is there a better way to model the above class, so that I can fetch the above information, since given a certain parameter (e.g. radius, diameter, area or perimeter) the user is expected to find other information.


I'd probably prefer a Circle class with a constructor using the radius. You can add static factory methods to create instances from the circumference and area.

public class Circle
{
    double radius;

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

    public static Circle fromCircumference(double circumference)
    {
        return new Circle(circumference / (Math.PI * 2));
    }

    public static Circle fromArea(double area)
    {
        return new Circle(Math.sqrt(area / Math.PI));
    }

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

    public double getRadius()
    {
        return radius;
    }

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

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


I would implement all 8 methods as static public methods inside the Circle class (or maybe in an associate helper class) and also declare the get{Diameter,Perimeter,radius} by calling the proper static methods defined above:

public class Circle {
  double radius;

  public double getPerimeter() {
    return getPerimeterFromRadius(radius);
  }

  // ... 

  public static double getPerimeterFromRadius(double radius) {
    return 2 * Math.PI * radius;
  }

}


As I understand, you don't always need a circle itself, but, for instance, you want to count potential circle area by given radius.

I'd rather move all these static functions to a separate utility class. By convention, such class name is a plural form of a class or interface with which it's used. So, it will be named Circles.

public class Circles {

    public static double diameterFromRadius(double radius) {...}

    public static double diameterFromPerimeter(double perimeter) {...}

    public static double perimeterFromArea(double area) {...}

    ...
}

If you still need class Circle for some reasons then consider to make your circle immutable (if it is not) i.e. remove any setters and make all fields final.

public class Circle {
  final double radius;

  public Circle (double r) {
      this.radius = r;
  }

  public double getRadius() {
      return radius;
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消