定义一个圆,求圆的面积_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 定义一个圆,求圆的面积

定义一个圆,求圆的面积

 2017/6/24 5:32:07  i拾贝  程序员俱乐部  我要评论(0)
  • 摘要:定义一个圆,求圆的面积TestCircle.javaclassPoint{privatedoublex;privatedoubley;Point(doublex1,doubley1){x=x1;y=y1;}publicdoublegetX(){returnx;}publicdoublegetY(){returny;}publicvoidsetX(doublei){x=i;}publicvoidsetY(doublei){y=i;}}classCircle{privatePointo
  • 标签:一个

定义一个圆,求圆的面积

TestCircle?.java

class="java" name="code">class Point {
    private double x;
    private double y;
    Point(double x1, double y1) { 
    	x = x1; 
    	y = y1;
    }
    public double getX() { return x; }
    public double getY() { return y; }
    public void setX(double i) { x = i; }
    public void setY(double i) { y = i; }
}

class Circle {
    private Point o;	//圆心
    private double radius;	//半径
    Circle(Point p, double r) {
    	o = p; 
    	radius = r;
    }
    Circle(double r) {
        o = new Point(0.0, 0.0);
        radius = r;
    }
    
    boolean contains(Point p) {
    	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 void setO(double x, double y) {
        o.setX(x); 
        o.setY(y);
    }
    public Point getO() { return o; }
    public double getRadius() { return radius;}
    public void setRadius(double r) { radius = r;}
    public double area() { //求圆的面积
        return 3.14 * radius * radius;
    }
}

public class TestCircle {
    public static void main(String args[]) {
        Circle c1 = new Circle(new Point(1.0,2.0), 2.0);
        Circle c2 = new Circle(5.0);
        System.out.println("c1:("+c1.getO().getX()+","
            +c1.getO().getY()+"),"+c1.getRadius());	//c1:(1.0,2.0),2.0
        System.out.println("c2:("+c2.getO().getX()
            +","+c2.getO().getY()+"),"+c2.getRadius());//c2:(0.0,0.0),5.0
        System.out.println("c1 area = "+c1.area());//c1 area = 12.56
        System.out.println("c1 area = "+c2.area());//c1 area = 78.5
        c1.setO(5,6);
        c2.setRadius(9.0);
        System.out.println("c1:("+c1.getO().getX()+","
            +c1.getO().getY()+"),"+c1.getRadius());//c1:(5.0,6.0),2.0
        System.out.println("c2:("+c2.getO().getX()+","
            +c2.getO().getY()+"),"+c2.getRadius());//c2:(0.0,0.0),9.0
        System.out.println("c1 area = "+c1.area());//c1 area = 12.56
        System.out.println("c1 area = "+c2.area());//c1 area = 254.34
        
        Point p1 = new Point(5.2, 6.3);
        System.out.println(c1.contains(p1));//true
        System.out.println(c1.contains(new Point(10.0,9.0)));//false
        
    }
}

?

发表评论
用户名: 匿名