java實驗報告-繼承與接口_第1頁
java實驗報告-繼承與接口_第2頁
java實驗報告-繼承與接口_第3頁
java實驗報告-繼承與接口_第4頁
java實驗報告-繼承與接口_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、成績: 面向?qū)ο笤砼cJava實踐課程實驗報告實驗2:繼承與接口姓 名 _ _ 班 級 _ _ 學 號 _ _ 實驗地點 _ _ 實驗時間 _ _ 指導教師 _ _ 一、實驗目的:l 掌握類的繼承機制,掌握成員變量的隱藏與方法的重寫l 掌握接口的定義和實現(xiàn)方法二、實驗要求:l 復習類的繼承以及接口的設(shè)計方法l 注意保證類的封裝性l 編寫程序完成以下實驗內(nèi)容并上交實驗報告三、實驗內(nèi)容:1、P153:習題42、P153:習題53、P153:習題64、在程序包packageOne中編寫至少含一個方法的公有接口InterfaceOne。在程序包packageTwo中定義公有類SuperClass,該類

2、含有一個受保護的內(nèi)部類InnerClass,這個類實現(xiàn)接口InterfaceOne。在程序包packageThree中定義公有類SubClass繼承SuperClass,類SubClass定義一個返回類型為InterfaceOne的方法返回內(nèi)部類InnerClass的對象實例。請正確使用程序包組織以上類和接口,編譯并運行,并觀察編譯器生成的字節(jié)碼文件的命名方式。四、設(shè)計思路:習題4的設(shè)計思路是設(shè)計一個抽象類作為父類,并設(shè)計相關(guān)字段以及計算成績等級的抽象方法,然后再設(shè)計本科生類和研究生類作為子類繼承,并且各自實現(xiàn)其計算成績等級的抽象方法,最終將結(jié)果輸出。習題5的設(shè)計思路跟習題4相同,只需額外設(shè)計

3、一個枚舉類存儲學生的成績等級,并對相關(guān)代碼作少許修改即可。習題6的設(shè)計思路是習題4的延伸,在習題4的類結(jié)構(gòu)基礎(chǔ)上,將原來的計算成績等級的抽象方法提取出來,放在一個公有接口中,然后再分別設(shè)計計算本科生和研究生成績等級的類實現(xiàn)接口的抽象方法。五、程序源代碼:習題4的源代碼:package Package_4;public abstract class Student String Name=""String Stu_type=""int C_score;int English_score;int Java_score;int score;String sco

4、_Level=""public Student(String name,String stu_type,int sco1,int sco2,int sco3)Name=name;Stu_type=stu_type;C_score=sco1;English_score=sco2;Java_score=sco3;score=(sco1+sco2+sco3)/3;package Package_4;public class Undergraduate extends Studentpublic Undergraduate(String name,String stu_type,i

5、nt sco1,int sco2,int sco3)super(name,stu_type,sco1,sco2,sco3);if(score>=85&score<=100)sco_Level="優(yōu)秀"else if(score>=75&score<85)sco_Level="良好"else if(score>=65&score<75)sco_Level="中等"else if(score>=60&score<65)sco_Level="及格&qu

6、ot;else if(score>=0&score<60)sco_Level="不及格"elsesco_Level="未知等級"static void Output(Undergraduate udg)System.out.println("*本科生*");/System.out.println("");System.out.print("姓名");System.out.print('t'+"學生類型");System.out.print(

7、't'+"C語言成績");System.out.print('t'+"英語成績");System.out.print('t'+"Java成績");System.out.print('t'+"平均分");System.out.println('t'+"成績等級");for(int i=0; i<udg.length;i+)System.out.print(udgi.Name);System.out.print(&

8、#39;t'+udgi.Stu_type);System.out.print('t'+String.valueOf(udgi.C_score);System.out.print('t'+String.valueOf(udgi.English_score);System.out.print('t'+String.valueOf(udgi.Java_score);System.out.print('t'+String.valueOf(udgi.score);System.out.println('t'+udgi

9、.sco_Level);System.out.println();package Package_4;public class Postgraduate extends Studentpublic Postgraduate(String name,String stu_type,int sco1,int sco2,int sco3)super(name,stu_type,sco1,sco2,sco3);if(score>=90&score<=100)sco_Level="優(yōu)秀"else if(score>=80&score<90)sc

