-->
当前位置:首页 > 题库

PROGRAMMING:Design a tiangle exception class

Luz5年前 (2021-05-10)题库447
Create an illegaltriangleexception class to deal with the three sides of the triangle. If the sum of any two sides is less than or equal to the third side, it will show that the three sides do not meet the requirements.
Then design a triangle class with three sides. If the three edges do not meet the requirements, an illegaltriangleexception is thrown.
The construction method of triangle is as follows:
```
public Triangle(double side1, double side2, double side3) throws IllegalTriangleException {
//Implementation
}
```
A method called tostring() returns the string description of the triangle.
The tostring() method is implemented as follows:
```
return "Triangle [side1=" + side1 + ", side2=" + side2 + ", side3=" + side3 + "]";
```
Write a test program as follows, the user input three sides of the triangle, and then display the corresponding information.
When submitting, attach this test program to the following file.
Test procedure:
```
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());
}
}
}
```
###Input format:
Enter three edges
###Output format:
If the three edges are correct, the information of tostring() will be output. Otherwise, the illegal edge length will be output`
For example, if you enter 1, you will output ` triangle [side1 = 1.0, side2 = 1.0, side3 = 1.0]`
If you enter 1, 2, and 3, output ` invalid: 1.0, 2.0, and 3.0`
###Input example:
Here is a set of inputs. For example:
```in
1 2 3
```
###Output example:
The corresponding output is given here. For example:
```out
Invalid: 1.0,2.0,3.0
```







answer:If there is no answer, please comment