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

下載本文檔

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

文檔簡介

1、實驗課程名稱:Java語言程序設(shè)計A實驗項目名稱實驗3:接口實驗成績實驗者專業(yè)班級組另U同組者無開始日期第一部分:實驗預(yù)習(xí)報告(包括實驗?zāi)康募耙饬x,實驗基本原理與方法,主要儀器設(shè)備及耗材,實驗容及要求,實驗方案與技術(shù)路線等)一實驗?zāi)康募耙饬x1 自定義接口。2 自定義類實現(xiàn)接口。3. 接口及實現(xiàn)類的多態(tài)處理。二.實驗基本原理與方法1. 接口的概念。2 接口對多態(tài)的支持。三主要儀器設(shè)備及耗材1. PC及其兼容機。2. 計算機操作系統(tǒng)。3. 程序編輯器 EditPlus /Eclipse4. Java開發(fā)工具 JDK。四實驗容及要求自定義形狀接口 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+a 2 b 2 ,等邊三角形的面積S=、3*a2/4,周長L=3*a ;六

3、邊形的面積 S=3 3 * a2 / 2,周長L=6*a。以下是簡略的UML類圖:1)接口 ShapeO ShapeculAreafListDouble)doub e A culGirth(List<Double?) : double2) 三角形類TriangleTrianglea double陽 b : double護 c ; double護 h : doubleA listData : List<Doijble>9 c Trianglefdouble» c Triangldouble, doublyc Triangle(double, double, doubl

4、e, double) gctUstDatn0 ; UstDoubles* setListData(ListD&uble>) : /aidC a culArea(List<Doulble doub ?厶 culGirth(List<Double*) : double3) 六邊形類4) 橢圓形類1° a 5ja b : doublea listData : List«Double>寸 Ellipsefdouble, double)厶 cylArcd(List*Double*) doubleO厶 cijlGirth(List< Double

5、: doubk getListDataO : Li5t<Doubk>第二部分:實驗過程記錄(可加頁)(代碼、運行結(jié)果、實驗中出現(xiàn)的問題及解決過程)Shape 接口:public in terface Shapepublic double culArea(List<Double> list );public double culGirth(List<Double> list );六邊形類Hexagon:public class Hexag on impleme nts Shapeprivate double a;List<Double> listD

6、ata=new ArrayList<Double>(); public Hexag on( 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=0; l=list.get(0)*6;return

7、l;public List<Double> getListData() return listData;三角形類Triangle :public class Triangleimplements Shape private double a;private double b;private double c; private double h;List<Double> listData =n ewArrayList<Double>(); public Triangle( double a)this . a = a;listData .add(1.0); li

8、stData .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( a);listData.add( b);li

9、stData.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(O)=1.O)s=Math.sqrt (3)*Math. pow( list .get(1), 2)/

10、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/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

11、 .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 I ;測試類 ShapesDemo public class ShapesDemo public static menuStrip ();public staticScanner sc =void main( Stri ngargs)void men uStrip()new Scanner(System. in );String choice = null ;do Sys

12、tem. out.pri ntln(System. out.pri ntln(System. out.pri ntln(System. out.pri ntln(System. out.pri ntln( System. out.pri ntln( choice = sc.n ext(); switch ( choice ) case case case case'選擇需要計算面積和周長的圖形形狀。");1. 三角形");2. 正六邊形");3. 橢圓形");4. 退出");請輸入選項【1-4 ");IIIIIIIIIIII&

13、quot;1"2“"3IIII"4"optio n1option2 (); break ; option3 (); break ; System. exit (0);();break;default :System. err .println("輸入錯誤!"); menu Strip (); while (!( choice .equals( "4"); private static void option 1()Scanner sc1 = new Scanner(System. in);"請選擇需要三角

14、形的類型。");"1.等邊三角形");"2.直角形");"3.普通");"請輸入選項【1-3 (返回上一級請輸入'0')");String tempChoice= null ;System.out .println(System.out .println(System.out .println(System.out .println(System.out .println(if trytempChoice=sc1. next();(tempChoice.equals( "1&qu

15、ot;)for (;)System, out .print("請輸入等邊三角形的邊長:");double aln =sc1.nextDouble();if (aln >0)Tria ngletria ngle1 =n ewTria ngle( aln);doublearea =triangle1.culArea( triangle1 .getListData();doublegirth =tria ngle1 .culGirth( tria ngle1 .getListData();System.out .println("此三角形的面積為:"+a

16、rea+"n 此 三角形的周長為:"+girth );break;elseSystem. err .println("輸入錯誤,請輸入大于 0的數(shù)值!"); catch (Exception e) System. err .println("輸入錯誤,請重新輸入!");opti on1 ();else if (tempChoice.equals( "2")try for (;)System. out .print("請輸入一條直角邊長:");double aln =sc1.nextDouble(

17、);System.out .print("請輸入另一條直角邊長:");double bln =sc1.nextDouble();if (aIn>0&&)ln>0)Triangletriangle1 =newTriangle( aln, bln);doublearea =triangle1.culArea( triangle1 .getListData();doublegirth =tria ngle1 .culGirth( tria ngle1 .getListData();System. out .println("此三角形的面積為:

18、"+area+"n 此三角形的周長為:"+girth );break;else System. err .println("輸入錯誤,請輸入大于 0的數(shù)值!"); catch (Exception e)System. err .println("輸入錯誤,請重新輸入!"); option1 ();else if (tempChoice.equals( "3")try for (;)System. out .print("請輸入三角形底邊長:");double aln =sc1.next

19、Double();System. out .pri nt("請輸入高:");double hln =sc1.nextDouble();System. out .print("請輸入三角形一條側(cè)邊邊長:"); double bln =sc1.nextDouble();System. out .print("請輸入三角形另一條側(cè)邊邊長:");double cIn =sc1.nextDouble();if (aln >0&&)In >0&&ln >0&&hIn >0)i

20、f ( aln +bIn )>cln &&(aln +cln )> bln &&(bln +cln )> aln)Tria ngletria ngle1 =newTriangle( aln , bln , cln , hln);doublearea =triangle1 .culArea( triangle1.getListData();doublegirth =tria ngle1 .culGirth( tria ngle1 .getListData();System.out .println("此三角形的面積為: "+a

21、rea+"n此三角形的周長為:"+girth );break;elseSystem. err .println("輸入錯誤!不能構(gòu)成三角 形!請重新輸入數(shù).");elseSystem. err .println("輸入錯誤,請輸入大于 0的數(shù) 值!"); catch (Exceptione)System.err .println("輸入錯誤,請重新輸入!"); optio n1 ();else if (tempChoice.equals( "0")menuStrip ();elseSystem.

22、 err .println("輸入錯誤! ”);String c=reChoice ();if (c.equals( "1")optio n1 ();else/返回主菜單private static void option2()Scanner sc2= newScanner(System. in );String c=reChoice ();if (c.equals( "1")try for (;)System.out .pri nt("請輸入正六邊形的邊長:");double aln =sc2.nextDouble();i

23、f (aln >0)Hexag on hexago n=new Hexag on( aln);double area =hexagon.culArea( hexagon.getListData(); doublegirth =hexago n.culGirth( hexag on .getListData();System.out .println("此正六邊形的面積為:"+area+"n此正六邊形的周長為:"+girth );break;elseSystem. err .println("輸入錯誤,請輸入大于 0的數(shù)值!");

24、 catch (Exceptione)System. err .println("輸入錯誤,請重新輸入!");option2 ();else/返回主菜單menu Strip ();private static void option3()Scanner sc3= new Scanner(System. in);String c=reChoice ();if (c.equals( "1")try for (;)System.out .pri nt("請輸入橢圓長半軸長:"); double aIn =sc3.nextDouble();S

25、ystem.out .pri nt("請輸入橢圓短半軸長:"); double bln =sc3.nextDouble();if (aIn >0&&ln >0)if (aln>bIn)Ellipseellipse =new Ellipse( aln , bln);doublearea=ellipse .culArea( ellipse .getListData();doublegirth =ellipse .culGirth( ellipse .getListData();System. out .println("此橢圓形的面積

26、為:"+area+"n 此橢圓的周長為:"+girth );break;elseSystem. err .println("輸入錯誤,長半軸長度小于短半 軸,請重新您輸入!");elseSystem. err .println("輸入錯誤,請輸入大于0的數(shù)值!" ); catch (Exceptione)System. err .println("輸入錯誤,請重新輸入!");option3 ();else /返回主菜單menu Strip () ; private static Stri ng reCho

27、ice()Scanner sc4 =new Scanner(System. in);String tempSelect ;for (;) System.out .println("是否要繼續(xù)計算? n" + "1繼續(xù)計算.2返回主菜單.n"+ "請輸入選擇【1-2】:"); tempSelect = sc4. next();if (tempSelect .equals( "1" )| tempSelect .equals( "2") break;else System. err .println

28、("錯誤選項!請重新選擇!");/繼續(xù)循環(huán)return tempSelect ;運行結(jié)果如下:三角形的計算占-Problems Javadoc 風(fēng) Declaration 國 Console 滋 IShapesDemo Java Application匚:Program FilesJavajdkl.O.Tbinjavaw.exe (20156月6日 下午9:12:43) 選擇需要計算面積和周長的圖形形狀-1三角形2 .正六邊形4橢圓形4 退岀請輸入選項1-4J1請選擇需要三角形的類型。等邊三角形2*直角形弓普通請輸入選項1-3J (返回上一級請輸入 V)1請輸入等邊三角形的

29、邊長 5此三角形的面和対 此三角形的周長為;15.0 占_ Problems 曲 Javadoc 甌 D&clar-ation 曰 Consol?毘SliapEsDemo Java Application匚:1-bmavaw.ej(e 201S66S 下午9:12:43) 選擇需要計算面積和周長的圖形形狀1三角形2*正六邊形3 *橢圓形4 .退岀請輸人選頂ri-41請選擇需要三角形的類型。1 等誼三角形2 直角形3 普通請輸入選頂ri-3(返回上一級請輸入8、)2請輸入一條直角邊長| 4請輸入另一條直角邊長:此三角形的面和為:10.0此三角形的周長為 Problems 曲 Javado

30、c E;. Declaration 旦匚onsole 滋Shape5Demc Java ApplicatiorJ C:Proqrarri FiIesJavaidkl. 1.0_79birri3vaw.exe (20156月6日下午9:22:17) 請選擇需要三角形的類型。1. 等邊三角形2直角形請輸入選項1-3(返回上一級請輸入10 ')3請輸入三角形底邊長 55蹄入高;34請輸入三角形一條側(cè)邊邊長::請輸入三角形另一條側(cè)邊邊長|請輸入三角形底邊長;輸入惜誤!不能構(gòu)成三角形!諳童新輸入數(shù)*7請輸入高:13請輸入三角形一條側(cè)邊邊長;15請輸入三角形另一條側(cè)邊邊長 9此三角形的面和為主45.5此三角形的周長為:31.0計算六邊形:J Problems 岳 Javadoc 也

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論