【JAVA 课后习题 12.5】+ 自定义异常类

这题好水~~部分类名被我简写了~~还是比交喜欢敲c++~ JAVA 太没劲了~~

Tr类代码 (即Triangle类):

package Tr;

import org.omg.IOP.IOR;

public class Tr {
    double s1 = 1.0;
    double s2 = 1.0;
    double s3 = 1.0;
    public Tr(){    
    }
    public Tr(double s1,double s2,double s3)
        throws IllegalAccessException{ // 抛出异常
           if((s1 + s2 <= s3) || (s2 + s3 <= s1) || (s2 + s3) <= s1)
                throw new IllegalAccessException("Wrong");
            else{
        this.s1 = s1;
        this.s2 = s2;
        this.s3 = s3;
            }
     }
    public double gets1(){
        return this.s1;
    }
    public double gets2(){
        return this.s2;
    }
    public double gets3(){
        return this.s3;
    }
    public double getArea(){ // 三角形面积计算公式
        double a = (this.s1 * this.s1 + this.s2 * this.s2 - this.s3 * this.s3) /  2 * this.s1 * this.s2;
        double b = Math.sqrt(1.0 - a * a);
        return 0.5 * this.s1 * this.s2 * b;
    }
    public double getPe(){
        return this.s1 + this.s2 + this.s3;
    }
    public String toString(){
        return "Tr : s1 = " + s1 + " s2 = " + s2 + " s3 = " + s3;
    }
 }

Text 类代码(也即测试代码):

package Tr;

public class Test {

    public static void main(String[] args) {
        try{
            Tr a1 = new Tr(1.0,2.0,3.0);
            Tr a2 = new Tr(1.0,1.0,1.0);
            a2.gets1();
        }
        catch(IllegalAccessException ex){ // 捕获异常
            System.out.println("The three number cannot be Triangle");
        }
}
}

你可能感兴趣的:(java练习,JAVA,课后习题及知识点总结)