10、o_Level="良好"else if(score>=70&score<80)sco_Level="中等"else if(score>=60&score<70)sco_Level="及格"else if(score>=0&score<60)sco_Level="不及格"elsesco_Level="未知等級"static void Output(Postgraduate pg)System.out.println("*研究生*

11、");/System.out.println("");System.out.print("姓名");System.out.print('t'+"學生類型");System.out.print('t'+"C語言成績");System.out.print('t'+"英語成績");System.out.print('t'+"Java成績");System.out.print('t'+"

12、;平均分");System.out.println('t'+"成績等級");for(int i=0; i<pg.length;i+)System.out.print(pgi.Name);System.out.print('t'+pgi.Stu_type);System.out.print('t'+String.valueOf(pgi.C_score);System.out.print('t'+String.valueOf(pgi.English_score);System.out.print(&

13、#39;t'+String.valueOf(pgi.Java_score);System.out.print('t'+String.valueOf(pgi.score);System.out.println('t'+pgi.sco_Level);System.out.println();package Package_4;public class Test public static void main(String args) Undergraduate udg=new Undergraduate5;udg0=new Undergraduate(&qu

14、ot;小明","本科生",80,95,79);udg1=new Undergraduate("小紅","本科生",60,82,76);udg2=new Undergraduate("小白","本科生",90,49,67);udg3=new Undergraduate("小黑","本科生",56,74,65);udg4=new Undergraduate("小軍","本科生",68,81,84);Postgr

15、aduate pg=new Postgraduate5;pg0=new Postgraduate("小明","研究生",80,95,79);pg1=new Postgraduate("小紅","研究生",60,82,76);pg2=new Postgraduate("小白","研究生",90,49,67);pg3=new Postgraduate("小黑","研究生",56,74,65);pg4=new Postgraduate(&qu

16、ot;小軍","研究生",68,81,84);Undergraduate.Output(udg);Postgraduate.Output(pg);習題5的源代碼:package Package_5;public enum Sco_Level A("優(yōu)秀"),B("良好"),C("中等"),D("及格"),E("不及格"),X("未知等級");private String signal;Sco_Level(String level)this.sig

17、nal=level;public String getSignal()return signal;package Package_5;public abstract class Student String Name=""String Stu_type=""int C_score;int English_score;int Java_score;int score;/String sco_Level=""Sco_Level sco_Level;public Student(String name,String stu_type,int

18、 sco1,int sco2,int sco3)Name=name;Stu_type=stu_type;C_score=sco1;English_score=sco2;Java_score=sco3;score=(sco1+sco2+sco3)/3;package Package_5;public class Undergraduate extends Studentpublic Undergraduate(String name,String stu_type,int sco1,int sco2,int sco3)super(name,stu_type,sco1,sco2,sco3);if(

19、score>=85&score<=100)sco_Level=Sco_Level.A;else if(score>=75&score<85)sco_Level=Sco_Level.B;else if(score>=65&score<75)sco_Level=Sco_Level.C;else if(score>=60&score<65)sco_Level=Sco_Level.D;else if(score>=0&score<60)sco_Level=Sco_Level.E;elsesco_Leve

20、l=Sco_Level.X;static void Output(Undergraduate udg)System.out.println("*本科生*");/System.out.println("");System.out.print("姓名");System.out.print('t'+"學生類型");System.out.print('t'+"C語言成績");System.out.print('t'+"英語成績");Sy

21、stem.out.print('t'+"Java成績");System.out.print('t'+"平均分");System.out.println('t'+"成績等級");for(int i=0; i<udg.length;i+)System.out.print(udgi.Name);System.out.print('t'+udgi.Stu_type);System.out.print('t'+String.valueOf(udgi.C_sco

22、re);System.out.print('t'+String.valueOf(udgi.English_score);System.out.print('t'+String.valueOf(udgi.Java_score);System.out.print('t'+String.valueOf(udgi.score);System.out.println('t'+udgi.sco_Level.getSignal();System.out.println();package Package_5;public class Postg

23、raduate extends Studentpublic Postgraduate(String name,String stu_type,int sco1,int sco2,int sco3)super(name,stu_type,sco1,sco2,sco3);if(score>=90&score<=100)sco_Level=Sco_Level.A;else if(score>=80&score<90)sco_Level=Sco_Level.B;else if(score>=70&score<80)sco_Level=Sco_

24、Level.C;else if(score>=60&score<70)sco_Level=Sco_Level.D;else if(score>=0&score<60)sco_Level=Sco_Level.E;elsesco_Level=Sco_Level.X;static void Output(Postgraduate pg)System.out.println("*研究生*");/System.out.println("");System.out.print("姓名");System.ou

25、t.print('t'+"學生類型");System.out.print('t'+"C語言成績");System.out.print('t'+"英語成績");System.out.print('t'+"Java成績");System.out.print('t'+"平均分");System.out.println('t'+"成績等級");for(int i=0; i<pg.le

26、ngth;i+)System.out.print(pgi.Name);System.out.print('t'+pgi.Stu_type);System.out.print('t'+String.valueOf(pgi.C_score);System.out.print('t'+String.valueOf(pgi.English_score);System.out.print('t'+String.valueOf(pgi.Java_score);System.out.print('t'+String.valueO

27、f(pgi.score);System.out.println('t'+pgi.sco_Level.getSignal();System.out.println();package Package_5;public class Test public static void main(String args) Undergraduate udg=new Undergraduate5;udg0=new Undergraduate("小明","本科生",80,95,79);udg1=new Undergraduate("小紅&quo

28、t;,"本科生",60,82,76);udg2=new Undergraduate("小白","本科生",90,49,67);udg3=new Undergraduate("小黑","本科生",56,74,65);udg4=new Undergraduate("小軍","本科生",68,81,84);Postgraduate pg=new Postgraduate5;pg0=new Postgraduate("小明","研究生

29、",80,95,79);pg1=new Postgraduate("小紅","研究生",60,82,76);pg2=new Postgraduate("小白","研究生",90,49,67);pg3=new Postgraduate("小黑","研究生",56,74,65);pg4=new Postgraduate("小軍","研究生",68,81,84);Undergraduate.Output(udg);Postgradu

30、ate.Output(pg);習題6的源代碼:package Package_6;public interface Sco_Level abstract String getLevel(int score);package Package_6;public class ugd_Level implements Sco_Levelpublic String getLevel(int score)String sco_Level=""if(score>=85&score<=100)sco_Level="優(yōu)秀"else if(score&g

31、t;=75&score<85)sco_Level="良好"else if(score>=65&score<75)sco_Level="中等"else if(score>=60&score<65)sco_Level="及格"else if(score>=0&score<60)sco_Level="不及格"elsesco_Level="未知等級"return sco_Level;package Package_6;public

32、 class pg_Level implements Sco_Levelpublic String getLevel(int score)String sco_Level=""if(score>=90&score<=100)sco_Level="優(yōu)秀"else if(score>=80&score<90)sco_Level="良好"else if(score>=70&score<80)sco_Level="中等"else if(score>=60&a

33、mp;score<70)sco_Level="及格"else if(score>=0&score<60)sco_Level="不及格"elsesco_Level="未知等級"return sco_Level;package Package_6;public abstract class Student String Name=""String Stu_type=""int C_score;int English_score;int Java_score;int scor

34、e;String sco_Level=""public Student(String name,String stu_type,int sco1,int sco2,int sco3)Name=name;Stu_type=stu_type;C_score=sco1;English_score=sco2;Java_score=sco3;score=(sco1+sco2+sco3)/3;package Package_6;public class Undergraduate extends Studentpublic Undergraduate(String name, Stri

35、ng stu_type, int sco1, int sco2,int sco3) super(name, stu_type, sco1, sco2, sco3);ugd_Level ugd_level=new ugd_Level();sco_Level=ugd_level.getLevel(score);static void Output(Undergraduate udg)System.out.println("*本科生*");/System.out.println("");System.out.print("姓名");Syst

36、em.out.print('t'+"學生類型");System.out.print('t'+"C語言成績");System.out.print('t'+"英語成績");System.out.print('t'+"Java成績");System.out.print('t'+"平均分");System.out.println('t'+"成績等級");for(int i=0; i<

37、udg.length;i+)System.out.print(udgi.Name);System.out.print('t'+udgi.Stu_type);System.out.print('t'+String.valueOf(udgi.C_score);System.out.print('t'+String.valueOf(udgi.English_score);System.out.print('t'+String.valueOf(udgi.Java_score);System.out.print('t'+St

38、ring.valueOf(udgi.score);System.out.println('t'+udgi.sco_Level);System.out.println();package Package_6;public class Postgraduate extends Studentpublic Postgraduate(String name, String stu_type, int sco1, int sco2,int sco3) super(name, stu_type, sco1, sco2, sco3);/ TODO Auto-generated constru

39、ctor stubpg_Level pg_level=new pg_Level();sco_Level=pg_level.getLevel(score);static void Output(Postgraduate pg)System.out.println("*研究生*");/System.out.println("");System.out.print("姓名");System.out.print('t'+"學生類型");System.out.print('t'+"C

40、語言成績");System.out.print('t'+"英語成績");System.out.print('t'+"Java成績");System.out.print('t'+"平均分");System.out.println('t'+"成績等級");for(int i=0; i<pg.length;i+)System.out.print(pgi.Name);System.out.print('t'+pgi.Stu_ty

41、pe);System.out.print('t'+String.valueOf(pgi.C_score);System.out.print('t'+String.valueOf(pgi.English_score);System.out.print('t'+String.valueOf(pgi.Java_score);System.out.print('t'+String.valueOf(pgi.score);System.out.println('t'+pgi.sco_Level);System.out.prin

42、tln();package Package_6;public class Test public static void main(String args) Undergraduate udg=new Undergraduate5;udg0=new Undergraduate("小明","本科生",25,95,74);udg1=new Undergraduate("小紅","本科生",60,82,82);udg2=new Undergraduate("小白","本科生",64,49,27);udg3=new Undergraduate("小黑","本科生",94,77,89);udg4=new Undergraduate("小軍","

溫馨提示

  • 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

提交評論