【项目四】Point类、Graphic抽象类、 Rect类、Circle类 + GraphicOperate接口

题目描述

Point类、Graphic抽象类、 Rect类、Circle类基础上:

  • 定义接口:GraphicOperate,用于完成图形对象的操作
    • 成员有:
      • int compareTo(Graphic):比较两个图形的关系(重合、相交、不相交)
      • boolean pointIsIn(Point):判断参数点坐标是否在图形区域中
  • 要求Rect和Circle两个类实现该接口。

核心思想

  • 接口实现关键字:implements
  • 注意:
    (1). 接口可以被多重实现(implements),抽象类只能被单一继承(extends)
    (2). 接口只有定义,抽象类可以有定义和实现
    (3). 接口的字段定义默认为:public static final, 抽象类字段默认是”friendly”(本包可见)

代码实现

  • GraphicOperate.java接口
/**
 * 接口
 */
public interface GraphicOperate {
	//比较两个图形的关系(重合、相交、不相交)
	int compareTo(Graphic graphic);
	//判断参数点坐标是否在图形区域中 
	boolean pointIsIn(Point point);
}
  • Point.java
public class Point {
	double x;
	double y;

	public Point() {
		this.x = 0;
		this.y = 0;
	}

	public Point(double x, double y) {
		super();
		this.x = x;
		this.y = y;
	}

	public double getX() {
		return x;
	}

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

	public double getY() {
		return y;
	}

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

	/**
	 * 两点之间的距离
	 * 
	 * @param p
	 * @return
	 */
	double distanceTo(Point p) {
		return (int) (Math.sqrt(Math.pow((this.x - p.x), 2)) + Math.pow((this.y - p.y), 2));
	}

	/**
	 * 中心点
	 * 
	 * @param p
	 * @return
	 */
	Point centerPoint(Point p) {
		Point midPoint = new Point();
		midPoint.setX((this.x + p.x) / 2);
		midPoint.setY((this.y + p.y) / 2);
		return midPoint;
	}

}
  • Graphic.java
public abstract class Graphic {
	String color;

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public Graphic() {

	}

	public Graphic(String color) {
		this.color = color;
	}

	void printInfo() {
		System.out.println("color="+this.color);
	}
	
	abstract double getArea();

	abstract double getCircum();
}
  • Rect.java
public class Rect extends Graphic implements GraphicOperate {

	Point lefttop;
	Point rightbottom;

	public Point getLefttop() {
		return lefttop;
	}

	public void setLefttop(Point lefttop) {
		this.lefttop = lefttop;
	}

	public Point getRightbottom() {
		return rightbottom;
	}

	public void setRightbottom(Point rightbottom) {
		this.rightbottom = rightbottom;
	}

	public Rect() {
		super();
		this.lefttop = new Point();
		this.rightbottom = new Point();
	}

	public Rect(String color, Point lefttop, Point rightbottom) {
		this.color = color;
		this.lefttop = lefttop;
		this.rightbottom = rightbottom;
	}

	public Rect(String color, int x_left, int y_top, int x_right, int y_bottom) {
		this.color = color;
		this.lefttop = new Point(x_left, y_top);
		this.rightbottom = new Point(x_right, y_bottom);
	}

	@Override
	double getArea() {
		// 面积 = 宽*高
		return this.getWidth() * this.getHeight();
	}

	@Override
	double getCircum() {
		// 周长=2 *(宽 + 高)
		return 2 * (this.getWidth() + this.getHeight());
	}

	@Override
	void printInfo() {
		if (isRect()) {
			System.out.println("宽:" + this.getWidth() + " 高:" + this.getHeight() + " 周长:" + this.getCircum() + " 面积:"
					+ this.getArea() + " 颜色:" + this.color);
		} else {
			System.out.println("这不是长方形");
		}
	}

	// 长方形的高
	double getHeight() {
		return Math.abs(this.lefttop.getX() - this.rightbottom.getX());
	}

	// 长方形的宽
	double getWidth() {
		return Math.abs(this.lefttop.getY() - this.rightbottom.getY());
	}

	/**
	 * 判断是否是长方形 非长方形条件:x1=x2 或者是 y1=y2
	 * 
	 * @return 是/否
	 */
	boolean isRect() {
		if (this.lefttop.getX() == this.rightbottom.getX() || this.lefttop.getY() == this.rightbottom.getY()) {
			return false;
		} else {
			return true;
		}
	}

	//1重合 2不想交 3相交
	@Override
	public int compareTo(Graphic graphic) {
		Rect rect = (Rect) graphic;
		if ((this.getLefttop().getX() == rect.getLefttop().getX()
				&& this.getLefttop().getY() == rect.getLefttop().getY()
				&& this.getRightbottom().getX() == rect.getRightbottom().getX()
				&& this.getRightbottom().getY() == rect.getRightbottom().getY())
				|| (this.getLefttop().getX() == rect.getRightbottom().getX()
						&& this.getLefttop().getY() == rect.getRightbottom().getY()
						&& this.getRightbottom().getX() == rect.getLefttop().getX()
						&& this.getRightbottom().getY() == rect.getLefttop().getY())) {
			return 1;
		}else if ((Math.max(this.getLefttop().getX(), this.getRightbottom().getX()) < Math.min(rect.getLefttop().getX(), rect.getRightbottom().getX()))||
				(Math.min(this.getLefttop().getX(), this.getRightbottom().getX()) > Math.max(rect.getLefttop().getX(), rect.getRightbottom().getX()))||
				(Math.max(this.getLefttop().getY(), this.getRightbottom().getY()) < Math.min(rect.getLefttop().getY(), rect.getRightbottom().getY()))||
				(Math.min(this.getLefttop().getY(), this.getRightbottom().getY()) > Math.max(rect.getLefttop().getY(), rect.getRightbottom().getY()))) {
			return 2;
		} else {
			return 3;
		}
	}

