java試驗二要點_第1頁
java試驗二要點_第2頁
java試驗二要點_第3頁
java試驗二要點_第4頁
java試驗二要點_第5頁
已閱讀5頁,還剩20頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、信息工程學(xué)院1Java程序設(shè)計實驗/實習(xí)報告學(xué)院:班級:地信姓名:學(xué)號:2成績:A面向?qū)ο缶幊桃?、實?實習(xí)過程(一)實驗?zāi)康腏ava程序。掌握Java程序面向?qū)ο缶幊痰幕炯軜?gòu),會運用面向?qū)ο蟮乃枷刖帉懀ǘ嶒炦^程【實驗題1】完成如下程序,完成思考題基本要求運行程序并觀察運行結(jié)果。思考問題試述程序中主要語句的作用運行結(jié)果:主要程序語句的作用已標注為注釋:/類的聲明class CCircle double pi;/申明成員變量double radius;/申明成員變量double getRadius() / 創(chuàng)建方法實驗報告的內(nèi)容與格式按任課教師的要求書寫。void setCircle(do

2、uble r, double p) /創(chuàng)建方法 pi=p;/初始化數(shù)據(jù)成員radius=r;/初始化數(shù)據(jù)成員public class Ex2_1/類的聲明 public static void main( Stri ng args)主方法 CCircle cir仁new CCircle(); / 創(chuàng)建對象cir1.setCircle(2.0,3.1416);調(diào)用對象System.out.pri ntl n( "radius="+cir1.getRadius(); /輸出結(jié)果【實驗題2】設(shè)計一個用來描述汽車的類,使用類的靜態(tài)成員變量來表示汽車的車主姓 名、當(dāng)前的速率和當(dāng)前方向

3、盤的轉(zhuǎn)向角度,使用類的非靜態(tài)成員方法來表示改變汽車的 速率和停車兩個操作。package zz1;public class CarStri ngn ame;int speed ;int jiaodu ;publicCar(Stri ng n ame,int speed, int jiaodu)30this . name=name;this . speed =speed;this . jiaodu =jiaodu;System. out .println("車主:"+this .name);System. out .println("方向盤角度:"+thi

4、s . jiaodu );void cha ngen ame(Stri ng n ame)this . name=name;System. out .println("當(dāng)前車主為:"+this .name);void changespeed(int speed)this . speed =speed;System. out .println("當(dāng)前速度為:"+this . speed );void changejiaodu(int jiaodu)this . jiaodu =jiaodu;System. out .println("當(dāng)前方向盤角

5、度為:"+this . jiaodu );void stop()this . speed =0;System. out .println("停車后,車速為:"+this . speed );public static void main( Stri ng args)Car car= new Car("張三",0,0);car.changename("好友”);car.cha ngespeed(20);car.cha ngejiaodu(30);car.stop();結(jié)果:c: MisersXJldmxnisti*ator>(i:

6、CD: >cd eeIU): l>javac Car, javaD: 221 >cd友為好20囈 S 呂度為為盤車 :角王度向, ;為盤車速方后 至速®-赴翌睪 當(dāng)半當(dāng)停【實驗題3】定義一個類MyProgram,包含兩個屬性:一個是private 的整型屬性data、一個是private 的String 類型的屬性str,圭寸裝這兩個屬性的四個方法setData ()和getData ()、setStr() 和getStr ();將這兩個屬性轉(zhuǎn)變?yōu)樽址姆椒ㄊ莟oStr()。編寫程序,使用MyProgram類,實現(xiàn)數(shù)據(jù)的訪問和修改,并調(diào)用toStr ()方法 顯示

7、該類的屬性?;疽缶帉懲暾绦?。思考問題試述程序中各個方法的作用。代碼如下:package zz1;publicclass Myprogram privateintdata ;private String str ;void setdata( int data)this . data =data;int getdata()return data ;void setstr(Stri ng str)this . str =str;Stri ng getstr()return str ;Stri ng toStr()String a= data +""a=str ;return

8、 a;public static void main( Stri ng args)Myprogram m= new Myprogram();m. data =1000;m.str ="jgfldjsglkdj"System. out .println("轉(zhuǎn)換之前:"+m. data );結(jié)果如下:D:>cd 立土1D: !21>jauac Myprosram.jauaD : zel>cdsD:>jaua aal變機之前:1000 轉(zhuǎn)換之后:Jsrf Jdj思考問題試述程序中各個方法的作用。setData ()和setStr()方法

