0

我必须使用外部值类来运行我的 main 方法。我的课外(圈课)如下:

  import java.text.DecimalFormat;

public class Circle
{       //start class Circle
    private double radius;      //stores user input for radius
    private int x;      //stores user input for x
    private int y;      //stores user input for y

    public Circle()
    {       //open Circle()
    }       //close Circle()

    public Circle (double radius)
    {       //start Circle
        setRadius(radius);
    }       //end Circle(double)

    public Circle (int x, int y)
    {       //start Circle
        setX(x);
        setY(y);
    }       //end Circle(int, int)

    public Circle (double radius, int x, int y)
    {               //start Circle
        setX(x);
        setY(y);    
        if (radius > 0)     //makes sure radius has a correct value
        {
            this.radius=radius;     //stores the input radius if it is positive
        }
    }       // end Circle (double, int, int)

    public void setRadius(double radius)
    {       //start setRadius
        if (radius > 0)     //stores correct variable input for radius
        {
            this.radius=radius;
        }
    }       //end setRadius(double)

    public void setX(int x)
    {       //start setX
        setX(x);        //stores X (user input) for x
    }       //end setX(int)

    public void setY(int y)
    {       //start setY        
        setY(y);        //stores Y (user input) for y
    }       //end setY(int)

    public void setXY(int x, int y)
    {       //start setXY
        setXY(x,y);     //stores values for X and Y
    }       //end setXY

    public double getRadius()
    {       //start getRadius
        double dVal;        //stores the value for radius
        dVal=this.radius;       //stores the value for radius for the input circle

        return dVal;        //returns the value for radius
    }       //end getRadius()

    public int getX()
    {       //start getX
         return x;      //returns the value of x
    }       //end getX()

    public int getY()
    {       //starts getY
        return y;       //returns the value of y
    }       //ends getY

    public double area()
    {       //start area()
        double dArea;       //stores the value of dArea

        dArea= radius * radius * 3.141599999;   //how dArea is calculated

        return dArea;       //returns the value stored to dArea
    }  //end area ()

    public double circumference()
    {       //start circumference
        double dCircum;     //stores the value of dCircumference

        dCircum= (2* radius) * 3.1415999999;        //how dCircumerence is calculated

        return dCircum;     //returns the calculated value of dCircumference
    }  //end circumference()

    public String toString()
    {       //start toString
        DecimalFormat df= new DecimalFormat ("0.000");
        double dCircum= circumference();
        double dArea= area();
        String strStuff;
        strStuff= "\nRadius: " +this.radius;
        strStuff+= "\nXCoord: " + x;
        strStuff+= "\nYCoord: " + y;
        strStuff+= "\nArea: " + df.format(dArea);
        strStuff+="\nCircumference: " + df.format(dCircum);
        strStuff+="\n\n";

        return strStuff;        //returns the values of the class
    }       //end toString
}   

//end Circle class

以下是我的TestCircle类
公共类TestCircle

    {       //start TestCircle
public static void main(String[] args)
{       //starts main
    Circle c1= new Circle(7,0,14);


    double dRad;        //stores a value to dRad
    dRad= c1.area();        //runs area() for the stored values
}       //ends main
}           //ends TestCircle

它们都编译正确,但是当我运行我的 TestCircle 类时,它只是一遍又一遍地在“Circle.setX(Circle.java.44) 处发出这个。我猜这与我在第 44 行设置 X 的位置有关但我不知道是什么。请帮忙。

4

2 回答 2

2

您的setX方法只是调用自己,这使它调用自己,然后调用自己,依此类推,直到您的 Java 实例用完堆栈空间。你需要做setX这样的事情。

public void setX(int x){
    this.x = x;
}

setY和会有类似的问题setXY

于 2013-10-29T02:49:43.767 回答
0

您在 setX() 方法中调用 setX() 方法。这是一个递归调用。由于没有条件它会永远运行,你会得到一个堆栈溢出异常

只需在该方法中将传递的值设置为您的实例变量

public void setX(int x){
   this.x = x;
}
于 2013-10-29T04:02:12.293 回答