java定义一个三角形类,该类包括3个Point对象。定义有参的构造方法,给定3个点,先判断是否构成三角形,如果不能构成三角形,初始化一个默认的三角形。定义无参的构造方法。提供求面积和周长的方法。最后编写一个类,测试该三角形类。
时间: 2023-09-06 21:04:57 浏览: 177
### 回答1:
Triangle.java:
```java
public class Triangle {
private Point point1;
private Point point2;
private Point point3;
public Triangle(Point point1, Point point2, Point point3) {
if (!isTriangle(point1, point2, point3)) {
System.out.println("Invalid points, initializing a default triangle.");
this.point1 = new Point(0, 0);
this.point2 = new Point(1, 0);
this.point3 = new Point(0, 1);
} else {
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
}
}
public Triangle() {
this(new Point(0, 0), new Point(1, 0), new Point(0, 1));
}
public double getArea() {
double a = point1.distanceTo(point2);
double b = point2.distanceTo(point3);
double c = point3.distanceTo(point1);
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
public double getPerimeter() {
return point1.distanceTo(point2) + point2.distanceTo(point3) + point3.distanceTo(point1);
}
private boolean isTriangle(Point point1, Point point2, Point point3) {
double a = point1.distanceTo(point2);
double b = point2.distanceTo(point3);
double c = point3.distanceTo(point1);
return a + b > c && a + c > b && b + c > a;
}
}
```
Point.java:
```java
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 double getY() {
return y;
}
public double distanceTo(Point other) {
double dx = x - other.getX();
double dy = y - other.getY();
return Math.sqrt(dx * dx + dy * dy);
}
}
```
Test.java:
```java
public class Test {
public static void main(String[] args) {
Point p1 = new Point(0, 0);
Point p2 = new Point(3, 0);
Point p3 = new Point(0, 4);
Triangle t1 = new Triangle(p1, p2, p3);
System.out.println("Area: " + t1.getArea());
System.out.println("Perimeter: " + t1.getPerimeter());
Point p4 = new Point(0, 0);
Point p5 = new Point(1, 0);
Point p6 = new Point(0, 1);
Triangle t2 = new Triangle(p4, p5, p6);
System.out.println("Area: " + t2.getArea());
System.out.println("Perimeter: " + t2.getPerimeter());
}
}
```
输出:
```
Area: 6.0
Perimeter: 12.0
Area: 0.5
Perimeter: 3.414213562373095
```
### 回答2:
三角形类设计如下:
```java
public class Triangle {
private Point point1;
private Point point2;
private Point point3;
// 有参构造方法
public Triangle(Point point1, Point point2, Point point3) {
if (isTriangle(point1, point2, point3)) {
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
} else {
// 初始化一个默认的三角形
this.point1 = new Point(0, 0);
this.point2 = new Point(0, 1);
this.point3 = new Point(1, 0);
}
}
// 无参构造方法
public Triangle() {
this.point1 = new Point(0, 0);
this.point2 = new Point(0, 1);
this.point3 = new Point(1, 0);
}
// 判断是否能构成三角形
private boolean isTriangle(Point point1, Point point2, Point point3) {
// 利用向量叉积判断
double x1 = point2.getX() - point1.getX();
double y1 = point2.getY() - point1.getY();
double x2 = point3.getX() - point1.getX();
double y2 = point3.getY() - point1.getY();
double crossProduct = x1 * y2 - x2 * y1;
// 三点共线时,叉积为0,不能构成三角形
return crossProduct != 0;
}
// 求三角形面积
public double getArea() {
double x1 = point1.getX();
double y1 = point1.getY();
double x2 = point2.getX();
double y2 = point2.getY();
double x3 = point3.getX();
double y3 = point3.getY();
return Math.abs(x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))/2;
}
// 求三角形周长
public double getPerimeter() {
double side1 = point1.distanceTo(point2);
double side2 = point2.distanceTo(point3);
double side3 = point3.distanceTo(point1);
return side1 + side2 + side3;
}
}
```
测试三角形类的主类如下:
```java
public class Main {
public static void main(String[] args) {
Point point1 = new Point(1, 1);
Point point2 = new Point(2, 2);
Point point3 = new Point(3, 3);
Triangle triangle1 = new Triangle(point1, point2, point3);
System.out.println("Triangle 1 area: " + triangle1.getArea());
System.out.println("Triangle 1 perimeter: " + triangle1.getPerimeter());
Triangle triangle2 = new Triangle();
System.out.println("Triangle 2 area: " + triangle2.getArea());
System.out.println("Triangle 2 perimeter: " + triangle2.getPerimeter());
}
}
```
以上代码将根据给定的三个点初始化一个三角形对象,如果给定的三个点无法构成三角形,则初始化一个默认的三角形。通过调用获取面积和周长的方法,打印出三角形的属性值。
### 回答3:
首先,需要定义一个Point类来表示三角形的顶点。Point类可以包含两个私有属性x和y,代表该点的横坐标和纵坐标。
接下来,创建一个Triangle类来表示三角形。Triangle类可以包含三个私有属性point1、point2和point3,分别代表三角形的三个顶点。在Triangle类中定义具有3个Point对象的有参构造方法,通过给定的3个点来初始化三角形的顶点。在这个构造方法中,需要判断给定的3个点是否能够构成三角形。判断的条件是任意两个点之间的距离之和是否大于第三个点之间的距离,如果不能构成三角形,则初始化一个默认的三角形。此外,还需定义一个无参的构造方法来初始化一个默认的三角形对象。
为了计算三角形的面积和周长,Triangle类还需提供两个方法,分别是calcArea()和calcPerimeter()。对于calcArea()方法,可以使用海伦公式来计算三角形的面积。对于calcPerimeter()方法,可以分别计算三个边的长度并相加,得到三角形的周长。
最后,可以编写一个测试类来验证Triangle类的功能。在测试类中,创建多个Triangle对象,并调用其方法来获取面积和周长,并输出结果进行验证。
```java
class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
// 省略getter和setter方法
}
class Triangle {
private Point point1;
private Point point2;
private Point point3;
public Triangle(Point point1, Point point2, Point point3) {
double distance1 = calcDistance(point1, point2);
double distance2 = calcDistance(point1, point3);
double distance3 = calcDistance(point2, point3);
if (isValidTriangle(distance1, distance2, distance3)) {
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
} else {
// 初始化默认的三角形对象
this.point1 = new Point(0, 0);
this.point2 = new Point(3, 0);
this.point3 = new Point(0, 4);
}
}
public Triangle() {
this(new Point(0, 0), new Point(3, 0), new Point(0, 4));
}
private double calcDistance(Point point1, Point point2) {
int deltaX = point1.getX() - point2.getX();
int deltaY = point1.getY() - point2.getY();
return Math.sqrt(deltaX * deltaX + deltaY * deltaY);
}
private boolean isValidTriangle(double distance1, double distance2, double distance3) {
return distance1 + distance2 > distance3 &&
distance1 + distance3 > distance2 &&
distance2 + distance3 > distance1;
}
public double calcArea() {
double distance1 = calcDistance(point1, point2);
double distance2 = calcDistance(point1, point3);
double distance3 = calcDistance(point2, point3);
double s = (distance1 + distance2 + distance3) / 2;
return Math.sqrt(s * (s - distance1) * (s - distance2) * (s - distance3));
}
public double calcPerimeter() {
double distance1 = calcDistance(point1, point2);
double distance2 = calcDistance(point1, point3);
double distance3 = calcDistance(point2, point3);
return distance1 + distance2 + distance3;
}
// 省略getter和setter方法
}
public class TriangleTest {
public static void main(String[] args) {
// 创建三个顶点对象
Point point1 = new Point(0, 0);
Point point2 = new Point(3, 0);
Point point3 = new Point(0, 4);
// 创建三角形对象
Triangle triangle1 = new Triangle(point1, point2, point3);
Triangle triangle2 = new Triangle(); // 默认的三角形对象
// 输出面积和周长
System.out.println("Triangle 1 Area: " + triangle1.calcArea());
System.out.println("Triangle 1 Perimeter: " + triangle1.calcPerimeter());
System.out.println("Triangle 2 Area: " + triangle2.calcArea());
System.out.println("Triangle 2 Perimeter: " + triangle2.calcPerimeter());
}
}
```
这样,就实现了一个包含三个顶点的三角形类,并且提供了求面积和周长的方法。最后通过测试类进行验证。
阅读全文