9、的作用是獲得數(shù)據(jù)和資料,getData ()、和getStr () 作用為設(shè)置數(shù)據(jù)和資料。toStr()函數(shù)用來將其他類型轉(zhuǎn)化為字符串類型。【實驗題4】 定義一個類實現(xiàn)銀行帳戶的概念,包括的變量有"帳號"和"存款余額",包括的方法有"存款"、"取款"、"查詢余額"和”顯示帳號”。定義主類,創(chuàng)建帳戶類的 對象,并完成相應(yīng)操作。提示:關(guān)鍵代碼如下:public int getleftm on ey()return leftm on ey;public void savem on ey(double

10、mon ey)leftm on ey+=mon ey;public void getm on ey(double mon ey)if(mon eyv=left mon ey)leftm on ey-=mon ey;elseban kacco unt ba=new ban kaccou nt(888123,1000);ba.savemo ney(21000);System.out.println(”存入 21000 元后余額為:"+ba.getleftmoney();ba.getmo ney(11500);System.out.pri ntln( "11500元后余額為:&q

11、uot;+ba.getleftmo ney();代碼如下:package zz1;public class Ban kaccou nt int acc;int leftm oney ;Ban kaccou nt( int acc, int leftmon ey)this . acc =acc;this . leftmoney =leftmoney;publicint getleft mon ey()returnleftm oney ;public void savemoney( double money)leftm oney +=mon ey;public void getmoney( dou

12、ble money)if (money<= leftmoney )leftm oney -=mon ey;elseSystem. out .println("只能?。?quot;+leftmoney );public static void main( Stri ng args)Ban kaccou nt ba=new Ba nkaccou nt(888123,1000);ba.savemo ney(21000);System. out .println("存入 21000 元后余額為:"+ba.getleftmoney();ba.getmo ney(115

13、00);System. out .println("取岀 11500 元后余額為:"+ba.getleftmoney();結(jié)果如下:I) : >cd zzlD:Banltaccount jav耳D: Szal>cdvJ) >jaua zzl. Bankaccount 存入210002;后余題為;22000 歐出"E00元后余額為:10500【實驗題5】 完成課本4.13 (102頁)jar文件的生成并產(chǎn)生Use類的運行結(jié)果II an a method In TestOne clas I am a method In TestTvo class【實

14、驗題6】 對象的組合?,F(xiàn)有一個 Rectangle類,請設(shè)計一個柱形類cuboid。1. 此類具有兩個成員變量,(1)Rectangle類型的成員變量rect (長方體以長方 形做底)和(2)height (柱形的高)。2. 此類應(yīng)具有構(gòu)造方法。3. ( 1)此類應(yīng)具有求面積的方法 getVolme()。(2)此類應(yīng)具有設(shè)置和獲得底的長度和寬度的方法:getBottomWidth(),setBottomWidth(),getBottomLe ngth(),setBottomLe ngth()。Rectangle的代碼如下:public class Recta ngle double width

15、,le ngth;public void setWidth(double w) if(w > 0)this.width二w;public double getWidth()return width;public void setLe ngth (double h) if(le ngth > 0) thisen gth =h;public double getLe ngth () return len gth;代碼如下:package zz1;public class Cuboid double rect , height ;double width , length ;Cuboid

16、( double height)this . height =height;public void getBottomWidth( double width)if (width > 0)this . width =width;public void setBottomWidth()System. out .println("底面寬是:"+width );public void getBottomLength( double length) if (length > 0)this . length=length;public void setBottomLengt

17、h ()System. out .println("底面長是:"+length );public void getVolme()double V;V=width *length * height ;System. out .println("柱形的體積是:"+V);public static void main(String args)Cuboid cub= new Cuboid(20);cub.getBottomWidth(15);cub.getBottomLe ngth(17);cub.setBottomWidth();cub.setBottomLe

18、 ngth(); cub.getVolme();結(jié)果如下:zl1517是 z 積 卷的 >面面形【實驗題5】有圖形類的父類Shape,參照圓Circle類補充完整正方性Square和三角 形Triangle類,并分析運行結(jié)果。class Shape void draw() void erase() class Circle exte nds Shape void draw() System.out.pri ntln ("Circle.draw()"); void erase() System.out.pri ntl n("Circle.erase()&quo

19、t;);class Square exte nds Shape void draw() void erase() class Trian gle exte nds Shape void draw() void erase() public class Shapes public static Shape ran dShape() switch(i nt)(Math.ra ndom() * 3) default: / To quiet the compilercase 0: return new Circle();case 1: return new Square();case 2: retur

20、n new Tria ngle();public static void main( Stri ng args) Shape s = new Shape3; / Fill up the array with shapes:for(i nt i = 0; i vs.len gth; i+)si = ran dShape(); / Make polymorphic method calls:for(i nt i = 0; i vs.len gth; i+)si.draw();補充完整的代碼如下:package zz1;class Shape void draw() void erase() cla

21、ss Circleexte nds Shape voiddraw() System.out .println("Circle.draw()");voiderase() System.out .println("Circle.erase()");classSquareexte ndsShapevoiddraw() System.out .println("Square.draw()");voiderase() System.out .println("Square.erase()");classTrian gleex

