java Triangle Задача
Конструктор, имеющий в качестве параметров координаты трех вершин. Убедитесь, что эти точки относятся к вершинам треугольника. Проверьте, что созданный треугольник существует и не является вырожденным. Если это не так, используйте throw new IllegalArgumentException(), чтобы вызвать ошибку. double area() – возвращает площадь треугольника. Point centroid() – возвращает центроид треугольника. код:
class Triangle {
Point a, b, c;
double length1, length2, length3;
public Triangle(Point a, Point b, Point c) {
if (a == null || b == null || c == null) {
throw new IllegalArgumentException();
}
if (((length1 + length2) < length3) || ((length1 + length3) < length2) || ((length2 + length3) <
length1)) {
throw new IllegalArgumentException();
}
this.a = a;
this.b = b;
this.c = c;
}
public Point centroid(){
double xc= (((a.getX()+b.getX()+c.getX())/3));
double yc= (((a.getY()+b.getY()+c.getY())/3));
return new Point(xc, yc);
}
private double length3() {
double xDistanceSquare = Math.pow(c.getX() - b.getX(), 2.0);
double yDistanceSquare = Math.pow(c.getY() - b.getY(), 2.0);
return Math.sqrt(xDistanceSquare + yDistanceSquare);
}
private double length2() {
double xDistanceSquare = Math.pow(a.getX() - c.getX(), 2.0);
double yDistanceSquare = Math.pow(a.getY() - c.getY(), 2.0);
return Math.sqrt(xDistanceSquare + yDistanceSquare);
}
private double length1() {
double xDistanceSquare = Math.pow(a.getX() - b.getX(), 2.0);
double yDistanceSquare = Math.pow(a.getY() - b.getY(), 2.0);
return Math.sqrt(xDistanceSquare + yDistanceSquare);
}
public double area() {
length1 = length1();
length2 = length2();
length3 = length3();
double s = (length1 + length2 + length3) / 2;
return Math.sqrt(s * (s - length1) * (s - length2) * (s - length3));
}
}
Выдает вот такое : riangleTest.testConstructorDegenerative1:44 Expected java.lang.RuntimeException to be thrown, but nothing was thrown. [ERROR] TriangleTest.testConstructorDegenerative2:50 Expected java.lang.RuntimeException to be thrown, but nothing was thrown. [ERROR] TriangleTest.testConstructorDegenerative3:56 Expected java.lang.RuntimeException to be thrown, but nothing was thrown. [ERROR] TriangleTest.testConstructorDegenerative4:62 Expected java.lang.RuntimeException to be thrown, but nothing was thrown.
Ответы (2 шт):
Вычисление длин сторон треугольника, надо вычислить до проверки на вырожденность, в конструкторе. А ещё in if statement я добавил "=". Последний вид контруктора выглядит:
public Triangle(Point a, Point b, Point c) {
if (a == null || b == null || c == null) {
throw new IllegalArgumentException();
}
this.a = a;
this.b = b;
this.c = c;
length1 = length1();
length2 = length2();
length3 = length3();
if (length1 + length2 <= length3 || length1 + length3 <= length2 || length2 + length3 <= length1) {
throw new IllegalArgumentException();
}
}
И вы можете найти краткое объяснение про вырожденный треугольник, здесь.
Перепишите площадь через косое векторное произведение, длины сторон вообще не нужны
public double area() {
return Math.abs(0.5*((c.getX() - a.getX()) * (b.getY() - a.getY()) -
(c.getY() - a.getY()) * (b.getX() - a.getX())));
}
Для проверки, что треугольник не вырожден, достаточно проверить, что area не равно нулю.