圆类与方法

时间:2016-10-07 07:43:05

标签: java class methods

我正在创建一个圆圈类程序,用于检查圆圈是否相互重叠。有六种初始给定方法:getter methods for x,y, and radius and setter methods for x,y, and radius(6)。还有两种额外的方法:getArea method(返回区域的值)和doesOverLap method(返回哪些圆重叠或不重叠)。此源文件夹中有许多演示,但以下是批量(借口缩进):

public class MyCircle 
{


    private double x;
    private double y;
    private double radius;

    private double other_x; //keeps user input for other_x
    private double other_y; //keeps user input for other_y
    private double other_radius;    //keeps user input for other_radius

    private double third_x; //keeps user input for third_x
    private double third_y; //keeps user input for third_y
    private double third_radius;    //keeps user input for third_radius


    /*
    The setX method stores a value in the x field. 
    @ param value The value to store in x.
    */
    public void setX(double value)
    {
        x = value;
    }

    /*
    The setY method stores a value in the y field.
    @ param value The value to store in y.
    */
    public void setY(double value)
    {
        y = value;
    }

    /*
    The setRadius method stores a value in the radius field.
    @param value The value to store in radius.
    */
    public void setRadius(double value) 
    {
        radius = value;
    }

        /*
        The getX method returns x's value. 
        @return The value in the x field.
        */
        public double getX()
        {
            return x; //returns value input for x
        }

        /*
        The getY method return y's value.
        @return The value in the y field.
        */
        public double getY()
        {
            return y; //returns value input for y
        }

        /*
        The getRadius method returns radius's value.
        @return The value in the radius field.
        */
        public double getRadius()
        {
            return radius; //returns value input for radius
        }

        /*
        The getArea method returns the circle object's area.
        @return The product of 3.14159 * radius * radius (area = 3.14159(pie) * radius * radius).
        */
        public double getArea()
        {       
            return radius * radius * 3.14159;
        }

        public boolean doesOverlap(MyCircleTest OtherCircleTest) 
        {
            double distance;

            distance = Math.sqrt(Math.pow(other_x - x, 2) + Math.pow(other_y - y, 2));
            return distance < other_radius + radius;


            if(distance < other_radius + radius)
            {
                System.out.println("My other circle overlaps my circle");
            }
            else
            {
                System.out.println("My other circle does not overlap my circle");
            }
        }



            /*
            The setOtherX method stores a value in the x field. 
            @ param value The value to store in x.
            */
            public void setOtherX(double value)
            {
                other_x = value;
            }

            /*
            The setOtherY method stores a value in the y field.
            @ param value The value to store in y.
            */
            public void setOtherY(double value)
            {
                other_y = value;
            }

            /*
            The setOtherRadius method stores a value in the radius field.
            @param value The value to store in radius.
            */
            public void setOtherRadius(double value) 
            {
                other_radius = value;
            }

            /*
            The getOtherX method returns x's value. 
            @return The value in the x field.
            */
            public double getOtherX()
            {
                return other_x; //returns value input for x
            }

            /*
            The getY method return y's value.
            @return The value in the y field.
            */
            public double getOtherY()
            {
                return other_y; //returns value input for y
            }

            /*
            The getRadius method returns radius's value.
            @return The value in the radius field.
            */
            public double getOtherRadius()
            {
                return other_radius; //returns value input for radius
            }

            /*
            The getArea method returns the circle object's area.
            @return The product of 3.14159 * radius * radius (area = 3.14159(pie) * radius * radius).
            */
            public double getOtherArea()
            {       
                return other_radius * other_radius * 3.14159;
            }

            public boolean doesOverlap(MyCircleTest OtherCircleTest) 
            {

                //Equation to see whether circles overlap

                double distance_2;

                distance_2 = Math.sqrt(Math.pow(other_x - third_x, 2) + Math.pow(other_y - third_y, 2));
                return distance < other_radius + third_radius;


                if(distance_2 < other_radius + third_radius)
                {
                    System.out.println("My other circle overlaps my third circle");
                }
                else
                {
                    System.out.println("My other circle does not overlap third circle");
                }

            }

                public void setThirdX(double value)
                {
                    third_x = value;
                }

                /*
                The setThirdY method stores a value in the third y field.
                @ param value The value to store in third y.
                */

                public void setThirdY(double value)
                {
                    third_y = value;
                }

                /*
                The setThirdRadius method stores a value in the third radius field.
                @param value The value to store in third radius.
                */

                public void setThirdRadius(double value) 
                {
                    third_radius = value;
                }

                    /*
                    The getThirdX method returns third x's value. 
                    @return The value in the third x field.
                    */
                    public double getThirdX()
                    {
                        return third_x; //returns value input for third x
                    }

                    /*
                    The getY method return third y's value.
                    @return The value in the third y field.
                    */

                    public double getThirdY()
                    {
                        return third_y; //returns value input for third y
                    }

                    /*
                    The getThirdRadius method returns third radius's value.
                    @return The value in the third radius field.
                    */

                    public double getThirdRadius()
                    {
                        return third_radius; //returns value input for third radius
                    }

                    /*
                    The getArea method returns the circle object's area.
                    @return The product of 3.14159 * radius * radius (area = 3.14159(pie) * radius * radius).
                    */
                    public double getThirdArea()
                    {       
                        return third_radius * third_radius * 3.14159;
                    }


                    public boolean doesOverlap (MyCircleTest ThirdCircleTest) 
                    {

                        //Equation to see whether circles overlap

                        double distance_3;

                        distance_3 = Math.sqrt(Math.pow(third_x - x, 2) + Math.pow(third_y - y, 2));
                        return distance_3 < third_radius + radius;


                        if(distance_3 < third_radius + radius)
                        {
                            System.out.println("My third circle overlaps circle");
                        }
                        else
                        {
                            System.out.println("My third circle does not overlap circle");
                        }

                    }
}

我在最终方法doOverLap方法上遇到麻烦,该方法应该说明程序的整体结果。它应说明两个圆是否重叠。另外,我应该显示每个圆的面积值,这在我的代码中不是。

要求如下: 有三个圆形物体,其中两个应重叠,其中两个不应重叠。将显示三个圆圈的区域。 doOverLap方法应该指出哪些圈子在一圈,哪些圈子没有。任何帮助将不胜感激。

暗示给定:如果两个圆的总和半径为+,则两个圆重叠。大于它们中心之间的距离。

1 个答案:

答案 0 :(得分:1)

首先,正如@JF Meier在评论中指出的那样,这不是OO方法。您应该有一个类Circle,其中包含仅与单个圆相关的数据和逻辑,然后创建可以使用的Circle类的多个实例。
此外,我不知道这个代码模板是由您自己提供还是创建的,但初始数据应该通过构造函数传递,而不是只有setter。这是有道理的,因为在我看来,圆圈应该是不变的。

例如:

public class Circle {
    private final double x;
    private final double y;
    private final double radius;

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

这样,您只需要getArea()doesOverlap(Circle other)的一种变体,因为现在这些方法中的每一种都“仅”属于一个Circle对象。

现在,您只需创建多个圈子的新实例,并在其上使用您的方法。

顺便说一句:如果您的某个方法(例如doesOverlap(Circle other))中有参数,请确保您实际上使用 other