Java 获取无法构造的对象数
对于我的一个课堂作业,我需要创建一个创建三角形对象的类。根据几何规则,对于一个有效三角形,任何给定边的长度应小于其他两条边的长度之和。我所编写的对象构造函数类可以采用任意双精度,这意味着可以输入三角形的一侧,其长度比其他两个的总和还要长。这违反了三角形的几何规则(这是可以的)。但是,我想记录有多少三角形违反了几何规则,因此无法构造 通常,您会使用try,catch块来捕获异常。然而,我在这里遇到的问题是,一旦到达异常,就会跳过异常后面的对象,而不会创建该对象。这会导致两个问题:第一,非法对象之后的任何合法对象都不会被创建。第二,除了第一个非法对象之外,我无法跟踪有多少对象未能创建(因为其余对象被跳过)。我如何重新构造代码,以便创建合法对象,即使遇到非法对象,也能记录有多少合法/非法对象? 我使用了一个自定义异常处理程序对象,它是我为triangle对象编写的,但为了减少代码,这里没有包括它。 我是一个初学者,所以对你来说很明显的解决方案对我来说是模糊的。谢谢你的帮助 这是我的三角形测试仪代码:Java 获取无法构造的对象数,java,object,exception,constructor,Java,Object,Exception,Constructor,对于我的一个课堂作业,我需要创建一个创建三角形对象的类。根据几何规则,对于一个有效三角形,任何给定边的长度应小于其他两条边的长度之和。我所编写的对象构造函数类可以采用任意双精度,这意味着可以输入三角形的一侧,其长度比其他两个的总和还要长。这违反了三角形的几何规则(这是可以的)。但是,我想记录有多少三角形违反了几何规则,因此无法构造 通常,您会使用try,catch块来捕获异常。然而,我在这里遇到的问题是,一旦到达异常,就会跳过异常后面的对象,而不会创建该对象。这会导致两个问题:第一,非法对象之后
public class TestTriangle {
public static void main(String[] args) throws IllegalTriangleException {
// Start of try block for creating 4 triangle objects
try {
Triangle triangle1 = new Triangle(); // create first triangle using default constructor
System.out.println(triangle1.toString()); // output the information about length of sides
// output info about perimeter and area of triangle
System.out.println("The perimeter of the triangle1 is " + triangle1.getPerimeter());
System.out.println("The area of the triangle1 is " + triangle1.getArea());
System.out.println();
Triangle triangle2 = new Triangle(3, 4, 5); // create a second triangle given sides
System.out.println(triangle2.toString());
System.out.println("The perimeter of the triangle2 is " + triangle2.getPerimeter());
System.out.println("The area of the triangle2 is " + triangle2.getArea());
System.out.println();
Triangle triangle3 = new Triangle(4, 2, 5); // create a third triangle given sides
System.out.println(triangle3.toString());
System.out.println("The perimeter of the triangle3 is " + triangle3.getPerimeter());
System.out.println("The area of the triangle3 is " + triangle3.getArea());
System.out.println();
Triangle triangle4 = new Triangle(2, 3, 7); // try to create an illegal triangle
System.out.println(triangle4.toString());
System.out.println("The perimeter of the triangle4 is " + triangle4.getPerimeter());
System.out.println("The area of the triangle4 is " + triangle4.getArea());
System.out.println();
Triangle triangle5 = new Triangle(2, 4, 9); // try to create an illegal triangle
System.out.println(triangle5.toString());
System.out.println("The perimeter of the triangle5 is " + triangle5.getPerimeter());
System.out.println("The area of the triangle5 is " + triangle5.getArea());
System.out.println();
// catch exceptions, if any
} catch (IllegalTriangleException ex) {
System.out.println("Exception: " + ex);
}
System.out.println();
System.out.println(Triangle.getNumberOfObjects() + " triangles constructed");
System.out.println(Triangle.illegalAttempts() + " triangle(s) failed to construct");
}
}
如果您需要查看,这里是类构造函数:
public class Triangle {
// declare variables for each side of the triangle
private double side1;
private double side2;
private double side3;
// declare variables to keep track of number of objects created or illegal attempts
private static int numberOfObjects = 0;
private static int illegalAttempts = 0;
// default constructor for triangle
public Triangle() {
side1 = 1.0;
side2 = 1.0;
side3 = 1.0;
numberOfObjects++;
}
// constructor for creating triangles based on length of sides
public Triangle(double newSide1, double newSide2, double newSide3) throws IllegalTriangleException {
setSides (newSide1, newSide2, newSide3);
numberOfObjects++;
}
// setter for creating triangles given the length of sides
public void setSides (double newSide1, double newSide2, double newSide3) throws IllegalTriangleException {
// According to geometric rules, in triangles, any one side should be shorter than the combined length of other two.
// Check to see if any one side is longer than the combined length of other two. If so, throw the designed exception.
if (newSide1 >= newSide2 + newSide3) {
illegalAttempts++;
throw new IllegalTriangleException(newSide1, newSide2, newSide3);
} else if (newSide2 >= newSide1 + newSide3) {
illegalAttempts++;
throw new IllegalTriangleException(newSide2, newSide1, newSide3);
} else if (newSide3 >= newSide1 + newSide2) {
illegalAttempts++;
throw new IllegalTriangleException(newSide3, newSide1, newSide2);
} else {
side1 = newSide1; // In case the geometric rule is not violated, create the sides.
side2 = newSide2;
side3 = newSide3;
}
}
// constructor for getting the number triangle created
public static int getNumberOfObjects() {
return numberOfObjects;
}
public static int illegalAttempts() {
return illegalAttempts;
}
// constructor for calculating the perimeter of triangles
public double getPerimeter() {
return side1 + side2 + side3;
}
// constructor for calculating the area of a triangle given three sides: sqrt(p*(p-a)*(p-b)*(p-c))
public double getArea() {
double perimeter = side1 + side2 + side3;
return Math.sqrt(perimeter * (perimeter - side1) * (perimeter - side2) * (perimeter - side3));
}
// create an string object giving information about the triangle
public String toString() {
return "Triangle: Side a = " + side1 + ", Side b = " + side2
+ ", Side c = " + side3;
}
}
代码的输出应该是这样的:
三角形:a侧=4.0,b侧=2.0,c侧=5.0
三角形3的周长为11.0
三角形3的面积为64.48255578061404
例外:非法三角形例外:非法三角形,任意两边之和小于另一边
第1面是7.0
第二面是2.0
第三面是3.0
构造了3个三角形
2个三角形构造失败
考虑到三角形初始化除了其值之外是相同的,一个选项可能是从try块中提取硬编码的值。如果任何一个三角形未能构造,则将捕获、处理该三角形,然后转到下一个三角形参数集
public class TestTriangle {
public static void main(String[] args) throws IllegalTriangleException {
Triangle triangle1 = new Triangle(); // create first triangle using default constructor
System.out.println(triangle1.toString()); // output the information about length of sides
// output info about perimeter and area of triangle
System.out.println("The perimeter of the triangle1 is " + triangle1.getPerimeter());
System.out.println("The area of the triangle1 is " + triangle1.getArea());
System.out.println();
List<List<Integer>> triangleEdgeLengths = new ArrayList<>();
triangleEdgeLengths.add(Arrays.asList(3, 4, 5));
triangleEdgeLengths.add(Arrays.asList(4, 2, 5));
triangleEdgeLengths.add(Arrays.asList(2, 3, 7));
triangleEdgeLengths.add(Arrays.asList(2, 4, 9));
triangleEdgeLengths.forEach((edgeLengths) -> {
try {
Triangle triangle = new Triangle(edgeLengths.get(0), edgeLengths.get(1), edgeLengths.get(2));
System.out.println(triangle.toString());
System.out.println("The perimeter of the triangle is " + triangle.getPerimeter());
System.out.println("The area of the triangle is " + triangle.getArea());
System.out.println();
// catch exceptions, if any
} catch (IllegalTriangleException ex) {
System.out.println("Exception: " + ex);
}
});
System.out.println();
System.out.println(Triangle.getNumberOfObjects() + " triangles constructed");
System.out.println(Triangle.illegalAttempts() + " triangle(s) failed to construct");
}
}
twicelost请求了一个避免使用列表的选项。一个有点冗长的选项是将每个三角形构造包装在try/catch中
public static void main(String[] args) throws IllegalTriangleException {
Triangle triangle1 = new Triangle(); // create first triangle using default constructor
System.out.println(triangle1.toString()); // output the information about length of sides
// output info about perimeter and area of triangle
System.out.println("The perimeter of the triangle1 is " + triangle1.getPerimeter());
System.out.println("The area of the triangle1 is " + triangle1.getArea());
System.out.println();
try {
Triangle triangle2 = new Triangle(3, 4, 5); // create a second triangle given sides
System.out.println(triangle2.toString());
System.out.println("The perimeter of the triangle2 is " + triangle2.getPerimeter());
System.out.println("The area of the triangle2 is " + triangle2.getArea());
System.out.println();
} catch (IllegalTriangleException ex) {
System.out.println("Exception: " + ex);
}
try {
Triangle triangle3 = new Triangle(4, 2, 5); // create a third triangle given sides
System.out.println(triangle3.toString());
System.out.println("The perimeter of the triangle3 is " + triangle3.getPerimeter());
System.out.println("The area of the triangle3 is " + triangle3.getArea());
System.out.println();
} catch (IllegalTriangleException ex) {
System.out.println("Exception: " + ex);
}
try {
Triangle triangle4 = new Triangle(2, 3, 7); // try to create an illegal triangle
System.out.println(triangle4.toString());
System.out.println("The perimeter of the triangle4 is " + triangle4.getPerimeter());
System.out.println("The area of the triangle4 is " + triangle4.getArea());
System.out.println();
} catch (IllegalTriangleException ex) {
System.out.println("Exception: " + ex);
}
try {
Triangle triangle5 = new Triangle(2, 4, 9); // try to create an illegal triangle
System.out.println(triangle5.toString());
System.out.println("The perimeter of the triangle5 is " + triangle5.getPerimeter());
System.out.println("The area of the triangle5 is " + triangle5.getArea());
System.out.println();
} catch (IllegalTriangleException ex) {
System.out.println("Exception: " + ex);
}
System.out.println();
System.out.println(Triangle.getNumberOfObjects() + " triangles constructed");
System.out.println(Triangle.illegalAttempts() + " triangle(s) failed to construct");
}
这解决了我代码中的问题,我接受答案。谢谢然而,我们仍然不了解列表和数组的列表。我想知道我是否可以用一种更简单的方法得到同样的效果。如需进一步澄清,我们将不胜感激。谢谢。在这种情况下,另一个可能符合您的条件的选项是在每个三角形构造周围复制粘贴try/catch块。我已经更新了我的答案,以包括该选项。
public static void main(String[] args) throws IllegalTriangleException {
Triangle triangle1 = new Triangle(); // create first triangle using default constructor
System.out.println(triangle1.toString()); // output the information about length of sides
// output info about perimeter and area of triangle
System.out.println("The perimeter of the triangle1 is " + triangle1.getPerimeter());
System.out.println("The area of the triangle1 is " + triangle1.getArea());
System.out.println();
try {
Triangle triangle2 = new Triangle(3, 4, 5); // create a second triangle given sides
System.out.println(triangle2.toString());
System.out.println("The perimeter of the triangle2 is " + triangle2.getPerimeter());
System.out.println("The area of the triangle2 is " + triangle2.getArea());
System.out.println();
} catch (IllegalTriangleException ex) {
System.out.println("Exception: " + ex);
}
try {
Triangle triangle3 = new Triangle(4, 2, 5); // create a third triangle given sides
System.out.println(triangle3.toString());
System.out.println("The perimeter of the triangle3 is " + triangle3.getPerimeter());
System.out.println("The area of the triangle3 is " + triangle3.getArea());
System.out.println();
} catch (IllegalTriangleException ex) {
System.out.println("Exception: " + ex);
}
try {
Triangle triangle4 = new Triangle(2, 3, 7); // try to create an illegal triangle
System.out.println(triangle4.toString());
System.out.println("The perimeter of the triangle4 is " + triangle4.getPerimeter());
System.out.println("The area of the triangle4 is " + triangle4.getArea());
System.out.println();
} catch (IllegalTriangleException ex) {
System.out.println("Exception: " + ex);
}
try {
Triangle triangle5 = new Triangle(2, 4, 9); // try to create an illegal triangle
System.out.println(triangle5.toString());
System.out.println("The perimeter of the triangle5 is " + triangle5.getPerimeter());
System.out.println("The area of the triangle5 is " + triangle5.getArea());
System.out.println();
} catch (IllegalTriangleException ex) {
System.out.println("Exception: " + ex);
}
System.out.println();
System.out.println(Triangle.getNumberOfObjects() + " triangles constructed");
System.out.println(Triangle.illegalAttempts() + " triangle(s) failed to construct");
}