JAVA题目~由Point类组合成Triangle类 Exp04-1

【问题描述】先定义Point类,再定义Triangle类,在Triangle类中定义三个Point对象来表示一个三角形的三个顶点,然后定义一个方法isTri()对这三个点是否能构成三角形进行判定。构造方法先调用isTri(),如果三个点能够构成三角形,则对三角形对象进行初始化,否则给出提示并退出程序。最后定义两个方法getPerimeter()和getArea(),分别求三角形的周长和面积。
在TestTriangle类的main()方法中创建一个Triangle对象,从键盘输入该对象三个顶点的坐标,然后求该三角形的周长和面积(小数点后保留3位),或者给出不能构成三角形的提示。

【样例输入1】
1 1
2 2
3 3

【样例输出1】

Please enter the coordinates of p1:
1 1
Please enter the coordinates of p2:
2 2
Please enter the coordinates of p3:
3 3
Can't form a triangle!

【样例输入2】
1 1
2 0
0 3

【样例输出2】

Please enter the coordinates of p1:
1 1
Please enter the coordinates of p2:
2 0
Please enter the coordinates of p3:
0 3
Perimeter=7.256
Area=0.500


import java.util.Scanner;

class Point {
	private double x, y;

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

	public double getX() {
		return x;
	}

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

	public double getY() {
		return y;
	}

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

	public double getDisstance(Point p) {
		double dist = Math.sqrt((this.getX() - p.getX()) * (this.getX() - p.getX())
				+ (this.getY() - p.getY()) * (this.getY() - p.getY()));
		return dist;
	}

}

class Triangle {
	Point a;
	Point b;
	Point c;

	public Triangle(Point a, Point b, Point c) {
		super();
		this.a = a;
		this.b = b;
		this.c = c;
	}

	public boolean isTri() {
		double m, n, z;
		m = a.getDisstance(b);
		n = b.getDisstance(c);
		z = c.getDisstance(a);
		if ((m + n) > z && (m + z) > n && (n + z) > m) {
			return true;
		} else
			return false;
	}

	public Point getA() {
		return a;
	}

	public void setA(Point a) {
		this.a = a;
	}

	public Point getB() {
		return b;
	}

	public void setB(Point b) {
		this.b = b;
	}

	public Point getC() {
		return c;
	}

	public void setC(Point c) {
		this.c = c;
	}

	public double getPerimeter() {
		double m, n, z;
		m = a.getDisstance(b);
		n = b.getDisstance(c);
		z = c.getDisstance(a);
		return m + n + z;

	}

	public double getArea() {
		double m, n, z, half;
		m = a.getDisstance(b);
		n = b.getDisstance(c);
		z = c.getDisstance(a);
		half = (m + n + z) / 2;
		return Math.sqrt(half * (half - m) * (half - n) * (half - z));
	}
}

public class TestTriangle {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.printf("Please enter the coordinates of p1:\n");
		double ax = sc.nextDouble();
		double ay = sc.nextDouble();
		Point a = new Point(ax, ay);
		System.out.printf("Please enter the coordinates of p2:\n");
		double bx = sc.nextDouble();
		double by = sc.nextDouble();
		Point b = new Point(bx, by);
		System.out.printf("Please enter the coordinates of p3:\n");
		double cx = sc.nextDouble();
		double cy = sc.nextDouble();
		Point c = new Point(cx, cy);
		sc.close();
		Triangle d = new Triangle(a, b, c);
		if (d.isTri() == true) {
			double d2 = d.getArea();
			double d1 = d.getPerimeter();
			System.out.printf("Perimeter=%.3f\n", d1);
			System.out.printf("Area=%.3f\n", d2);
		} else {
			System.out.printf("Can't form a triangle!\n");
			System.exit(0);
		}
	}
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值