码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-07-15 14:34:20      阅读:101      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!