PackageCom.train;ImportJava.lang.Math;/******************** * Java.lang.Object java.lang.Math * static double sqrt (double A)---returns the positive square root of the correctly rounded double value. * ******************/classNosanjiaoexceptionextendsruntimeexception{nosanjiaoexception (String msg) {Super(msg); }} Public classExceptiontest { Public Static voidMain (string[] args) {Sanj Sanj=NewSanj (10,20,30); System.out.println ("This Sanj ' area is" +Sanj.getarea ()); Sanj.showinfo (); }}classsanj{Private Doublex = 0; Private Doubley = 0; Private Doublez = 0; Private DoubleCirlength = 0; Sanj (DoubleARGX,DoubleArgy,DoubleARGZ)throwsnosanjiaoexception{ 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 NewNosanjiaoexception ("The three sides cannot form a triangle!") "); } } Public voidShowinfo () {System.out.println ("Sanj ' x =" + This. x+ "; y = "+ This. y+ "; z = "+ This. z); } Public DoubleGetarea () {returnMath.sqrt (cirlength* (cirlength-x) * (cirlength-y) * (cirlength-z)); }}
: Console:
Java Foundation---Exceptions---exercises