2022年Java語言程序設(shè)計A實驗3:接口_第1頁
2022年Java語言程序設(shè)計A實驗3:接口_第2頁
2022年Java語言程序設(shè)計A實驗3:接口_第3頁
2022年Java語言程序設(shè)計A實驗3:接口_第4頁
2022年Java語言程序設(shè)計A實驗3:接口_第5頁
已閱讀5頁,還剩17頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、精選精選.精選.實驗課程名稱:Java語言程序設(shè)計A實驗工程名稱實驗3:接口實驗成績實 驗 者專業(yè)班級組 別同 組 者無開始日期第一局部:實驗預習報告包括實驗目的及意義,實驗根本原理與方法,主要儀器設(shè)備及耗材,實驗內(nèi)容及要求,實驗方案與技術(shù)路線等一實驗目的及意義1自定義接口。2自定義類實現(xiàn)接口。3接口及實現(xiàn)類的多態(tài)處理。二實驗根本原理與方法1接口的概念。2接口對多態(tài)的支持。三主要儀器設(shè)備及耗材1PC及其兼容機。2計算機操作系統(tǒng)。3程序編輯器EditPlus/Eclipse。4Java開發(fā)工具JDK。四實驗內(nèi)容及要求自定義形狀接口Shape,該接口聲明了計算面積、周長的方法。然后,分別編寫三角形

2、類Triangle、六邊形類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接口S

3、hape精選精選.精選.三角形類Triangle六邊形類橢圓形類第二局部:實驗過程記錄可加頁代碼、運行結(jié)果、實驗中出現(xiàn)的問題及解決過程Shape接口:精選精選.精選.import java.util.List;public interface Shapepublic double culArea(List list);public double culGirth(List list);六邊形類Hexagon:import java.util.*;public class Hexagon implements Shape private double a;List listData=new Ar

4、rayList();public Hexagon(double a) this.a = a;listData.add(a); Overridepublic double culArea(List list) double s=0;s=Math.sqrt(3)*3*Math.pow(list.get(0), 2)/2;return s;精選精選.精選.Overridepublic double culGirth(List list) double l=0;l=list.get(0)*6;return l;public List getListData() return listData;三角形類

5、Triangle:import java.util.*;public class Triangle implements Shape private double a;private double b;private double c;private double h;List listData=new ArrayList();public Triangle(double a)this.a = a;listData.add(1.0);精選精選.精選.listData.add(a);public Triangle(double a, double b) this.a = a;this.b = b

6、;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(a);listData.add(b);listData.add(c);listData.add(h);精選精選.精選.public List getListData()return listData;public voi

7、d setListData(List listData) this.listData = listData;Overridepublic double culArea(List list)double s=0;if(list.get(0)=1.0)s=Math.sqrt(3)*Math.pow(list.get(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(

8、List 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.pow(list.get(2), 2);if(list.get(0)=3.0) l=list.get(1)+list.get(2)+list.get(3); return l; 測試類ShapesDemo:public class ShapesDemo public static void mai

9、n(String args)menuStrip(); public static void menuStrip() Scanner sc = new Scanner(System.in);String choice = null;精選精選.精選.do System.out.println(選擇需要計算面積和周長的圖形形狀。);System.out.println(1.三角形);System.out.println(2.正六邊形);System.out.println(3.橢圓形);System.out.println(4.退出);System.out.println(請輸入選項【1-4】);c

10、hoice = sc.next();switch (choice) case 1:option1();break;case 2: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.pr

11、intln(請選擇需要三角形的類型。);精選精選.精選.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(請輸入等邊三角形的邊長:);double aIn=sc1.nextDouble();if(aIn0)Triangle triangle1=new Triang

12、le(aIn);double area=triangle1.culArea(triangle1.getListData();double girth=triangle1.culGirth(triangle1.getListData();System.out.println(此三角形的面積為:+area+n此三角形的周長為:+girth);break;elseSystem.err.println(輸入錯誤,請輸入大于0的數(shù)值!); 精選精選.精選. catch (Exception e) System.err.println(輸入錯誤,請重新輸入!);option1(); else if(tem

13、pChoice.equals(2)try for(;)System.out.print(請輸入一條直角邊長:);double aIn=sc1.nextDouble();System.out.print(請輸入另一條直角邊長:);double bIn=sc1.nextDouble();if(aIn0&bIn0)Triangle triangle1=new Triangle(aIn,bIn);double area=triangle1.culArea(triangle1.getListData();double girth=triangle1.culGirth(triangle1.getListD

14、ata();System.out.println(此三角形的面積為:+area+n此三角形的周長為:+girth);break;elseSystem.err.println(輸入錯誤,請輸入大于0的數(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(請輸入高:);doubl

15、e hIn=sc1.nextDouble();System.out.print(請輸入三角形一條側(cè)邊邊長:);double bIn=sc1.nextDouble();System.out.print(請輸入三角形另一條側(cè)邊邊長:);double cIn=sc1.nextDouble();if(aIn0&bIn0&cIn0&hIn0)if(aIn+bIn)cIn&(aIn+cIn)bIn&(bIn+cIn)aIn)Triangle triangle1=new Triangle(aIn,bIn,cIn,hIn);double 精選精選.精選.area=triangle1.culArea(trian

16、gle1.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ù)值!); catch (Exception e) System.err.println(輸入錯誤,請重新輸入!);option1(); else if(tempChoice

17、.equals(0)menuStrip();else精選精選.精選.System.err.println(輸入錯誤!);String c=reChoice();if(c.equals(1)option1();else/返回主菜單private static void option2()Scanner sc2=new Scanner(System.in);String c=reChoice();if(c.equals(1)try for(;)System.out.print(請輸入正六邊形的邊長:);double aIn=sc2.nextDouble();if(aIn0)Hexagon hexa

18、gon=new Hexagon(aIn);double area=hexagon.culArea(hexagon.getListData();double girth=hexagon.culGirth(hexagon.getListData();精選精選.精選.System.out.println(此正六邊形的面積為:+area+n此正六邊形的周長為:+girth);break;elseSystem.err.println(輸入錯誤,請輸入大于0的數(shù)值!); catch (Exception e) System.err.println(輸入錯誤,請重新輸入!);option2();else/返

19、回主菜單menuStrip();private static void option3()Scanner sc3=new Scanner(System.in);String c=reChoice();if(c.equals(1)try for(;)精選精選.精選.System.out.print(請輸入橢圓長半軸長:);double aIn=sc3.nextDouble();System.out.print(請輸入橢圓短半軸長:);double bIn=sc3.nextDouble();if(aIn0&bIn0)if(aInbIn)Ellipse ellipse=new Ellipse(aIn

20、,bIn);double area=ellipse.culArea(ellipse.getListData();double 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(輸入錯誤,請重新輸入!);option3();else/返回主菜單menuStrip();精選精選.精選.private static String reChoice()Scanner sc4=new Scanne

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論