java请求注释_求达人给java代码【注释】!!请求尽量详细,万分感谢!!
(1)定義一個接口Inf,含有常量π和一個實現計算功能的方法calculate(),再分別定義一個面積類area和一個周長類circumference,各自按計算圓面積和圓周長具體實現接口中的方法,并以...
(1) 定義一個接口Inf,含有常量π和一個實現計算功能的方法calculate( ),再分別定義一個面積類area和一個周長類circumference,各自按計算圓面積和圓周長具體實現接口中的方法,并以半徑為5來測試這兩個類。
interface Inf
{
double PI=3.1415926;
double calculate();
}
class area implements Inf
{
double r;
public area(double r1){r=r1;}
public double calculate()
{
return PI*r*r;
}
public void output()
{
System.out.println("圓面積為:" + this.calculate());
}
}
class circumference implements Inf
{
double r;
public circumference(double r1){r=r1;}
public double calculate()
{
return 2*PI*r;
}
public void output()
{
System.out.println("圓周長為:" + this.calculate());
}
}
public class te1
{
public static void main(String args[])
{
area a = new area(5);
a.output();
circumference c = new circumference(5);
c.output();
}
}
(2) 定義一個類,在main方法的try塊中產生并拋出一個異常,在catch塊中捕獲異常,并輸出相應信息,同時加入finally子句,輸出信息,證明它的無條件執行。
public class te2{
public static void main(String args[])
{
try {throw new MyException();}
catch(Exception e)
{
System.out.println("It's caught");
}
finally
{
System.out.println("It's finally caught");
}
}
}
class MyException extends Exception{}
(3) 定義一個類Caculate實現10以內的整數加減法的計算。自定義一個異常類NumberRangeException,當試圖進行超范圍運算時,產生相應的信息。編寫應用程序進行測試。
import java.io.*;
class NumberRangeException extends Exception
{
public NumberRangeException()
{
}
}
public class te3
{
public static void main(String args[])
{
int a = 0;
int b = 0;
int add=0;
int sub=0;
while(true)
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
try
{
a = Integer.parseInt(input.readLine());
b = Integer.parseInt(input.readLine());
}
catch (NumberFormatException e)
{
}
catch (IOException e)
{
}
try
{
if (a > 10||b > 10)
throw new NumberRangeException();
else
add=a+b;
sub=a-b;
break;
}
catch (NumberRangeException e)
{
System.out.println("您所輸入的數字大于10!");
break;
}
}
System.out.println("兩數相加得:"+add);
System.out.println("兩數相減得:"+sub);
}
}
展開
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的java请求注释_求达人给java代码【注释】!!请求尽量详细,万分感谢!!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript设计思维
- 下一篇: 安卓JAVA调用lua_android中