java例程练习(Circle类和Point类柔和练习)

public class Test {
	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());
		System.out.println("c2:(" + c2.getO().getX() + ","
				+ c2.getO().getY() + ")," + c2.getRadius());
		
		System.out.println("c1 are is :" + c1.area());
		System.out.println("c2 are is :" + c2.area());
		
		c1.setO(5, 6);
		c2.setRadius(9.0);
		
		System.out.println("c1:(" + c1.getO().getX() + ","
				+ c1.getO().getY() + ")," + c1.getRadius());
		System.out.println("c2:(" + c2.getO().getX() + ","
				+ c2.getO().getY() + ")," + c2.getRadius());
		
		System.out.println("c1 are is :" + c1.area());
		System.out.println("c2 are is :" + c2.area());
		
		Point p1 = new Point(5.2, 6.3);
		System.out.println(c1.contains(p1));
		System.out.println(c1.contains(new Point(10.0, 9.0)));
		
	}
}


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

class Circle {
	private Point o;
	private double radius;
		
	public void setO(double x, double y) {
		o.setX(x);
		o.setY(y);
	}
	
	public Point getO() {
		return o;
	}
	
	public void setRadius(double r) {
		radius = r;
	}
	
	public double getRadius() {
		return radius;
	}
	
	Circle(Point o, double radius) {
		this.o = o;
		setRadius(radius);
	}
	
	Circle(double radius) {
		o = new Point(0.0, 0.0);
		setRadius(radius);
	}
	
	public double area() {
		return 3.14 * radius * radius;
	}
	
	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;
		}
	}
	
}





------------------------------------------------华丽的分割线---------------------------------------------------------------------------------------------------

----------------------------------改进的例子

class TestCirclePoint 
{
	public static void main(String[] args) 
	{
		Circle c1 = new Circle(new Point(1, 2), 2.0);
		Circle c2 = new Circle(5.0);

		System.out.println(c1.display());
		System.out.println(c2.display());

		System.out.println(c1.getArea());
		System.out.println(c2.getArea());

		c1.setO(5, 6);
		c2.setRadius(9.0);

		System.out.println(c1.display());
		System.out.println(c2.display());

		System.out.println(c1.getArea());
		System.out.println(c1.getArea());

		Point p1 = new Point(5, 6);
		System.out.println(c1.isContains(p1));
		System.out.println(c1.isContains(new Point(10, 9)));
	}
}

class Point
{
	private int x;
	private int y;

	Point(int x, int y)
	{
		this.setX(x);
		this.setY(y);
	}

	public int getX() 
	{
		return x;
	}

	public int getY()
	{
		return y;
	}

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

	public void setY(int y)
	{
		this.y = y;
	}

	public String display() 
	{
		return "(" + x + "," + y + ")";
	}
}

class Circle
{
	private Point o;
	private double radius;

	Circle(double radius)
	{
		this.setO(new Point(0, 0));
		this.setRadius(radius);
	}

	Circle(Point o, double radius)
	{
		this.o = o;
		this.radius = radius;
	}

	public Point getO()
	{
		return o;
	}

	public void setO(Point p)
	{
		this.o = p;
	}

	public void setO(int x, int y)
	{
		o.setX(x);
		o.setY(y);
	}

	public double getRadius()
	{
		return radius;
	}

	public void setRadius(double radius)
	{
		this.radius = radius;
	}

	public double getArea()
	{
		return Math.PI * radius * radius;
	}

	public boolean isContains(Point p) 
	{
		double x = o.getX() - p.getX();
		double y = o.getY() - p.getY();

		double distance = x * x + y * y;

		if(distance < radius * radius)
		{
			return true;
		}

		return false;
	}

	public String display()
	{
		return "O:" + o.display() + "R:" + radius;
	}
}


你可能感兴趣的:(java,c,String,Class)