首页 > 编程语言 > 详细

Java基础---异常---练习

时间:2015-07-15 14:32:55      阅读:165      评论:0      收藏:0      [点我收藏+]
package com.train;
import java.lang.Math;
/********************
 * java.lang.Object
        java.lang.Math
 * static double sqrt(double a)---返回正确舍入的 double 值的正平方根。 
 * ******************/


class NoSanjiaoException extends RuntimeException{
    NoSanjiaoException(String msg) {
        super(msg);
    }
}

public class ExceptionTest {

    public static void main(String[] args) {
        
        Sanj sanj = new Sanj(10,20,30);
        
        System.out.println("This Sanj‘ area is  "+sanj.getArea());
        sanj.showInfo();
    }
}

class Sanj{
    private double x = 0;
    private double y = 0;
    private double z = 0;
    private double cirLength = 0;
    
    Sanj(double argX ,double argY, double argZ) throws NoSanjiaoException{
        this.x = argX;
        this.y = argY;
        this.z = argZ;
        this.cirLength = this.x + this.y +this.z;
        if(x+y<=z||x+z<=y||y+z<=x){
            throw new NoSanjiaoException("此三边无法构成三角形!");
        }
    }
    
    public void showInfo(){
        System.out.println("Sanj‘ x = "+this.x+"; y = "+this.y+"; z = "+this.z);
    }
    
    public double getArea(){
        return Math.sqrt(cirLength*(cirLength-x)*(cirLength-y)*(cirLength-z));
    }
}

:console:

技术分享

Java基础---异常---练习

原文:http://www.cnblogs.com/plant/p/4647993.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!