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

该博客介绍了如何使用Java编程实现一个Triangle类,该类包含三个Point对象表示三角形的顶点,并提供了判断是否能构成三角形、计算周长和面积的方法。在TestTriangle类的main方法中,用户输入三个顶点坐标,程序会计算并输出三角形的周长和面积,或者在无法构成三角形时给出提示。

【问题描述】先定义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);
		}
	}
}

 

Java中,你可以先创建一个名为`Point`的基本,代表二维空间中的一个点,通常包含两个属性(如x坐标和y坐标)。然后,为了表示三角形,可以定义一个名为`Triangle`的,这个会作为`Point`的实集合来存储三个顶点。下面是一个简单的子: ```java // Point,表示二维空间的一个点 public class Point { private double x; private double y; // 构造函数 public Point(double x, double y) { this.x = x; this.y = y; } // 获取和设置坐标的方法 public double getX() { return x; } public void setX(double newX) { this.x = newX; } public double getY() { return y; } public void setY(double newY) { this.y = newY; } // 简短的描述点信息 @Override public String toString() { return "(" + x + ", " + y + ")"; } } // Triangle,包含三个Point型的成员变量并表示一个三角形 public class Triangle { private Point pointA; private Point pointB; private Point pointC; // 构造函数,接受三个点作为参数 public Triangle(Point a, Point b, Point c) { pointA = a; pointB = b; pointC = c; } // 获取三角形的顶点 public Point getVertexA() { return pointA; } public Point getVertexB() { return pointB; } public Point getVertexC() { return pointC; } // 为了简单起见,这里只展示了基本的构造和访问方法,实际应用中可能会有更多功能 @Override public String toString() { return "Triangle with vertices: " + pointA + ", " + pointB + ", " + pointC; } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值