1. 程式人生 > >編寫一個Cirle類,包括構造方法,求圓的面積和周長,如果圓的半徑小於0則丟擲異常,讓使用者必須處理,半徑必須大於0才可以

編寫一個Cirle類,包括構造方法,求圓的面積和周長,如果圓的半徑小於0則丟擲異常,讓使用者必須處理,半徑必須大於0才可以

public class Circle{
private double radios;


public Circle() {
super();
// TODO Auto-generated constructor stub
}


public Circle(double radios) {
super();
this.radios = radios;
}
public double getC(){
while(this.getRadios()<0) {//當半徑小於0時 丟擲異常
try {
throw new CircleException();//處理異常類為Exception的子類
} catch (CircleException e) {
// TODO Auto-generated catch block
e.catchErro(this);
}
}
return this.getRadios()*2*3.14;
}
public double getS() {
while(this.getRadios()<0) {
try {
throw new CircleException();
} catch (CircleException e) {
// TODO Auto-generated catch block
e.catchErro2(this);
}
}
return this.getRadios()*this.getRadios()*3.14;
}


public double getRadios() {
return radios;
}


public void setRadios(double radios) {
this.radios = radios;
}

}