实验7
7-2 设计一个Tiangle异常类 (20分)
创建一个IllegalTriangleException类,处理三角形的三边,任意两边之和小于等于第三边,则显示三条边不符合要求。
然后设计一个有三条边的Triangle的类。如果三条边不符合要求,则抛出一个IllegalTriangleException异常。
三角形的构造方法如下:
public Triangle(double side1, double side2, double side3) throws IllegalTriangleException {
//实现
}
一个名为toString()的方法返回这个三角形的字符串描述。
toString()方法的实现如下所示:
return "Triangle [side1=" + side1 + ", side2=" + side2 + ", side3=" + side3 + "]";
编写一个测试程序如下,用户输入三角形的三条边,然后显示相应信息。 提交时,将此测试程序附在后面一起提交。 测试程序:
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double s1 = input.nextDouble();
double s2 = input.nextDouble();
double s3 = input.nextDouble();
try {
Triangle t = new Triangle(s1,s2,s3);
System.out.println(t);
}
catch (IllegalTriangleException ex) {
System.out.println(ex.getMessage());
}
}
}
输入格式:
输入三条边
输出格式:
如果三条边正确,则输出toString()的信息,否则,输出IllegalTriangleException: 非法的边长
例如,输入1 1 1,则输出Triangle [side1=1.0, side2=1.0, side3=1.0]
若输入 1 2 3,则输出Invalid: 1.0,2.0,3.0
输入样例:
在这里给出一组输入。例如:
1 2 3
输出样例:
在这里给出相应的输出。例如:
Invalid: 1.0,2.0,3.0
答案:
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double s1 = input.nextDouble();
double s2 = input.nextDouble();
double s3 = input.nextDouble();
try {
Triangle t = new Triangle(s1,s2,s3);
System.out.println(t);
}catch (IllegalTriangleException ex) {
System.out.println(ex.getMessage());
}
}
}
class IllegalTriangleException extends Exception{
private double side1;
private double side2;
private double side3;
public IllegalTriangleException(double side1,double side2 ,double side3) {
super("Invalid: "+side1+","+side2+","+side3);//message是父类成员变量,使用super向上传参!
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
}
class Triangle{
private double side1;
private double side2;
private double side3;
public Triangle(double side1,double side2,double side3)
throws IllegalTriangleException{
if((side1+side2)>side3&(side1+side3)>side2&(side2+side3)>side1)
{
this.side1=side1;this.side2=side2;this.side3=side3;}
else
throw new IllegalTriangleException(side1,side2,side3);
}
@Override
public String toString(){
return "Triangle ["+"side1="+this.side1+", side2="+this.side2+", side3="+this.side3+"]";
}
}
答案2:
import java.util.*;
public class PTA333 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double s1 = input.nextDouble();
double s2 = input.nextDouble();
double s3 = input.nextDouble();
try {
Triangle t = new Triangle(s1,s2,s3);
System.out.println(t);
}
catch (IllegalTriangleException ex) {
System.out.println(ex.getMessage());
}
}
}
class IllegalTriangleException extends Exception{
String message;
double side1;
double side2;
double side3;
public IllegalTriangleException(double side1,double side2,double side3) {
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
public String getMessage() {
return "Invalid: "+this.side1+","+this.side2+","+this.side3+"";
}
}
class Triangle{
double side1;
double side2;
double side3;
public Triangle(double side1, double side2, double side3) throws IllegalTriangleException {
if(side1+side2<=side3||side1+side3<=side2||side2+side3<=side1) {
throw new IllegalTriangleException(side1,side2,side3);
}else {
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
}
public String toString() {
return "Triangle [side1=" + side1 + ", side2=" + side2 + ", side3=" + side3 + "]";
}
}