




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗(yàn)項(xiàng)目名稱繼承和多態(tài)院系:學(xué)號(hào):姓名: 指導(dǎo)老師:所屬課程:Java語言程序設(shè)計(jì)專業(yè)班級(jí):實(shí)驗(yàn)地點(diǎn):本實(shí)驗(yàn)項(xiàng)目成績:教師簽字:日期:1實(shí)驗(yàn)?zāi)康?1)掌握類的繼承機(jī)制。(2)熟悉類中成員變量和方法的訪問控制。(3 )熟悉方法或構(gòu)造方法多態(tài)性。2.實(shí)驗(yàn)內(nèi)容(1)模擬編寫程序,理解類的繼承、多態(tài)、繼承和多態(tài)規(guī)則。(2 )獨(dú)立編程,實(shí)現(xiàn)類的繼承和多態(tài)。3.實(shí)驗(yàn)作業(yè)設(shè)計(jì)一個(gè)類Shape (圖形)包含求面積和周長的area()方法和perimeter()方法以及設(shè)置顏色的方法 SetColor(),并利用Java多態(tài)技術(shù)設(shè)計(jì)其子類Circle (圓形)類、Rectangle(矩形)類和 Tria ngl
2、e (三角形)類,并分別實(shí)現(xiàn)相應(yīng)的求面積和求周長的方法。每個(gè)類都要覆蓋toString 方法。海倫公式:三角形的面積等于s(s-a)(s-b)(s-c) 的開方,其中s=(a+b+c)/2程序代碼為:Class 包P ackageClass;publicp rivateclass Shape Stri ngcolor"while"publicthisShap e(Stri ng color).color = color;publicthisvoid setColor(Stri ng color).color = color;publicreturnStri ng getCo
3、lor() color ;p ublic doublegetArea()return 0;p ublicdoubleget PerimeterOreturn 0;public Stri ng toStri ng()return "color:" + colorClass;packagepublicp rivatedouble radiusclass Circleexte ndsShape p ublicCircle(Stri ng color,super (color);double radius) this.radius = radius;publicthisvoid s
4、etRadius( double radius) .radius = radius;publicreturndouble getRadius()radius ;p ublicdouble getCircleArea()return3.14* radius * radiusp ublicdoublegetCircle PerimeterOreturn 3.14*2* radius ;public Stri ng toStri ng()retur n "The Area is:"+ getCircleAreaO+ "n The P erimeter is:"
5、+ getCircle PerimeterO;p ackageClass;p ublicclass Recta ngleprivatedouble width ;privatedouble height ;publicRecta ngle(Stri ng color,double width,double height) super (color);this.width = width;this.height = height;publicvoid setWidth(doublewidth)this.width = width;publicdouble getWidth()returnwidt
6、h ;publicvoid setHeight(doubleheight)this.height = height;publicdouble getHeight()Sha peexte ndsretur nheightp ublicdoublegetRecta ngleAreaOreturn width * height ;p ublicdoublegetRecta ngle Perimeter()return 2*( width + height );public Stri ng toStri ng()+ getRecta ngleAreaOretur n "The Area is
7、:"+ "n The P erimeter is:"+ getRecta ngle Perimeter。;p ackageClass;p ublicclassTria ngleexte ndsShapeprivatedoublea;privatedoubleb;privatedoublec;privatedoubles;publicTria ngle(Stringdouble a, double b, double c, double s)super (color); this=a;thisthisthispublicthis=b;=c;=s;void setA(
8、 double.a = a;publicreturn a;double getA()a)publicvoid setB( doubleb)this.b = b;publicreturndouble getB() b;publicthisvoid setC( doublec).c = c;p ublicdouble getC()return c;p ublicdoublegetTria ngleAreaOreturn Math. sqrt ( s*( s- a)*( s- b)*( s- c);p ublicdoublegetTria ngle Penmeter()public Stri ng
9、toStri ng()retur n "The Area is:"return a + b + c;+ getTria ngleAreaO+ "nThe P erimeter is:"+ getTria ngle Perimeter。;p ublicstatic voidScanner input =System. out .print(main( Stri ng args) new Sca nn er(System."請(qǐng)輸入圓的半徑:in );“);double radius = inp ut. nextDouble();Circle cir
10、cle =new Circle( n ullout .println(circle.toString();System.,radius);System.doubleSystem.doubleout .print("n請(qǐng)輸入矩形的寬:width = inp ut .n extDouble();out .print("請(qǐng)輸入矩形的高:height = inp ut .n extDouble();“);“);Main 包packageMain;imp ortClass.Sha pe;imp ortClass.Circle;imp ortClass.Recta ngle;imp o
11、rtClass.Tria ngle;imp ortjava.util.Sca nner;publicclass test Recta ngle recta ngle =new Recta ngle( n ull , width, height);System. out .println(rectangle.toString();System. out .print( "n請(qǐng)輸入三角形的第一條邊a : ”);doublea = inp ut. nextDoubleO;System.doubleSystem.doubledoubleout .print("請(qǐng)輸入三角形的第二條邊b = inp ut. nextDoubleO;out .print("請(qǐng)輸入三角形的第三條邊c = inp ut. nextDoubleO;s = (a + b + c)/2;b : “);c : “);,a, b, c, s);Tria ngle tria ngle =new Trian gle(n ullSystem. out .println(tnangle.toStringO);運(yùn)行結(jié)果為:諳輸入矩形的寬;2請(qǐng)輸入矩形的高!
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025屆河北省永年縣一中高一物理第二學(xué)期期末監(jiān)測(cè)模擬試題含解析
- 教育技術(shù)應(yīng)用與文化傳承的關(guān)系研究
- 教育技術(shù)中的專利申請(qǐng)與風(fēng)險(xiǎn)規(guī)避
- 2025屆江西省豐城二中高二物理第二學(xué)期期末預(yù)測(cè)試題含解析
- 2025屆廣東省廣州市番禺區(qū)禺山高級(jí)中學(xué)物理高一下期末調(diào)研模擬試題含解析
- 探索教育游戲化如何影響孩子的情緒認(rèn)知能力
- 教育技術(shù)項(xiàng)目的投資規(guī)劃與風(fēng)險(xiǎn)控制
- 福建省師范大學(xué)附中2025年高一物理第二學(xué)期期末考試試題含解析
- 醫(yī)療培訓(xùn)中融入教育心理學(xué)的效果評(píng)估
- 技術(shù)如何塑造現(xiàn)代辦公模式
- 《中國特色社會(huì)主義理論體系的形成和發(fā)展》(課件)
- 職業(yè)技術(shù)學(xué)院嬰幼兒托育服務(wù)與管理專業(yè)人才培養(yǎng)方案
- 2025臺(tái)州市椒江區(qū)輔警考試試卷真題
- 中學(xué)生零食消費(fèi)情況調(diào)查與分析
- 國開本科《管理英語4》機(jī)考總題庫及答案
- 軟裝行業(yè)競品分析報(bào)告
- 公司收購公司協(xié)議書
- 基于移動(dòng)端的互聯(lián)網(wǎng)金融服務(wù)創(chuàng)新研究
- T∕CACM 024-2017 中醫(yī)臨床實(shí)踐指南 穴位埋線減肥
- GB 45189-2025氰化物安全生產(chǎn)管理規(guī)范
- 新科粵版九年級(jí)上冊(cè)初中化學(xué)全冊(cè)課前預(yù)習(xí)單
評(píng)論
0/150
提交評(píng)論