	@Override
	public boolean pointIsIn(Point point) {
		if (Math.min(this.getLefttop().getX(), this.getRightbottom().getX())<point.getX() && 
				point.getX()<Math.max(this.getLefttop().getX(), this.getRightbottom().getX()) &&
				Math.min(this.getLefttop().getY(), this.getRightbottom().getY())<point.getY() && 
				point.getY()<Math.max(this.getLefttop().getY(), this.getRightbottom().getY())) {
			return true;
		}else {			
			return false;
		}
	}

}
  • Circle.java
public class CirCle extends Graphic implements GraphicOperate {
	Point center;
	int r;

	
	
	public CirCle() {
		this.color = "无色";
		this.center = new Point();
		this.r = 0;
	}


	public CirCle(String color,Point center, int r) {
		this.color = color;
		this.center = center;
		this.r = r;
	}

	public int getR() {
		return r;
	}

	public void setR(int r) {
		this.r = r;
	}

	public Point getCenter() {
		return center;
	}

	public void setCenter(Point center) {
		this.center = center;
	}

	@Override
	double getArea() {
		return Math.PI * r * r;
	}

	@Override
	double getCircum() {
		return 2 * Math.PI * r;
	}

	@Override
	void printInfo() {
		System.out.println("颜色:"+this.color + " 圆心:" + "("+this.center.getX()+","+this.center.getY()+")"+" 半径:"+ this.r +" 周长:"+getCircum()+" 面积:"+getArea());
	}
	
	/**
	 * 设置圆的参数信息
	 * 
	 * @param color  颜色
	 * @param center 圆心
	 * @param r      半径
	 */
	void setinfo(String color, Point center, int r) {
		this.color = color;
		this.center = center;
		this.r = r;
	}

	/**
	 * 改变圆的大小
	 * 
	 * @param num 放大或缩小比例
	 */
	void changeSize(int num) {
		this.r = r * num;
	}


	//1重合:中心点在一个位置 半径相等
	//2相交:圆心间的距离 < 半径之和 
	//3相离:圆心间的距离 > 半径之和
	@Override
	public int compareTo(Graphic graphic) {
		CirCle cirCle = (CirCle)graphic;
		//两个圆心间的距离
		double rAndR = this.getCenter().distanceTo(cirCle.getCenter());
		if (this.getR()==cirCle.getR() && this.getCenter().getX() == cirCle.getCenter().getX() && this.getCenter().getY()==cirCle.getCenter().getY()) {
			return 1;
		}else if (rAndR < this.getR()+cirCle.getR()) {
			return 2;
		}else {
			return 3;
		}
	}


	@Override
	public boolean pointIsIn(Point point) {
		//点在圆内:点和圆心的距离<=5
		if (this.getCenter().distanceTo(point )<= 5) {
			return true;
		}else {
			return false;
		}
	}
	
}
  • Test.java
public class Test {
	public static void main(String[] args) {
		//无参构造测试
		Rect rect1 = new Rect();
		rect1.setColor("红色");
		rect1.setLefttop(new Point(1,1));
		rect1.setRightbottom(new Point(3,4));
		
		rect1.printInfo();
		//Rect(String color, Point lefttop, Point rightbottom)测试
		Rect rect2 = new Rect("白色",new Point(1,1),new Point(2, 2));
		rect2.printInfo();
		//Rect(String color, int x_left, int y_top, int x_right, int y_bottom)测试
		Rect rect3 = new Rect("黄色",2,2,4,4);
		rect3.printInfo();
		
		Rect rect4 = new Rect("绿色",2,2,2,9);
		rect4.printInfo();
		Rect rect5 = new Rect("黑色",4,4,7,7);
		
		Point point1 = new Point(1.2,1.2);
		System.out.println(rect1.pointIsIn(point1));
		System.out.println(rect2.pointIsIn(point1));
		System.out.println(rect3.pointIsIn(point1));
		//长方形重合
		System.out.println(rect1.compareTo(rect1));
		//长方形相交
		System.out.println(rect1.compareTo(rect2));
		//长方形相离
		System.out.println(rect1.compareTo(rect5));
		
		//Circle 测试
		CirCle cirCle1 = new CirCle();
		cirCle1.printInfo();
		
		CirCle cirCle2  = new CirCle("红色",new Point(1,1),3);
		cirCle2.printInfo();
		//重合测试
		CirCle cirCle3 = new CirCle("黑色",new Point(1,1),3);
		System.out.println(cirCle2.compareTo(cirCle3));
		//相交测试
		CirCle cirCle4 = new CirCle("黑色",new Point(1,1),4);
		System.out.println(cirCle2.compareTo(cirCle4));
		//相离测试
		CirCle cirCle5 = new CirCle("黑色",new Point(8,8),2);
		System.out.println(cirCle2.compareTo(cirCle5));
		
		//点在圆内
		System.out.println(cirCle3.pointIsIn(point1));
		//点在圆外
		System.out.println(cirCle5.pointIsIn(point1));
	}
}

运行结果展示

在这里插入图片描述


本节完!

更多信息交流请加QQ:1406073270
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值