java编程题:定义Triangle类用于表示三角形,其任意两个边的和必须大于第三条边。定义 20
java编程题:定义Triangle类用于表示三角形,其任意两个边的和必须大于第三条边。定义IllegalException用于生成违反以上规则的输入。Triangle类...
java编程题:定义Triangle类用于表示三角形,其任意两个边的和必须大于第三条边。定义IllegalException用于生成违反以上规则的输入。Triangle类的构造方法如下public Triangle(double side1,double side 2,double side3)throws IllegalTriangleException{//Implement it}
展开
1个回答
展开全部
以下代码仅供参考
package baiduzhidao;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
Triangle t1;
try {
t1 = new Triangle(2, 1, 2);
} catch (IllegalTriangleException e) {
e.printStackTrace();
System.out.println("不合法的三角形");
return;
}
System.out.println("side1=" + t1.side1 + ",side2=" + t1.side2 + ",side3=" + t1.side3);
}
}
class Triangle {
double side1;
double side2;
double side3;
public Triangle(double side1, double side2, double side3) throws IllegalTriangleException {
super();
if (side1 + side2 <= side3 || side1 + side3 <= side2 || side2 + side3 <= side1) {
throw new IllegalTriangleException();
}
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
}
class IllegalTriangleException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询