22、te ndsShape voiddraw() System.out .println("Triangle.erase()");voiderase() System.out .println("Triangle.erase()");publicclassShapes publicstaticShape ran dShape() switch ( intdefault:/ To quiet the compilercase 0:returnnew Circle();case 1:returnnew Square();case 2:returnnew Tria

23、 ngle();)(Math. random () * 3) publicstaticvoid main( Stri ng args) Shape s = new Shape3;/ Fill up the array with shapes:for (int i = 0; i <s.len gth ; i+)si = ran dShape();/ Make polymorphic method calls:for (int i = 0; i <s.len gth ; i+)si.draw();運行結(jié)果:>cd zzl【實驗題6】 有兩個類:MobileManagement和M

24、obile,分別描述如圖所示兩部手機名稱及價格,類MobileManagement 在包 .nwsuaf.jp.p3中,而 Mobile 在包.nwsuaf.jp.p3.data 中。它們代碼如下。運行 MobileManagement.java,你應(yīng)該 看到如圖所示結(jié)果?;疽笤诳瞻滋幪顚懴嚓P(guān)代碼并修改上面程序,使程序能夠顯示兩部手機的 價格和數(shù)量,運行結(jié)果如圖所示。E365, 1780 RMB M330, 1450 RMB手機及價格圖程序Mobile.java源代碼:public class Mobile /* Holds the n ame of the mob

25、ile. */ private String name;/* Holds the price of the mobile. */private float price;/* Creates a new mobile object. */public Mobile(String name, float price) this .name = name;this .price = price;/* Gets the n ame of the mobile. */public String getName() return name;/* Gets the price of the mobile.

26、*/Public float getPrice() return price;程序 MobileManagement.java源代碼:import javax.swing.JOptionPane;public class MobileMa nageme nt /* Defines the entry point of the applicatio n. */public static voidmain( Stri ng args) / Creates two mobile phone objects.Mobile mobilel = new Mobile("E365", 1

27、780);Mobile mobile2 = new Mobile("M330", 1450);/ Displays the two mobile phones in a dialog box.JOptionPane. showMessageDialog (null , "Mobilephon es:nn "+mobile1.getName()+"n"+mobile2.getName();實驗結(jié)果如下截圖:C:SUsers SAdmin1stratorJd:D: >javac cneduwsuaf jpp3 SdataJlobil

28、e . JavdD: >jauac D: XcneduVjiusuaf jpp3 XMobileFlanaQenent. Java0): >J*ua cn >edu. nwsuaf jp«p3 .Mo bile Manage me ntMobile phones:E365: 1780.0RMBM330:1450.0RMBThere are 2 mobile phones.確定【實驗題 7】有四個類,主類 Store 在包 .nwsuaf.jp.p4中,Mobile、Mp3Player、Product 在包 .nwsuaf.jp.p4.data

29、 中,Mobile、Mp3Player 是 Product 的子類,Product和Store代碼如下:Store.java 源代碼:package cn. edu .n wsuaf.jp.p4;import java.util.Arrays;import javax.swi ng.JOptio nPane;import cn. edu .n wsuaf.jp.p4.data.Mobile;import cn. edu .n wsuaf.jp.p4.data.Mp3Player;import cn. edu .n wsuaf.jp.p4.data.Product;public class St

30、ore /* Defines the entry point of the application. */public static void main( Stri ng args) / Creates two mobile phone objects.Mobile mobilel = new Mobile("Chi na Mobile", "E365",1780);Mobile mobile2 = new Mobile("Chi na Mobile", "M330",1450);Mp3Player player1

31、 = new Mp3Player("Meizo X3", 256, 399);Mp3Player player2 = new Mp3Player("Meizo E5", 512, 580);Mp3Player player3 = new Mp3Player("刈ve XM MP3 Play",256, 930);Product products=mobile1,mobile2,player1,player2, player3;Arrays.sort(products);Stri ng text =""for(i n

32、t in dex = 0; in dex <products .len gth; +in dex)text += productsi ndex.toStri ng()+"n"/ Displays the two mobile phones in a dialog box.JOpti onPan e.showMessageDialog( nu ll,"The productsare:nn"+text+"nThere are "+Product.getCou nt()+" products.");Mp3Playe

33、r的完整代碼如下:package cn. edu .n wsuaf.jp.p4.data;public class Mp3Player exte nds Productprivate int capacity;public Mp3Player(Stri ng n ame,i nt capacity,float price)super( name,price);this.capacity=capacity;public float getPrice()return price;public Stri ng n ame()return n ame;public static int coun t(

34、)retur n count;public Stri ng toStri ng()return name+" "+"("+capacity +" "+"MB"+")"+","+" "+price+" "+"RMB"Mobile的完整代碼如下:package cn. edu .n wsuaf.jp.p4.data;public class Mobile exte nds Productprivate String stri ng;public Mobile(Stri ng stri ng,Stri ng n ame,float price)super( name,price);

溫馨提示

  • 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

提交評論