定义一个circle类,其中包含计算圆周长和面积的方法。若输入的半径小于0,就要引发一个自定义异常。
  小弟刚学习JAVA,这一题书上的答案我认为给错了,但是自己又写不出来,请各位高手帮帮忙,谢谢啊!!!

解决方案 »

  1.   

    if(Integer.parseInt(args[0]) <0)
    {
    throw new YourException() ;
    }
      

  2.   

    public class Circle 
    {
       double radius = 0;   public Circle(double radius) throws YourException
       {
            if(radius <0)
            {
                throw new YourException() ;
            }
            else
            {
                this.radius = radius;
            }
       }   public double getPerimeter()
       {
            return Math.PI * 2 * radius;
       }   public double getArea()
       {
            return radius * radius * Math.PI;
       }
    }