class NoTriangleException extends Exception{
NoTriangleException()
{
super();
}
NoTriangleException(String message)
{
super(message);
}
}
class Triangle{
int a,b,c;
Triangle(int a,int b,int c) throws NoTriangleException
{
this.a = a;
this.b = b;
this.c = c;
if(a<0||b<0||c<0)
{
throw new NoTriangleException("输入的三角形边长不能为负");
}
else if( a+b<=c||a+c<=b||b+c<=a ||a-b>=c||a-c>=b||b-c>=a)
{
throw new NoTriangleException("输入的三角形边长不合法");
}
}
//求周长
public int Get_C()
{
int C=0;
C= a+b+c;
return C;
}
//求面积
public double Get_S()
{
double s = 0;
double p =Get_C()/2;
double t = p*(p-a)*(p-b)*(p-c);
s = Math.sqrt(t);
return s;
}
}
public class Main {
public static void main(String[] args) {
try {
Triangle ta1 = new Triangle(3, 4, 90);
System.out.println("周长为:"+ta1.Get_C());
System.out.println("面积为:"+ta1.Get_S());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
try {
Triangle ta2 = new Triangle(3, -4, 5);
System.out.println("周长为:"+ta2.Get_C());
System.out.println("面积为:"+ta2.Get_S());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
try {
Triangle ta3 = new Triangle(3, 4, 5);
System.out.println("周长为:"+ta3.Get_C());
System.out.println("面积为:"+ta3.Get_S());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}