题目1、定义一个接口,包括两个方法:求三角形面积和求圆面积;2、自定义异常类:当三角形的两边长度之和小于第三边,抛出异常;3、定义一个类,实现上述接口,及异常处理;4、在PUBLIC CLASS中通过键盘输入相应的数值, 当发生异常时,程序会提示异常; 当没有异常发生时,程序会正确输出所求结果;
package hexianlan;
import java.util.Scanner;
interface A{
public void s(int a,int b,int c);
public void y(int x);
}//接口
class MyException extends Exception{
String message;
public MyException(){
message="两边之和小于第三边";
}
public String getMessage() {
return message;
}
}//自定义异常类
class B implements A{
public void s(int a,int b,int c) throws MyException{
if(a+b<c||a+c<b||b+c<a) {
MyException ex=new MyException();
throw(ex);
}
double s=(a+b+c)/2;
System.out.printf("三角形的面积为:",s);
}
public void y(int x) {
double sy=xx3.14;
System.out.printf("圆的面积为:",sy);
}
}
public class hxl {
public static void main(String arga[]) {
B b=new B();
Scanner sc=new Scanner(System.in);
int x1,x2,x3,x4;
try {
System.out.println("输入三角形的三边:");
x1=sc.nextInt();
x2=sc.nextInt();
x3=sc.nextInt();
b.s(x1,x2,x3);
System.out.println("输入三角形的三边:");
x1=sc.nextInt();
x2=sc.nextInt();
x3=sc.nextInt();
b.s(x1,x2,x3);
System.out.println("输入圆的半径:");
x4=sc.nextInt();
b.y(x4);
}
catch (MyException e) {
System.out.println(e.getMessage());
}
}
}