java矩形類的實現(xiàn)

java矩形類的實現(xiàn)

ID:32703935

大?。?7.50 KB

頁數(shù):6頁

時間:2019-02-14

java矩形類的實現(xiàn)_第1頁
java矩形類的實現(xiàn)_第2頁
java矩形類的實現(xiàn)_第3頁
java矩形類的實現(xiàn)_第4頁
java矩形類的實現(xiàn)_第5頁
資源描述:

《java矩形類的實現(xiàn)》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。

1、附頁:(1)源代碼及結(jié)果:importjava.io.*;classRectangle{intwidth;intheight;Rectangle(){}Rectangle(intw,inth){width=w;height=h;}publicintgetArea(){returnwidth*height;}publicintgetPerimeter(){returnwidth*2+height*2;}}classsquareextendsRectangle{intwidth;intheight;square(intw,inth){width=w;he

2、ight=h;}publicintgetPerimeter(){returnwidth*4;}publicintgetArea(){returnwidth*width;}}publicclassUseValue{publicstaticvoidmain(Stringargs[]){intm,n;try{System.out.print("請輸入長:");BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));Stringp=br.readLine();m=Integer.

3、parseInt(p);System.out.print("請輸入寬:");BufferedReaderbr1=newBufferedReader(newInputStreamReader(System.in));6/6Stringq=br1.readLine();n=Integer.parseInt(q);if(m==n){squaresquare1=newsquare(m,n);System.out.println("該圖形為正方形");System.out.print("其面積為:"+square1.getArea()+";");System.

4、out.print("其周長為:"+square1.getPerimeter()+";");}else{Rectanglerect1=newRectangle(m,n);System.out.print("該圖形為矩形");System.out.print("其面積為:"+rect1.getArea()+";");System.out.print("其周長為:"+rect1.getPerimeter()+";");}}catch(IOExceptione){}}}(2)源代碼及結(jié)果:importjava.io.*;interfacePrintable

5、{publicvoidprintItMyWay();publicvoidprintItMyWay(chara);}classRectangleimplementsPrintable{intwidth;intheight;Rectangle(){}Rectangle(intw,inth){width=w;height=h;}publicintgetArea(){returnwidth*height;}publicintgetPerimeter(){returnwidth*2+height*2;}6/6publicvoidprintItMyWay(){S

6、ystem.out.print("其長為:"+width+";");System.out.print("其寬為:"+height+";");System.out.print("其面積為:"+(width*height)+";");System.out.println("其周長為:"+(width*2+height*2)+";");}publicvoidprintItMyWay(chara){for(inti=1;i<=height;i++){for(intj=1;j<=width;j++){System.out.print(a);}System.ou

7、t.println("");}}}classsquareextendsRectangle{intwidth;intheight;square(intw,inth){width=w;height=h;}publicintgetPerimeter(){returnwidth*4;}publicintgetArea(){returnwidth*width;}publicvoidprintItMyWay(){System.out.print("其邊長為:"+width+";");System.out.print("其面積為:"+(width*width)+"

8、;");System.out.println("其周長為:"+(width*4)+";");}publicv

當(dāng)前文檔最多預(yù)覽五頁,下載文檔查看全文

此文檔下載收益歸作者所有

當(dāng)前文檔最多預(yù)覽五頁,下載文檔查看全文
溫馨提示:
1. 部分包含數(shù)學(xué)公式或PPT動畫的文件,查看預(yù)覽時可能會顯示錯亂或異常,文件下載后無此問題,請放心下載。
2. 本文檔由用戶上傳,版權(quán)歸屬用戶,天天文庫負(fù)責(zé)整理代發(fā)布。如果您對本文檔版權(quán)有爭議請及時聯(lián)系客服。
3. 下載前請仔細(xì)閱讀文檔內(nèi)容,確認(rèn)文檔內(nèi)容符合您的需求后進(jìn)行下載,若出現(xiàn)內(nèi)容與標(biāo)題不符可向本站投訴處理。
4. 下載文檔時可能由于網(wǎng)絡(luò)波動等原因無法下載或下載錯誤,付費完成后未能成功下載的用戶請聯(lián)系客服處理。