




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、實驗課程名稱:Java語言程序設(shè)計A實驗項目名稱實驗3:接口實驗成績實 驗 者專業(yè)班級組 別同 組 者無開始日期第一部分:實驗預(yù)習(xí)報告(包括實驗?zāi)康募耙饬x,實驗基本原理與方法,主要儀器設(shè)備及耗材,實驗內(nèi)容及要求,實驗方案與技術(shù)路線等)一實驗?zāi)康募耙饬x1自定義接口。2自定義類實現(xiàn)接口。3接口及實現(xiàn)類的多態(tài)處理。二實驗基本原理與方法1接口的概念。2接口對多態(tài)的支持。三主要儀器設(shè)備及耗材1PC及其兼容機(jī)。2計算機(jī)操作系統(tǒng)。3程序編輯器EditPlus/Eclipse。4Java開發(fā)工具JDK。四實驗內(nèi)容及要求自定義形狀接口Shape,該接口聲明了計算面積、周長的方法。然后,分別編寫三角形類Trian
2、gle、六邊形類Hexagon、橢圓形類Ellipse,它們都實現(xiàn)了Shape接口。最后,編寫測試類ShapesDemo,多態(tài)地創(chuàng)建各種形狀對象,計算面積、周長。五實驗方案及技術(shù)路線(含各種形狀面積、周長的計算公式,UML類圖,注意事項) 因為每種形狀的面積、周長計算所需要的參數(shù)個數(shù)不同,并且不同類型的三角形計算周長的面積的方法也不同,所以抽象類的參數(shù)就定為可變長度集合ArrayList,一般三角形的面積S=a*h/2,周長L=a+b+c;直角三角形面積S=a*b,周長L=a+b+,等邊三角形的面積S=,周長L=3*a;六邊形的面積S=,周長L=6*a。以下是簡略的UML類圖:1)接口Shap
3、e2) 三角形類Triangle3) 六邊形類4) 橢圓形類第二部分:實驗過程記錄(可加頁)(代碼、運行結(jié)果、實驗中出現(xiàn)的問題及解決過程)n Shape接口:import java.util.List;public interface Shapepublic double culArea(List<Double> list);public double culGirth(List<Double> list);n 六邊形類Hexagon:import java.util.*;public class Hexagon implements Shape private dou
4、ble a;List<Double> listData=new ArrayList<Double>();public Hexagon(double a) this.a = a;listData.add(a); Overridepublic double culArea(List<Double> list) double s=0;s=Math.sqrt(3)*3*Math.pow(list.get(0), 2)/2;return s;Overridepublic double culGirth(List<Double> list) double l
5、=0;l=list.get(0)*6;return l;public List<Double> getListData() return listData;n 三角形類Triangle:import java.util.*;public class Triangle implements Shape private double a;private double b;private double c;private double h;List<Double> listData=new ArrayList<Double>();public Triangle(d
6、ouble a)this.a = a;listData.add(1.0);listData.add(a);public Triangle(double a, double b) this.a = a;this.b = b;listData.add(2.0);listData.add(a);listData.add(b);public Triangle(double a, double b, double c, double h) super();this.a = a;this.b = b;this.c = c;this.h = h;listData.add(3.0);listData.add(
7、a);listData.add(b);listData.add(c);listData.add(h);public List<Double> getListData()return listData;public void setListData(List<Double> listData) this.listData = listData;Overridepublic double culArea(List<Double> list)double s=0;if(list.get(0)=1.0)s=Math.sqrt(3)*Math.pow(list.get
8、(1), 2)/4;if(list.get(0)=2.0)s=list.get(1)*list.get(2)/2;if(list.get(0)=3.0)s=list.get(1)*list.get(4)/2;return s;Overridepublic double culGirth(List<Double> list) double l=0;if(list.get(0)=1.0)l=3*list.get(1);if(list.get(0)=2.0) l=list.get(1)+list.get(2)+Math.sqrt(Math.pow(list.get(1), 2)+Math
9、.pow(list.get(2), 2);if(list.get(0)=3.0) l=list.get(1)+list.get(2)+list.get(3); return l; n 測試類ShapesDemo:public class ShapesDemo public static void main(String args)menuStrip(); public static void menuStrip() Scanner sc = new Scanner(System.in);String choice = null;do System.out.println("選擇需要計
10、算面積和周長的圖形形狀。");System.out.println("1.三角形");System.out.println("2.正六邊形");System.out.println("3.橢圓形");System.out.println("4.退出");System.out.println("請輸入選項【1-4】");choice = sc.next();switch (choice) case "1":option1();break;case "2&qu
11、ot;:option2();break;case "3":option3();break;case "4":System.exit(0);default:System.err.println("輸入錯誤!"); menuStrip(); while (!(choice.equals("4");private static void option1() Scanner sc1=new Scanner(System.in);String tempChoice=null;System.out.println("
12、請選擇需要三角形的類型。");System.out.println("1.等邊三角形");System.out.println("2.直角形");System.out.println("3.普通");System.out.println("請輸入選項【1-3】(返回上一級請輸入'0')");tempChoice=sc1.next();if(tempChoice.equals("1") try for(;)System.out.print("請輸入等邊三角形的邊
13、長:");double aIn=sc1.nextDouble();if(aIn>0)Triangle triangle1=new Triangle(aIn);double area=triangle1.culArea(triangle1.getListData();double girth=triangle1.culGirth(triangle1.getListData();System.out.println("此三角形的面積為:"+area+"n此三角形的周長為:"+girth);break;elseSystem.err.printl
14、n("輸入錯誤,請輸入大于0的數(shù)值!"); catch (Exception e) System.err.println("輸入錯誤,請重新輸入!");option1(); else if(tempChoice.equals("2")try for(;)System.out.print("請輸入一條直角邊長:");double aIn=sc1.nextDouble();System.out.print("請輸入另一條直角邊長:");double bIn=sc1.nextDouble();if(a
15、In>0&&bIn>0)Triangle triangle1=new Triangle(aIn,bIn);double area=triangle1.culArea(triangle1.getListData();double girth=triangle1.culGirth(triangle1.getListData();System.out.println("此三角形的面積為:"+area+"n此三角形的周長為:"+girth);break;elseSystem.err.println("輸入錯誤,請輸入大于0的
16、數(shù)值!"); catch (Exception e) System.err.println("輸入錯誤,請重新輸入!");option1(); else if(tempChoice.equals("3")try for(;)System.out.print("請輸入三角形底邊長:");double aIn=sc1.nextDouble();System.out.print("請輸入高:");double hIn=sc1.nextDouble();System.out.print("請輸入三角形一
17、條側(cè)邊邊長:");double bIn=sc1.nextDouble();System.out.print("請輸入三角形另一條側(cè)邊邊長:");double cIn=sc1.nextDouble();if(aIn>0&&bIn>0&&cIn>0&&hIn>0)if(aIn+bIn)>cIn&&(aIn+cIn)>bIn&&(bIn+cIn)>aIn)Triangle triangle1=new Triangle(aIn,bIn,cIn,hIn)
18、;double area=triangle1.culArea(triangle1.getListData();double girth=triangle1.culGirth(triangle1.getListData();System.out.println("此三角形的面積為:"+area+"n此三角形的周長為:"+girth);break;elseSystem.err.println("輸入錯誤!不能構(gòu)成三角形!請重新輸入數(shù).");elseSystem.err.println("輸入錯誤,請輸入大于0的數(shù)值!"
19、); catch (Exception e) System.err.println("輸入錯誤,請重新輸入!");option1(); else if(tempChoice.equals("0")menuStrip();elseSystem.err.println("輸入錯誤!");String c=reChoice();if(c.equals("1")option1();else/返回主菜單private static void option2()Scanner sc2=new Scanner(System.in
20、);String c=reChoice();if(c.equals("1")try for(;)System.out.print("請輸入正六邊形的邊長:");double aIn=sc2.nextDouble();if(aIn>0)Hexagon hexagon=new Hexagon(aIn);double area=hexagon.culArea(hexagon.getListData();double girth=hexagon.culGirth(hexagon.getListData();System.out.println("
21、此正六邊形的面積為:"+area+"n此正六邊形的周長為:"+girth);break;elseSystem.err.println("輸入錯誤,請輸入大于0的數(shù)值!"); catch (Exception e) System.err.println("輸入錯誤,請重新輸入!");option2();else/返回主菜單menuStrip();private static void option3()Scanner sc3=new Scanner(System.in);String c=reChoice();if(c.equ
22、als("1")try for(;)System.out.print("請輸入橢圓長半軸長:");double aIn=sc3.nextDouble();System.out.print("請輸入橢圓短半軸長:");double bIn=sc3.nextDouble();if(aIn>0&&bIn>0)if(aIn>bIn)Ellipse ellipse=new Ellipse(aIn,bIn);double area=ellipse.culArea(ellipse.getListData();dou
23、ble girth=ellipse.culGirth(ellipse.getListData();System.out.println("此橢圓形的面積為:"+area+"n此橢圓的周長為:"+girth);break;elseSystem.err.println("輸入錯誤,長半軸長度小于短半軸,請重新您輸入!");elseSystem.err.println("輸入錯誤,請輸入大于0的數(shù)值!"); catch (Exception e) System.err.println("輸入錯誤,請重新輸入!");optio
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- DB14-T 3363-2025 羊肚菌設(shè)施栽培技術(shù)規(guī)程
- 偏頭痛個案護(hù)理
- 車輛安全技術(shù)研發(fā)與應(yīng)用共享合作協(xié)議
- 標(biāo)準(zhǔn)員考試答案
- 2025年茶藝師職業(yè)技能鑒定考試題及答案
- 互聯(lián)網(wǎng)廠房轉(zhuǎn)租及創(chuàng)新創(chuàng)業(yè)項目合作協(xié)議
- 成品油運輸質(zhì)量監(jiān)控與責(zé)任承擔(dān)合同
- 車輛運輸與車輛租賃及保險服務(wù)協(xié)議
- 工程資料報驗程序
- 64872-鮑新中-物流成本管理(微課版第2版)-習(xí)題參考答案
- 精裝分包勞務(wù)合同協(xié)議書
- 2025年四年級下冊美術(shù)期末測試題附答案
- 店面借給別人合同協(xié)議書
- 圖像編輯基礎(chǔ)Photoshop試題及答案
- 宣城汽車精密零部件項目商業(yè)計劃書
- 2021入河(海)排污口三級排查技術(shù)指南
- 行為:2024年全球影視報告-YouGov
- 2025年中考第一次模擬考試卷:地理(陜西卷)(解析版)
- 2025年中考語文押題作文9篇
- 2025-2030菊粉粉行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- 2024年深圳市中考?xì)v史試卷真題(含答案解析)
評論
0/150
提交評論