TestCircle程序分析

//定义一个点类Point,有两个实型点
//定义一个圆类,包括一个点和一个半径,有两个构造方法分别是传入一个点和半径,和传入一个点,在圆类中可以设置点的位置
//有个包含方法,判断一个点是否在圆中
//返回一个点方法
//返回半径方法
//设置半径方法
//求圆面积

//定义两个圆,获取圆心和半径,面积
//重新设置两个圆的点和半径

//判断点是否在圆中

class Point
{
    private double x,y;
    Point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }
    public double getX()
    {
        return x;
    }
    public double getY()
    {
        return y;
    }
    public void setX(double _x)
    {
        x=_x;
    }
    public void setY(double _y)
    {
        y=_y;
    }            
}

class Cricle
{
    private Point p;
    double radius;
    public Cricle(Point o,double r)
    {
        p=o;
        radius=r;
    }
    public Cricle(double r)
    {
        p=new Point(0,0);
        radius=r;
    }
    public void setO(double x,double y)
    {
        p.setX(x);
        p.setY(y);
    }
    public Point getO()
    {        
        return p;
    }
    public double getRadius()
    {
        return radius;
    }
    public void setRadius(double r)
    {
        radius=r;
    }
    
    
    public double area()
    {
        return 3.14*radius*radius;
    }
    
    boolean contains(Point o)
    {        
        double x=p.getX()-o.getX();
        double y=p.getY()-o.getY();
        if (x*x+y*y>radius*radius) return false;
        else return true;
        
    }
    
}


public class TestCircle {
    public static void main(String[] args) 
    {
        Cricle c1=new Cricle(new Point(1.0,2.0),2);
        Cricle c2=new Cricle(5);
        
        System.out.println(c1.getO().getX()+" "+c1.getO().getY()+" "+c1.getRadius()+" "+c1.area());
        System.out.println(c2.getO().getX()+" "+c2.getO().getY()+" "+c2.getRadius()+" "+c2.area());
        
        c1.setO(5, 6);
        c2.setRadius(9.0);
        
        System.out.println(c1.getO().getX()+" "+c1.getO().getY()+" "+c1.getRadius()+" "+c1.area());
        System.out.println(c2.getO().getX()+" "+c2.getO().getY()+" "+c2.getRadius()+" "+c2.area());

        Point p=new Point(5.2,6.3);
        System.out.println(c1.contains(p));
        System.out.println(c1.contains(new Point(100,106)));
        
    }
}

 

posted @ 2013-03-28 14:57  glt66  阅读(261)  评论(0编辑  收藏  举报