第一篇:Java上機(jī)實(shí)驗(yàn)報(bào)告
Homework3實(shí)驗(yàn)報(bào)告
一、實(shí)驗(yàn)?zāi)康呐c要求
1、掌握J(rèn)ava中類(lèi)和接口的基本知識(shí);
2、繼承的基本知識(shí);
3、集合的基本操作
二、實(shí)驗(yàn)內(nèi)容 1.PIMCollection 類(lèi)
創(chuàng)建一個(gè)可以管理PIMEntities實(shí)體的集合,該類(lèi)實(shí)現(xiàn)了Collection接口,且必須實(shí)現(xiàn)了以下方法:(1).getNotes();返回PIMCollection中所有的PIMNote項(xiàng),如果不包含PIMNote項(xiàng),則返回一個(gè)空集合
(2).getTodos();返回集合PIMCollection中所有的PIMTodo項(xiàng),如果不包含PIMTodo項(xiàng),則返回一個(gè)空集合
(3).getAppointment();返回集合PIMCollection中所有的PIMAppointment項(xiàng),如果不包含PIMAppointment項(xiàng)。則返回一個(gè)空集合(4).getContacts();返回結(jié)合PIMCollection中所有的PIMContact項(xiàng),如果不包含PIMContact項(xiàng),則返回一個(gè)空集合
(5).getItemsForDate(Date d);返回集合PIMCollection中所有與d匹配的PIMEntities,如果沒(méi)有匹配d的項(xiàng),則返回一個(gè)空集合
(只有PIMTodo和PIMAppointment項(xiàng),因?yàn)镻IMNote和PIMContact跟日期沒(méi)有關(guān)系)。
2.TestPIMCollection類(lèi)(自己的測(cè)試類(lèi))
向PIMCollection集合類(lèi)中加入了一些PIMEntity實(shí)體類(lèi),并用PIMCollection類(lèi)中實(shí)現(xiàn)的方法分別篩選出了其中的PIMNote、PIMTodo、PIMAppointment、PIMContact、符合指定日期d的集合并打印輸出。
三、實(shí)驗(yàn)器材
計(jì)算機(jī)+windows操作系統(tǒng)+eclipse
四、實(shí)驗(yàn)步驟
1.程序源代碼(壓縮在文件夾里)2.編譯執(zhí)行程序 3.記錄執(zhí)行結(jié)果
五、實(shí)驗(yàn)結(jié)果
1.TestPIMCollection的測(cè)試代碼:
2.程序的執(zhí)行結(jié)果:
如圖所示:程序分別輸出了Todo、Note、Appointment、Contact、以及匹配指定日期的結(jié)合的結(jié)果。
六、實(shí)驗(yàn)小結(jié)
1、熟悉了Java集合的基本內(nèi)容和操作,也包括了泛型的一些知識(shí)。
2、通過(guò)這個(gè)實(shí)驗(yàn)鞏固了java的基礎(chǔ)知識(shí):類(lèi)和接口的有關(guān)知識(shí),繼承的應(yīng)用。
3、掌握了在eclipse下編譯執(zhí)行Java程序的知識(shí)。
4、通過(guò)幾次的編程,熟悉了java的編程規(guī)則。
5、綜合應(yīng)用所學(xué)知識(shí)完成java程序的分析、設(shè)計(jì)、調(diào)試和總結(jié),為以后編寫(xiě)更復(fù)雜的java程序打下了很好的基礎(chǔ)。
第二篇:上機(jī)實(shí)驗(yàn)報(bào)告格式
請(qǐng)各位同學(xué)按照下列格式寫(xiě):否則按照0分記
上機(jī)實(shí)驗(yàn)報(bào)告格式
Matlab學(xué)習(xí)第?次上機(jī)實(shí)驗(yàn)報(bào)告(第?次)
姓名:???班級(jí):???上機(jī)時(shí)間:??????
1.上機(jī)內(nèi)容
2.建模及算法分析
3.程序
4.小結(jié)
5.參考文獻(xiàn)
將文檔發(fā)到我的郵箱里面
learn_matlab@163.com
第三篇:上機(jī)實(shí)驗(yàn)報(bào)告
一. 題目1. 建立一個(gè)學(xué)生檔案,內(nèi)容包括學(xué)號(hào),姓名,年齡,性別,數(shù)學(xué),物理和英語(yǔ)3門(mén)功課成績(jī)。要求實(shí)現(xiàn)以下功能:1)數(shù)據(jù)輸入;2)查詢(xún)某個(gè)學(xué)生的成績(jī);3)按平均排列輸出;4)統(tǒng)計(jì)某門(mén)課各分?jǐn)?shù)段人數(shù);5)刪除某個(gè)學(xué)生記錄;2. 編程實(shí)現(xiàn)對(duì)二位數(shù)進(jìn)行加,減,乘運(yùn)算,每運(yùn)行一次程序做10道題,完成后給出成績(jī)(每題10分)。二. 設(shè)計(jì)思想和模塊劃分1.1.先定義所需要的條件,例如,姓名,學(xué)號(hào),性別,三門(mén)功課的成績(jī),平均分等。2.輸入姓名,學(xué)號(hào),性別,年齡,三門(mén)功課的成績(jī)后,就輸出相應(yīng)的姓名,學(xué)號(hào),性別,年齡,三門(mén)功課的成績(jī)。3.打印表頭;4.在求出平均分,并打印出平均分。5.在查找學(xué)生,并打印出學(xué)生的相關(guān)資料;并按照平均分的高低排列;6.在統(tǒng)計(jì)學(xué)生各科成績(jī)是否》=60分,如果是就是通過(guò),否就是沒(méi)通過(guò),最后打印出最后的結(jié)果。7.刪除學(xué)生,選中刪除的學(xué)生,如果確定要,那么該學(xué)生的一切記錄就變?yōu)?;2.1.隨機(jī)調(diào)用函數(shù),產(chǎn)生兩個(gè)其值為10到99的隨機(jī)整數(shù)分別放在c,d中用作運(yùn)算時(shí)的運(yùn)算數(shù)。2.隨機(jī)調(diào)用函數(shù),產(chǎn)生一個(gè)值為1到3 的隨機(jī)整數(shù)放入b中,用來(lái)選擇不同的運(yùn)算。根據(jù)b的值選擇不同的運(yùn)算。當(dāng)b=1時(shí),去進(jìn)行c+d的運(yùn)算練習(xí);當(dāng)b=2 時(shí),去進(jìn)行c-d的運(yùn)算練習(xí);當(dāng)b=3時(shí),去進(jìn)行c×d的運(yùn)算練習(xí)。每完成一個(gè)算題就給出是否正確的信息。3.根據(jù)答案正確與否統(tǒng)計(jì)。正確時(shí),顯示’right’;錯(cuò)誤時(shí),顯示’wrong’。4.步驟1.2.3.4.重復(fù)10次。5.輸出學(xué)生成績(jī)。三. 運(yùn)行結(jié)果1.Input the 1 student :num:03name:wfage:18sex:fmath:98phy:75eng:65Input the 2 student :num:06name:scage:17sex:fmath:78phy:45eng:65Input the 3 student :num:09name:ytage:17sex:mmath:69phy:75eng:501.readin 2.finds 3.del 4.tj 5.exit1-----------------------------table----num name age sex math phy eng ave------3 wf 18 f 98.0 75.0 65.0 79.3------9 yt 17 m 69.0 75.0 50.0 64.7-------6 sc 17 f 78.0 45.0 65.0 62.7-------1.readin 2.finds 3.del 4.tj 5.exit2Into number:3---num name age sex math phy eng ave---3 wf 18 f 98.0 75.0 65.0 79.3----1.readin 2.finds 3.del 4.tj 5.exit3Into number:6---num name age sex math phy eng ave---6 sc 17 f 78.0 45.0 65.0 62.6---true? Y/Ny1----------------table------------------num name age sex math phy eng ave----3 wf 18 f 98.0 75.0 65.0 79.3-----9 yt 17 m 69.0 75.0 50.0 64.7-----0 0 0 f 0.0 0.0 0.0 0.0-----1.readin 2.finds 3.del 4.tj 5.exit4 1.input the math 2.input the phy 3.input the eng 1math: pass num is :3 no pass num is :02.32+82=114right!97*15=1455right!99-58=41right!76-66=10right!58+13=71right!37+49=86right!99+52=151right!49*69=5462wrong!73-38=31wrong!86+34=120right!mark is 80四. 主要錯(cuò)誤改正方法1.在本題的編寫(xiě)過(guò)程中常出現(xiàn)begin 和end 不配對(duì),只要始它們的個(gè)數(shù)相同就可以了,就可以了。在程序中常出現(xiàn)的’unknown identifier’沒(méi)有定義的錯(cuò)誤,只要在程序前加上定義,就可以了。還有就是type mismatch’的類(lèi)型不匹配,只要根據(jù)前后內(nèi)容,重新定義,就可以了。2.在本題編寫(xiě)中,題目要求是兩個(gè)兩位數(shù)運(yùn)算,如果寫(xiě)成c:=random(100)在運(yùn)行過(guò)程中,就會(huì)出現(xiàn)一位數(shù)運(yùn)算,只要把它寫(xiě)成c:=random(90)+10,就可以了。五. 實(shí)習(xí)小結(jié)1.第一題的程序編寫(xiě)比較復(fù)雜,需要較多的定義,因而在后面的程序部分,就常出現(xiàn)標(biāo)識(shí)符重復(fù),而導(dǎo)致的類(lèi)型不匹配。還有就是每個(gè)部分編好后,上下不能連接,而無(wú)法調(diào)用。2.第二題較第一題簡(jiǎn)單,和書(shū)上的例題類(lèi)似,所以,比較簡(jiǎn)單,但從中也出現(xiàn)一些不問(wèn)題。以上兩大題中出現(xiàn)的問(wèn)題,要在同學(xué)的幫助下,才能完成。總的來(lái)說(shuō)兩題程序比較困難六. 程序清單1.program twins;const m=3;n=3;typestudent=recordnum:integer;name:string[3];age:integer;sex:char;s:array[1..n] of real;math,eng,phy:real;ave:real;end;sarr=array[1..m] of student;ta=array [1..m] of student;f=string[6];varstu:sarr;ct:ta;k,d:integer;procedure readin(var stu:sarr);const wrong='0<=data<=100 ,again!';var j,i:integer;a:student;beginwriteln;writeln('Input ',m,' num name sex math phy eng',' of student.');for j:=1 to m dobeginwriteln;writeln('Input the ',j,' student :');with a dobeginwrite('num:');readln(num);write('name:');readln(name);write('age:');readln(age);write('sex:');readln(sex);while(sex<>'m')and(sex<>'f')dobeginwrite('sex:');readln(sex)end;for i:=1 to n do begincase i of1:begin write('math:');readln(s[i]);while(s[i]<0)or(s[i]>100)dobeginwriteln(wrong);write('math:');readln(s[i]);end;end;2:begin write('phy:');readln(s[i]);while(s[i]<0)or(s[i]>100)dobeginwriteln(wrong);write('phy:');readln(s[i]);end;end;3:begin write('eng:');readln(s[i]);while(s[i]<0)or(s[i]>100)dobeginwriteln(wrong);write('eng:');readln(s[i]);end;end;end;end;stu[j]:=a;end;writeln;end;end;procedure ave(var stu:sarr);var j,i:integer;k:real;beginfor j:=1 to m dobegink:=0;with stu[j] dobeginfor i:=1 to n do k:=k+s[i];ave:=k/nend;end;end;procedure px(var stu:sarr);var tm:ta;j,i,p:integer;beginfor j:=1 to m dobeginp:=j;for i:=j+1 to m doif stu[i].ave>stu[p].ave then p:=i;tm[1]:=stu[p];stu[p]:=stu[j];stu[j]:=tm[1];end;end;procedure head(ct:ta);var i:integer;beginwrite(' ');for i:=1 to 72 do write('-');writeln;write('',' num':4,' ','name':9,' ','age':8,' ','sex':5,' ','math':6,' ','phy':9,' ','eng':8,' ');writeln('ave':6,' ');write(' ');for i:=1 to 72 do write('-');writeln;with ct[1] dobeginwrite('',num:7,' ',name:8,' ',age:8,' ',sex:5,' ');for i:=1 to n do write(s[i]:7,' ');write(ave:10,' ');writeln;end;write(' ');for i:=1 to 72 do write('-');writeln;end;procedure find(var stu:sarr);var n,i:integer;u:boolean;beginu:=true;while u dobeginwrite('Into number:');readln(n);for i:=1 to m dobeginif stu[i].num=n then beginct[1]:=stu[i];u:=false;d:=iend;end;end;end;procedure del(var stu:sarr);var i:integer;b:char;beginhead(ct);write('true? Y/N');readln(b);if b='y' then begin write('1');with stu[d] dobeginnum:=0;name:='0';age:=0;sex:='f';for i:=1 to n do s[i]:=0;ave:=0end;endelseend;procedure tj(var stu:sarr);var w1,q1,q2,num,j:integer;h1,h2,h3:boolean;beginh1:=false;h2:=false;h3:=false;q1:=0;q2:=0;writeln(' ':30,'1.input the math 2.input the phy 3.input the eng ');readln(w1);case w1 of1:h1:=true;2:h2:=true;3:h3:=true;end;if h1 thenbeginbeginfor j:=1 to m dowith stu[j] doif stu[j].math>=60 thenq1:=q1+1elseq2:=q2+1;writeln('math:');end;if h2 thenbeginbeginfor j:=1 to m dowith stu[j] doif stu[j].phy>=60 thenq1:=q1+1elseq2:=q2+1;writeln('phy:');end;if h3 thenbeginbeginfor j:=1 to m dowith stu[j] doif stu[j].eng>=60 thenq1:=q1+1elseq2:=q2+1;writeln('eng:');end;end;end;end;writeln(' ':5,'pass num is :',q1);writeln(' ':5,'no pass num is :',q2);end;procedure head1(var stu:sarr);var j,i:integer;beginwriteln('----------------table--------------':60);writeln;write(' ');for i:=1 to 72 do write('-');writeln;write('',' num':4,' ','name':9,' ','age':8,' ','sex':5,' ','math':6,' ','phy':9,' ','eng':8,' ');writeln('ave':6,' ');write(' ');for i:=1 to 72 do write('-');writeln;for j:=1 to m do beginwith stu[j] dobeginwrite('',num:7,' ',name:8,' ',age:8,' ',sex:5,' ');for i:=1 to n do write(s[i]:7:1,' ');write(ave:10:1,' ');writeln;end;write(' ');for i:=1 to 72 do write('-');writeln;end;end;beginreadin(stu);ave(stu);k:=0;while k<>5 dobeginwriteln('1.readin 2.finds 3.del 4.tj 5.exit');read(k);readln;while(k<>1)and(k<>2)and(k<>3)and(k<>4)and(k<>5)dobeginwriteln(' mistake,please again');read(k)end;case k of1: begin px(stu);head1(stu)end;2: begin find(stu);head(ct)end;3: begin find(stu);del(stu);px(stu);head1(stu)end;4: begin tj(stu);end;5: writeln('exit!');end;end;end..2.program shadow;var a,b,c,d,e:integer;right:boolean;procedure s1;beginwriteln('right!');right:=trueend;procedure s2;beginwriteln('wrong!');right:=falseend;procedure s3(x,y:integer);var h:integer;beginwrite(x,'+',y,'=');readln(h);if h=x+y then s1else s2 end;procedure s4(x,y:integer);var h,w:integer;beginif x 1、實(shí)驗(yàn)一:多線程程序設(shè)計(jì):炮打飛機(jī) 實(shí)驗(yàn)?zāi)康模簩W(xué)會(huì)使用多線程進(jìn)行并發(fā)程序設(shè)計(jì) 實(shí)驗(yàn)內(nèi)容:編寫(xiě)一個(gè)應(yīng)用程序模擬大炮打飛機(jī)。在GUI界面上,飛機(jī)水平飛行,用界面上的按鈕控制大炮的運(yùn)行方向(如向左,向右,向上,向下)。當(dāng)炮彈碰著飛機(jī)后,飛機(jī)墜落。 2、實(shí)驗(yàn)二:數(shù)據(jù)庫(kù)編程:書(shū)目信息管理 實(shí)驗(yàn)?zāi)康模赫莆諗?shù)據(jù)庫(kù)編程技術(shù)。能正確連接數(shù)據(jù)庫(kù),能對(duì)數(shù)據(jù)庫(kù)中信息進(jìn)行查詢(xún)、插 入、刪除、修改。 實(shí)驗(yàn)內(nèi)容:在數(shù)據(jù)庫(kù)中創(chuàng)建一張書(shū)目信息表,包括書(shū)名、作者、出版社、出版日期、書(shū) 號(hào)、價(jià)格字段。設(shè)計(jì)一個(gè)GUI界面進(jìn)行書(shū)目管理。在該界面上有四個(gè)選項(xiàng)卡,分別是查詢(xún)、插入、刪除、修改。點(diǎn)擊查詢(xún)選項(xiàng)卡,出現(xiàn)的界面上有書(shū)名、作者、出版社、書(shū)號(hào)四個(gè)文本框,一個(gè)按鈕和一個(gè)只讀文本區(qū)。文本框內(nèi)容可以為空,輸入相應(yīng)的查詢(xún)信息后(例如根據(jù)書(shū)名查詢(xún)可以?xún)H輸入書(shū)名),點(diǎn)擊界面上的“查詢(xún)”按鈕,可以在界面下方的文本區(qū)中顯示出符合條件的書(shū)目詳細(xì)信息。點(diǎn)擊插入選項(xiàng)卡,出現(xiàn)的界面上有書(shū)名、作者、出版社、出版日期、書(shū)號(hào)、價(jià)格文本框,一個(gè)按鈕。在文本框中輸入信息后,點(diǎn)擊“插入”按鈕,該書(shū)目信息插入數(shù)據(jù)庫(kù)表中。點(diǎn)擊刪除選項(xiàng)卡,出現(xiàn)的界面上有書(shū)名文本框和一個(gè)按鈕,輸入書(shū)名后點(diǎn)擊“刪除”按鈕,該書(shū)目信息從數(shù)據(jù)庫(kù)表中刪除。點(diǎn)擊修改選項(xiàng)卡,出現(xiàn)的界面上有書(shū)名、作者、出版社、出版日期、書(shū)號(hào)、價(jià)格文本框,一個(gè)按鈕。輸入的書(shū)名必須是已存在的,否則會(huì)彈出消息框顯示出錯(cuò)信息。輸入信息后,點(diǎn)擊“修改”按鈕,數(shù)據(jù)庫(kù)表中的相應(yīng)書(shū)目信息被修改為新值。 3、實(shí)驗(yàn)三:Applet編程:鴨子的移動(dòng) 實(shí)驗(yàn)?zāi)康模赫莆誂pplet程序設(shè)計(jì)的方法,學(xué)會(huì)在Applet中繪制圖片,能采用雙緩沖技術(shù)進(jìn)行圖片繪制,會(huì)在Applet中采用多線程進(jìn)行動(dòng)畫(huà)顯示。能在Applet中實(shí)現(xiàn)動(dòng)畫(huà)的平滑顯示,避免閃爍現(xiàn)象。 實(shí)驗(yàn)內(nèi)容:在Applet窗口中顯示一只鴨子圖片,要求這只鴨子以每秒100幀的速率水平移動(dòng),移到窗口邊界再返回移動(dòng)。在窗口中點(diǎn)擊鼠標(biāo),鴨子停止移動(dòng);再次點(diǎn)擊鼠標(biāo),鴨子恢復(fù)移動(dòng)。 4、實(shí)驗(yàn)四:網(wǎng)絡(luò)編程:聊天室的實(shí)現(xiàn) 實(shí)驗(yàn)?zāi)康模簳?huì)編寫(xiě)客戶(hù)端和服務(wù)器程序?qū)崿F(xiàn)C/S模式下的通信,會(huì)使用多線程進(jìn)行網(wǎng)絡(luò)編程。 實(shí)驗(yàn)內(nèi)容:編寫(xiě)一個(gè)聊天室程序:客戶(hù)端界面請(qǐng)自行設(shè)計(jì),可使其具有個(gè)性化特征; 可在實(shí)現(xiàn)基本的聊天室功能的基礎(chǔ)上添加個(gè)性化的功能。 學(xué) 生 實(shí) 驗(yàn) 報(bào) 告 冊(cè) (理工類(lèi)) 課程名稱(chēng):面向?qū)ο蟪绦蛟O(shè)計(jì) 專(zhuān)業(yè)班級(jí):16計(jì)算機(jī)科學(xué)與技術(shù)(專(zhuān)轉(zhuǎn)本) 學(xué)生學(xué)號(hào): 1613203022 學(xué)生姓名: 張義丹 所屬院部: 計(jì)算機(jī)工程 指導(dǎo)教師: 劉 晶 16 ——20 17 學(xué)年 第 2 學(xué)期 金陵科技學(xué)院教務(wù)處制 實(shí)驗(yàn)報(bào)告書(shū)寫(xiě)要求 實(shí)驗(yàn)報(bào)告上交電子稿,標(biāo)題采用四號(hào)黑體,正文采用小四號(hào)宋體,單倍行距。 實(shí)驗(yàn)報(bào)告書(shū)寫(xiě)說(shuō)明 實(shí)驗(yàn)報(bào)告中實(shí)驗(yàn)?zāi)康暮鸵蟆?shí)驗(yàn)儀器和設(shè)備、實(shí)驗(yàn)內(nèi)容與過(guò)程、實(shí)驗(yàn)結(jié)果與分析這四項(xiàng)內(nèi)容為必需項(xiàng)。教師可根據(jù)學(xué)科特點(diǎn)和實(shí)驗(yàn)具體要求增加項(xiàng)目。 填寫(xiě)注意事項(xiàng) (1)細(xì)致觀察,及時(shí)、準(zhǔn)確、如實(shí)記錄。(2)準(zhǔn)確說(shuō)明,層次清晰。 (3)盡量采用專(zhuān)用術(shù)語(yǔ)來(lái)說(shuō)明事物。 (4)外文、符號(hào)、公式要準(zhǔn)確,應(yīng)使用統(tǒng)一規(guī)定的名詞和符號(hào)。(5)應(yīng)獨(dú)立完成實(shí)驗(yàn)報(bào)告的書(shū)寫(xiě),嚴(yán)禁抄襲、復(fù)印,一經(jīng)發(fā)現(xiàn),以零分論處。 實(shí)驗(yàn)報(bào)告批改說(shuō)明 實(shí)驗(yàn)報(bào)告的批改要及時(shí)、認(rèn)真、仔細(xì),一律用紅色筆批改。實(shí)驗(yàn)報(bào)告的批改成績(jī)采用五級(jí)記分制或百分制,按《金陵科技學(xué)院課堂教學(xué)實(shí)施細(xì)則》中作業(yè)批閱成績(jī)?cè)u(píng)定要求執(zhí)行。 實(shí)驗(yàn)項(xiàng)目名稱(chēng):Java編程基礎(chǔ) 實(shí)驗(yàn)學(xué)時(shí): 6 同組學(xué)生姓名: ———— 實(shí)驗(yàn)地點(diǎn): 工科樓A101 實(shí)驗(yàn)日期: 17.3.21~17.4.4 實(shí)驗(yàn)成績(jī): 批改教師: 劉晶 批改時(shí)間: 實(shí)驗(yàn)1 Java編程基礎(chǔ) 一、實(shí)驗(yàn)?zāi)康暮鸵?/p> (1)熟練掌握J(rèn)DK1.6及Eclipse4.2編寫(xiě)調(diào)試Java應(yīng)用程序及Java小程序的方法;(2)熟練掌握J(rèn)ava應(yīng)用程序的結(jié)構(gòu); (3)了解Java語(yǔ)言的特點(diǎn),基本語(yǔ)句、運(yùn)算符及表達(dá)式的使用方法;(4)熟練掌握常見(jiàn)數(shù)據(jù)類(lèi)型的使用; (5)熟練掌握if-else、switch、while、do-while、for、continue、break、return語(yǔ)句的使用方法; (6)熟練掌握數(shù)組和字符串的使用; (7)調(diào)試程序要記錄調(diào)試過(guò)程中出現(xiàn)的問(wèn)題及解決辦法; (8)編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄,不斷積累編程及調(diào)試經(jīng)驗(yàn); (9)做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。 二、實(shí)驗(yàn)儀器和設(shè)備 奔騰以上計(jì)算機(jī),Windows 操作系統(tǒng),裝有JDK1.6和Eclipse4.2軟件。 三、實(shí)驗(yàn)過(guò)程 (1)分別使用JDK命令行和Eclipse編譯運(yùn)行Java應(yīng)用程序;適當(dāng)添加注釋信息,通過(guò)javadoc生成注釋文檔;為主方法傳遞參數(shù)“Hello world”字符串,并輸出,記錄操作過(guò)程。 public class Hello { public static void main(String args[]){ System.out.println(“Hello!”);} }(2)分別使用JDK命令行和Eclipse編譯Java Applet,并建立HTML文檔運(yùn)行該Applet。壓縮生成“.jar”文件。記錄操作過(guò)程。import java.awt.*;import java.applet.Applet;public class HelloApplet extends Applet { public void paint(Graphics g){ g.setColor(Color.red);g.drawString(“Hello!”,20,20);} } (3)根據(jù)變量score中存放的考試分?jǐn)?shù),輸出對(duì)應(yīng)的等級(jí)。要求從鍵盤(pán)輸入學(xué)生成績(jī),60分以下為D等;60~69為C等;70~89為B等;90~100為A等。(4)編寫(xiě)一個(gè)Java Application程序,輸出區(qū)間[200,300]上的所有素?cái)?shù),將其用數(shù)組prime[]保存,并以每10個(gè)一行的形式顯示運(yùn)行結(jié)果。(5)輸出下列數(shù)字形式,要求用二維數(shù)組完成。①n=4 0 0 0 0 0 1 1 1 0 1 2 2 0 1 2 3 ② n=4 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1(6)求二維數(shù)組的鞍點(diǎn),即該位置上的元素在該行上最大,在列上最小。也可能無(wú)鞍點(diǎn)。(7)分析下列程序的輸出結(jié)果,掌握equals()方法和“= =”的區(qū)別。class StringTest2{ public static void main(String args[]){ String s1 = “This is the second string.”; String s2 = “This is the second string.”; String s3 = new String(“This is the second string.”); String s4 = new String(s1); String s5 = s1; boolean result121 = s1.equals(s2); boolean result122 = s1 == s2; boolean result131 = s1.equals(s3); boolean result132 = s1 == s3; boolean result141 = s1.equals(s4); boolean result142 = s1 == s4; boolean result151 = s1.equals(s5); boolean result152 = s1 == s5; System.out.println(“s1 equals s2= ” +result121); System.out.println(“s1 == s2= ” +result122); System.out.println(“s1 equals s3= ” +result131); System.out.println(“s1 == s3= ” +result132); System.out.println(“s1 equals s4= ” +result141); System.out.println(“s1 == s4= ” +result142); System.out.println(“s1 equals s5= ” +result151); System.out.println(“s1 == s5= ” +result152);} }(8)判斷回文字符串 回文是一種“從前向后讀”和“從后向前讀”都相同的字符串。如“rotor”是一個(gè)回文字符串。 程序中使用了兩種算法來(lái)判斷回文字符串: 算法一:分別從前向后和從后向前依次獲得原串str的一個(gè)字符ch1、ch2,比較ch1和ch2,如果不相等,則str肯定不是回文串,yes=false,立即退出循環(huán):否則繼續(xù)比較,直到字符全部比較完,yes的值仍為true,才能肯定str是回文串。 算法二:將原串str反轉(zhuǎn)成temp串,再比較兩串,如果相等則是回文字符串。(9)使用String類(lèi)的compareTo(String s)方法,對(duì)以下字符串從小到大排序:“melon”, “apple”, “pear”, “banana”,顯示輸出排序結(jié)果。 要求: (1)編譯調(diào)試程序之前應(yīng)配置好環(huán)境變量; (2)要分別掌握用JDK命令行和Eclipse集成開(kāi)發(fā)環(huán)境調(diào)試Java程序;(3)注意Java兩大類(lèi)程序:應(yīng)用程序和小程序的區(qū)別。 程序清單: (建議程序中適當(dāng)添加注釋信息,增強(qiáng)可讀性;較長(zhǎng)程序可分欄書(shū)寫(xiě),保證報(bào)告排版整潔美觀。) (1)主方法傳遞參數(shù)“Hello world”字符串,并輸出 public class Hello { public Hello(){ System.out.println(“HelloWorld!”);} public static void main(String args[]){ new Hello();} }(2)Eclipse編譯Java Applet import java.awt.*;import java.applet.Applet;public class HelloApplet extends Applet { int height,width;public void init(){ this.height=100; this.width=300;} public void paint(Graphics g){ g.setColor(Color.red); g.drawString(“Hello!”, 20, 20);} }(3) package b;import java.util.Scanner;public class Test { public static void main(String args[]){ int score; //char grade; System.out.println(“請(qǐng)輸入分?jǐn)?shù)按回車(chē)”); Scanner reader=new Scanner(System.in); score=reader.nextInt(); if(score>=90&&score<=100){ System.out.println(“A”); } else if(score>=70&&score<=89){ System.out.println(“B”); } else if(score>=60&&score<=69){ System.out.println(“C”); } else if(score<60){ System.out.println(“D”); } else{ System.out.println(“數(shù)據(jù)錯(cuò)誤”); } } }(4) public class Lim { public static void main(String[] args){ int[] prime = calculation(20, 200, 300); for(int i = 0;i < prime.length;i++){ if(prime[i]!= 0){ if(i % 10 == 0 && i!= 0) System.out.println(); System.out.print(prime[i] + “ ”);// 打印數(shù)據(jù) } } } public static int[] calculation(int length, int start, int end){ int j; int step = 0; int[] prime = new int[length]; for(int i = start;i <= end;i++) { j = 2; while(i % j!= 0){ j++; } if(j == i) { prime[step] = i; step++; } } return prime; } }(5)① public class shuzu { public static void main(String args[]){ int i,j; int arr[][]=new int[4][]; for(i=0;i arr[i]=new int[arr.length];for(i=0;i<4;i++) for(j=3;j>=i;j--){ arr[i][j]=i; } for(j=0;j<4;j++){ for(i=3;i>=j;i--){ arr[i][j]=j; } } for(i=0;i<=3;i++){ for(j=0;j<=3;j++){ System.out.print(arr[i][j]); System.out.print(“ ”); } System.out.println(); } } } ②public class Shuzu { public static void main(String args[]){ int num[][]=new int[4][];for(int i=0;i num[i]=new int[2*i+1]; for(int m=0;m System.out.print(“ ”); } int k=i+1; for(int j=0;j if(j<=i) num[i][j]=j+1; else{ k--; num[i][j]=k; } System.out.print(num[i][j]+“ ”); } System.out.println();} } }(6)public class test { public static void main(String[] args){ // TODO Auto-generated method stub int[][] mat = {{11,12,13},{4,5,6},{7,8,9}}; for(int i=0;i { for(int j=0;j System.out.print(mat[i][j]+“ ”); System.out.println(); } boolean find = false;//找到鞍點(diǎn)標(biāo)記 int row=0;//第1行下標(biāo) int max=0;//記錄當(dāng)前行最大值的列下標(biāo) while(!find && row { max=0;//初始設(shè)每行第1列值最大 for(int j=1;j if(mat[row][j]>mat[row][max])//mat[row][max]為該行最大值 max = j; boolean yes = true;//再判斷mat[row][max]是否在列上最小 int j=0; while(yes && j { if(mat[j][max] yes=false; j++; } if(yes) find = true; else row++; } if(find) System.out.println(“The dort: ”+mat[row][max]); else System.out.println(“The dort: null”);} }(8)import java.util.Scanner;public class HuiWenTest { public static void main(String[] args){ // TODO Auto-generated method stub System.out.println(“請(qǐng)輸入一個(gè)字符串”);@SuppressWarnings(“resource”) Scanner input = new Scanner(System.in);String str = input.next();StringBuilder sb=new StringBuilder(str);sb.reverse();//將Sr倒置的方法 String newStr=new String(sb);if(str.equals(newStr)){ System.out.println(str+“是回文字符串”);}else{ System.out.println(str+“不是回文字符串”);} } }(9)import java.util.*;public class SortString { public static void main(String[] args){ // TODO Auto-generated method stub String [ ] a={“melon”,“apple”,“pear”,“banana”}; String [ ] b=Arrays.copyOf(a,a.length);System.out.println(“使用用戶(hù)編寫(xiě)的SortString類(lèi),按字典序排列數(shù)組a:”);SortString.sort(a);System.out.println(“排序結(jié)果是:”);for(String s:a){ System.out.print(“ ”+s);} System.out.println(“");System.out.println(”使用類(lèi)庫(kù)中的Arrays類(lèi),按字典序排列數(shù)組b:“);Arrays.sort(b);System.out.println(”排序結(jié)果是:“);for(String s:b){ System.out.print(” “+s);} } 四、實(shí)驗(yàn)結(jié)果與分析(程序運(yùn)行結(jié)果及其分析) (1) (2) (3) (4) (5) (6) (7) (8) (9) 五、實(shí)驗(yàn)體會(huì)(遇到問(wèn)題及解決辦法,編程后的心得體會(huì)) 在這次實(shí)驗(yàn)中,我知道了eclipse和jdk運(yùn)行程序的區(qū)別,jdk比較麻煩一些,需要配置變量。在實(shí)驗(yàn)中,配置jdk的環(huán)境變量要注意它的path和 classpath,如果classpath本身就有,可以在后面加分號(hào),這樣不影響其它的classpath的使用。學(xué)會(huì)了如何生成注釋文檔,主函數(shù)傳遞參數(shù)的方法,還有壓縮文件,實(shí)驗(yàn)中還對(duì)數(shù)組的創(chuàng)建和使用進(jìn)行了練習(xí),還有一些類(lèi)的應(yīng)用。 實(shí)驗(yàn)項(xiàng)目名稱(chēng): 面向?qū)ο缶幊?實(shí)驗(yàn)學(xué)時(shí): 8 同組學(xué)生姓名: ———— 實(shí)驗(yàn)地點(diǎn): 工科樓A101 實(shí)驗(yàn)日期: 17.4.11~17.5.2 實(shí)驗(yàn)成績(jī): 批改教師: 劉晶 批改時(shí)間: 實(shí)驗(yàn)2 面向?qū)ο缶幊?/p> 一、實(shí)驗(yàn)?zāi)康暮鸵?/p> (1)熟練掌握J(rèn)ava語(yǔ)言類(lèi)定義的基本語(yǔ)法;(2)熟練掌握類(lèi)成員的訪問(wèn)控制,對(duì)象建立的方法;(3)熟練掌握類(lèi)構(gòu)造方法、成員方法的定義和重載;(4)熟練掌握類(lèi)繼承、多態(tài)和抽象性;(5)熟練掌握接口的定義和實(shí)現(xiàn)方法;(6)掌握基本的異常處理方法; (7)調(diào)試程序要記錄調(diào)試過(guò)程中出現(xiàn)的問(wèn)題及解決辦法; (8)編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄,不斷積累編程及調(diào)試經(jīng)驗(yàn); (9)做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。 二、實(shí)驗(yàn)儀器和設(shè)備 奔騰以上計(jì)算機(jī),Windows 操作系統(tǒng),裝有JDK1.6和Eclipse4.2軟件。 三、實(shí)驗(yàn)過(guò)程 (1)定義一個(gè)Man類(lèi),保存在Man.java文件中,類(lèi)中包含說(shuō)話方法如下: public class Man { public void say() { System.out.println(“我是中國(guó)人!”); } } 為此類(lèi)打包為cn.edu.jit.chinese;再在Man.java文件所在路徑下,創(chuàng)建一個(gè)China.java文件,其中定義China類(lèi)如下: public class China { public static void main(String[] args) { Man lihua = new Man(); lihua.say(); } } 在China類(lèi)中引用Man類(lèi),輸出顯示“我是中國(guó)人!”。試著去掉Man類(lèi)的public修飾,看看會(huì)發(fā)生什么情況? (2)設(shè)計(jì)復(fù)數(shù)類(lèi),成員變量包括實(shí)部和虛部,成員方法包括實(shí)現(xiàn)復(fù)數(shù)加法、減法、字符串描述、比較是否相等等操作。 (3)包的建立與使用:設(shè)計(jì)計(jì)算器類(lèi)Calculator,計(jì)算加、減、乘、除和立方體體積,并且打包為mypackage。觀察源文件目錄下是否生成了mypackage文件夾,在該文件夾中是否有Calculate.class文件。編輯PackageDemo.java,保存在Calculator.java同一目錄下,引用計(jì)算器類(lèi)的各方法顯示計(jì)算結(jié)果。 (4)試編碼實(shí)現(xiàn)簡(jiǎn)單的銀行業(yè)務(wù):處理簡(jiǎn)單帳戶(hù)存取款、查詢(xún)。編寫(xiě)銀行帳戶(hù)類(lèi)BankAccount,包含數(shù)據(jù)成員:余額(balance)、利率(interest);操作方法:查詢(xún)余額、存款、取款、查詢(xún)利率、設(shè)置利率。再編寫(xiě)主類(lèi)UseAccount,包含main()方法,創(chuàng)建BankAccount類(lèi)的對(duì)象,并完成相應(yīng)操作。 (5)假定根據(jù)學(xué)生的3門(mén)學(xué)位課程的分?jǐn)?shù)決定其是否可以拿到學(xué)位,對(duì)于本科生,如果3門(mén)課程的平均分?jǐn)?shù)超過(guò)60分即表示通過(guò),而對(duì)于研究生,則需要平均超過(guò)80分才能夠通過(guò)。根據(jù)上述要求,請(qǐng)完成以下Java類(lèi)的設(shè)計(jì): 1)設(shè)計(jì)一個(gè)基類(lèi)Student描述學(xué)生的共同特征。 2)設(shè)計(jì)一個(gè)描述本科生的類(lèi)Undergraduate,該類(lèi)繼承并擴(kuò)展Student類(lèi)。3)設(shè)計(jì)一個(gè)描述研究生的類(lèi)Graduate,該類(lèi)繼承并擴(kuò)展Student類(lèi)。 4)設(shè)計(jì)一個(gè)測(cè)試類(lèi)StudentDemo,分別創(chuàng)建本科生和研究生這兩個(gè)類(lèi)的對(duì)象,并輸出相關(guān)信息。 (6)設(shè)計(jì)三角形類(lèi),繼承圖形抽象類(lèi),計(jì)算三角形面積和周長(zhǎng)。 (7)試編碼實(shí)現(xiàn)多態(tài)在工資系統(tǒng)中的應(yīng)用:給出一個(gè)根據(jù)雇員類(lèi)型利用abstract方法和多態(tài)性完成工資單計(jì)算的程序。Employee是抽象類(lèi),Employee的子類(lèi)有Boss(每星期發(fā)給他固定工資,而不計(jì)工作時(shí)間)、CommissionWorker(除基本工資外還根據(jù)銷(xiāo)售額發(fā)放浮動(dòng)工資)、PieceWorker(按其生產(chǎn)的產(chǎn)品數(shù)發(fā)放工資)、HourlyWorker(根據(jù)工作時(shí)間長(zhǎng)短發(fā)放工資)。該例的Employee的每個(gè)子類(lèi)都聲明為final,因?yàn)椴恍枰倮^承它們生成子類(lèi)。在主測(cè)試類(lèi)Test中測(cè)試各類(lèi)雇員工資計(jì)算結(jié)果。 提示:對(duì)所有雇員類(lèi)型都使用earnings()方法,但每個(gè)人掙的工資按他所屬的雇員類(lèi)計(jì)算,所有雇員類(lèi)都是從超類(lèi)Employee派生出的。在超類(lèi)中聲明earnings()為抽象方法,并且對(duì)于每個(gè)子類(lèi)都提供恰當(dāng)?shù)膃arnings()的實(shí)現(xiàn)方法。為了計(jì)算雇員的工資,程序僅僅使用雇員對(duì)象的一個(gè)超類(lèi)引用并調(diào)用earnings()方法。在一個(gè)實(shí)際的工資系統(tǒng)中,各種Employee對(duì)象的引用可以通過(guò)一個(gè)Employee引用數(shù)組來(lái)實(shí)現(xiàn)。程序依次使用數(shù)組的每個(gè)元素(Employee引用)調(diào)用每個(gè)對(duì)象的earnings()方法。Employee類(lèi)定義如下: abstract class Employee { private String firstName;private String lastName;public Employee(String first,String last){ firstName=first;lastName=last;} public String getEmployeeName(){ return firstName;} public String getLastName(){ return lastName;} public String toString(){ return firstName+lastName;} public abstract String earnings();}(8)設(shè)計(jì)圓柱體類(lèi)和圓椎體類(lèi),繼承圓類(lèi)Circle并實(shí)現(xiàn)體積接口Volume,計(jì)算表面積和體積。 (9)定義一個(gè)接口CanFly,描述會(huì)飛的方法public void fly();分別定義飛機(jī)類(lèi)和鳥(niǎo)類(lèi),實(shí)現(xiàn)CanFly接口。定義一個(gè)測(cè)試類(lèi),測(cè)試飛機(jī)和鳥(niǎo)。測(cè)試類(lèi)中定義一個(gè)makeFly(CanFly obj)方法,讓會(huì)飛的事物飛起來(lái)(即調(diào)用相應(yīng)類(lèi)的fly()方法)。然后在main方法中創(chuàng)建飛機(jī)對(duì)象和鳥(niǎo)對(duì)象,并在main方法中調(diào)用makeFly(CanFly obj)方法,讓飛機(jī)和鳥(niǎo)起飛。 (10)異常的捕獲:計(jì)算兩數(shù)相除并輸出結(jié)果。使用三個(gè)catch子句,分別捕捉輸入輸出異常、除數(shù)為0的異常和參數(shù)輸入有誤異常。import java.io.*;class Ex1 { public static void main(String args[ ]){ try{ BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));//建立輸入流緩沖區(qū) System.out.print(”請(qǐng)輸入除數(shù):“);String cl=strin.readLine();//鍵盤(pán)輸入 int a=Integer.parseInt(cl);System.out.print(”請(qǐng)輸入被除數(shù):“);cl=strin.readLine();int b=Integer.parseInt(cl);int c=b/a;System.out.println(”商為:“+c);} //捕獲與I/O有關(guān)的異常(空白處補(bǔ)全捕獲語(yǔ)句) //捕獲數(shù)值轉(zhuǎn)化時(shí)的異常,如不能將字符轉(zhuǎn)化成數(shù)值 //捕獲除數(shù)為0的異常 } } 編譯并運(yùn)行,當(dāng)產(chǎn)生輸入輸出異常時(shí)顯示異常信息;當(dāng)輸入除數(shù)為0時(shí),出現(xiàn)算術(shù)異常,提示除數(shù)為0,并要求重新輸入;當(dāng)輸入的不是整數(shù)時(shí),如將30輸成了3o,出現(xiàn)數(shù)值格式異常,提示輸入整數(shù)。 (11)編寫(xiě)程序包含自定義異常MyException,當(dāng)100被13和4除時(shí)拋出該異常,其余除數(shù)顯示商值。 要求: (1)注意選用適當(dāng)?shù)念?lèi)成員修飾符(private、protected、public等),比較它們的使用情況; (2)養(yǎng)成良好的編程習(xí)慣,嚴(yán)格按照命名規(guī)則為包、類(lèi)及類(lèi)成員命名,將每個(gè)程序打包,包的命名方式如two.num1表示實(shí)驗(yàn)二的第一題; (3)學(xué)會(huì)使用Eclipse的各種調(diào)試方法; (4)學(xué)會(huì)查閱Java API文檔,如查找異常類(lèi)的使用方法。 程序清單: (建議程序中適當(dāng)添加注釋信息,增強(qiáng)可讀性;較長(zhǎng)程序可分欄書(shū)寫(xiě),保證報(bào)告排版整潔美觀。)(1)package cn.edu.jit.chinese;// 為Man類(lèi)打包為cn.edu.jit.chinese public class Man { public void say(){ System.out.println(”我是中國(guó)人!“);} } package cn.edu.jit.chinese; import cn.edu.jit.chinese.*;//導(dǎo)入包 public class China { public static void main(String[] args){ Man lihua = new Man();//主方法先創(chuàng)建類(lèi)然后調(diào)用類(lèi) lihua.say();} }(2)public class Complex { private double real,image;//定義私有的real,image public Complex(double real,double image) {this.real=real;//賦值 this.image=image;} public Complex(double real){this(real,0);} public Complex(){this(0,0);} public Complex(Complex c){this(c.real,c.image);} public double getReal(){return real;} public void setReal(double real){ this.real = real;} public double getImage(){ return image;} public void setImage(double image){ this.image = image;} public Complex add(Complex c1,Complex c2)//寫(xiě)方法 {Complex C=new Complex(c1.real+c2.real,c1.image+c2.image);return C;} public Complex add(Complex c1){Complex C=new Complex(this.real+c1.real,this.image+c1.image);return C;} public Complex jian(Complex c1,Complex c2){Complex C=new Complex(c1.real-c2.real,c1.image-c2.image);return C;} public Complex jian(Complex c1){Complex C=new Complex(this.real-c1.real,this.image-c1.image);return C;} public boolean bijiao(Complex c1,Complex c2){return(c1.real==c2.real&&c1.image==c2.image);} public boolean bijiao(Complex c1){return(c1.real==this.real&&c1.image==this.image);} public String toString(){return this.real+”+“+this.image+”i“;} } public class ComplexText { public static void main(String[] args){ Complex c1=new Complex(2,5);//創(chuàng)建類(lèi),調(diào)用類(lèi)里面的方法 Complex c2=new Complex(5,2); Complex c3=new Complex(); System.out.println(c3.add(c1,c2)); System.out.println(c3.jian(c1,c2)); System.out.println(c3.bijiao(c1,c2));} }(3)public class Calculate { double i,j, t;public Calculate(int i,int j){this.i=i;this.j=j;} public Calculate(int i,int j,int t){this.i=i;this.j=j;this.t=t;} public double add(){return i+j;} public double jian(){return i-j;} public double cheng(){return i*j;} public double chu(){return i/j;} public double tiji(){return i*i*i+j*j*j+t*t*t;} } public class PackageDemo {//測(cè)試 public static void main(String[] args){ Calculate c1=new Calculate(8,4); Calculate c2=new Calculate(8,4,2); System.out.println(”相加=“+c1.add()); System.out.println(”相減=“+c1.jian()); System.out.println(”相乘=“+c1.cheng()); System.out.println(”相除 =“+c1.chu()); System.out.println(”立方體體積=“+c2.tiji());} }(4)public class BankAccount { double balance,interest,cunkuan;public BankAccount(double cunkuan,double balance)//寫(xiě)方法 {this.balance=balance; this.cunkuan=cunkuan;} public void set(double cunkuan) {if(cunkuan<10000)interest=0.1; else if(cunkuan<50000)interest=0.25; else if(cunkuan<100000)interest=0.035; else interest=0.5;} public double get() { return interest;} public void chaxun(double balance,double cunkuan) {System.out.println(”存款為:“+cunkuan); System.out.println(”余額為:“+balance);} public void qu(double qukuan) {System.out.println(”取款為:“+qukuan);System.out.println(”得到的利潤(rùn)率:“+(this.cunkuan-qukuan)*this.interest);} } public class UseAccount {//測(cè)試 public static void main(String[] args){ BankAccount c1=new BankAccount(40000,40000); c1.chaxun(40000,20000); c1.set(20000); System.out.println(”利率為“+c1.get()); c1.qu(10000);} }(5)public class Student { String name;int age;float average,chainese;float math,Enghish;public Student(String name,int age){this.name=name;this.age=age;System.out.println(name+”:“+age+”歲“+” “);} public void set(float chinese,float math,float Enghish){average=(chinese+math+Enghish)/3;} public float get(){return average;} } class Undergraduate extends Student// Student繼承Undergraduate {public Undergraduate(String name,int age){ super(name,age);} public void hege(float average){ this.average=average; if(average>=60)System.out.println(”本科生成績(jī)合格“);else System.out.println(”本科生成績(jī)不合格“);}} class Graduate extends Student// Student繼承Graduate {public Graduate(String name,int age){ super(name,age);//調(diào)用 } public void hege(float average){ this.average=average;if(average>=80)System.out.println(”研究生生成績(jī)合格“);else System.out.println(”研究生成績(jī)不合格“);} } public class StudentDemo {//測(cè)試 public static void main(String[] args){ Undergraduate c1=new Undergraduate(”小明 “,22); System.out.println(”本科生三門(mén)成績(jī)分別為:“+”59,“+”85,“+”90“); c1.set(65,75,60); System.out.println(”本科生平均分=“+c1.get()); c1.hege(c1.get()); System.out.println(); Graduate c2=new Graduate(”小紅 “,18); System.out.println(”研究生生三門(mén)成績(jī)分別為“+”90,“+”84,“+”88“); c2.set(80,86,79); System.out.println(”研究生生平均分=“+c2.get()); c2.hege(c2.get());}(6)public abstract class ClosedFigure {//定義抽象類(lèi) String shape;public ClosedFigure(String newShape){this.shape=newShape;} public abstract double perimeter();//定義抽象類(lèi),里面不能寫(xiě)方法 public abstract double area();} public class Triangle extends ClosedFigure {// ClosedFigure繼承Triangle double a,b,c;public Triangle(String newShape,double a,double b,double c){super(”newShape“);this.a=a;this.b=b; this.c=c;} public double perimeter(){return a+b+c;} public double area(){double s;s=(a+b+c)/2;return Math.sqrt(s*(s-a)*(s-b)*(s-c));} public String toString(){return(”三角形三邊長(zhǎng):“+a+” “+b+” “+c+” “+”周長(zhǎng):“+perimeter()+”面積:“+area());} public class Test { public static void main(String[] args){ Triangle c1=new Triangle(”三角形“,3,4,5); c1.perimeter(); c1.area(); System.out.println(c1.toString());} } }(7)public abstract class Employee { private String firstName;private String lastName;public Employee(String first,String last) {firstName=first; lastName=last;} public String getEmployeeName() {return firstName;} public String getLastName() { return lastName;} public String toString() {return firstName+lastName;} public abstract String earnings();} public final class Boss extends Employee{ double salary;public Boss(String first, String last, double salary){ super(first, last); this.salary = salary;} public String earnings(){return(salary+”“);} } public final class CommissionWorker extends Employee { double salary; double sale;double price;public CommissionWorker(String first, String last, double salary, double sale,double price){ super(first, last); this.salary = salary; this.sale = sale; this.price = price;} public String earnings(){return(salary+sale*price+”“);} } public final class PieceWorker extends Employee{ double number; double price; public PieceWorker(String first, String last, double number,double price){ super(first, last); this.number = number; this.price=price; } public String earnings() {return(number*price+”“);} } public final class HourlyWorker extends Employee {double time;double money;public HourlyWorker(String first, String last, double time, double money){ super(first, last);this.time = time;this.money = money;} public String earnings(){ return(time*money+”“);} } public class Test { public static void main(String[] args){ Employee c1=new Boss(”張“,”三“,10000); System.out.println(”張三月工資:“+c1.earnings()); Employee c2=new CommissionWorker(”李“,”四“,4000,1500,2); System.out.println(”李四月工資:“+c2.earnings()); Employee c3=new PieceWorker(”王“,”五“,1000,3); System.out.println(”王五月工資:“+c3.earnings()); Employee c4=new HourlyWorker(”劉“,”三“,600,30); System.out.println(”劉三月工資:“+c4.earnings());} }(8)public class Circle { String shape;double r;double height;double pi;public Circle(String shape,double r,double height,double pi){this.shape=shape;this.height=height;this.r=r;this.pi=pi;} } public interface Volume { public abstract double area();public abstract double NewVolume();} public class Yuanzhu extends Circle implements Volume { public Yuanzhu(String shape, double r, double height, double pi){ super(shape, r, height, pi);} public double area(){ return pi*r*r;} public double NewVolume(){return area()*height;} } public class Yuanzhui extends Yuanzhu implements Volume { public Yuanzhui(String shape, double r, double height, double pi){ super(shape, r, height, pi); // TODO Auto-generated constructor stub } double s;public double area(){s=Math.sqrt(height*height+r*r);return pi*r*s+pi*r*r;} public double NewVolum(){return 1.0/3*pi*r*pi*r*height;} } public class Test { public static void main(String[] args){ Yuanzhu c1=new Yuanzhu(”圓柱“,4,6,3.14); Yuanzhui c2=new Yuanzhui(”圓錐“,2,3,3.14); System.out.println(”圓柱表面積:“+c1.area()); System.out.println(”圓柱體積:“+c1.NewVolume()); System.out.println(”圓錐表面積:“+c2.area()); System.out.println(”圓錐體積:“+c2.NewVolume());} }(9)public interface CanFly {//定義接口CanFly public void fly();} public class Plane implements CanFly{//使用接口 @Override public void fly(){ // TODO Auto-generated method stub System.out.println(”飛機(jī)借助螺旋槳飛上天空“);} } public class Bird implements CanFly{ @Override public void fly(){ // TODO Auto-generated method stub System.out.println(”小鳥(niǎo) 借助翅膀飛上天空“);} } public class Test { static void makeFly(CanFly obj){ obj.fly();} public static void main(String[] args){ // TODO Auto-generated method stub CanFly p =new Plane(); makeFly(p); CanFly b =new Bird(); makeFly(b);} }(10)import java.io.*;public class Ex1 { public static void main(String args[ ]){ try{ BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));//建立輸入流緩沖區(qū) System.out.print(”請(qǐng)輸入除數(shù):“);String cl=strin.readLine();//鍵盤(pán)輸入 int a=Integer.parseInt(cl);System.out.print(”請(qǐng)輸入被除數(shù):“);cl=strin.readLine();int b=Integer.parseInt(cl);int c=b/a;System.out.println(”商為:“+c);} //捕獲與I/O有關(guān)的異常(空白處補(bǔ)全捕獲語(yǔ)句) catch(IOException e){System.out.println(”輸入輸出異常“);} //捕獲數(shù)值轉(zhuǎn)化時(shí)的異常,如不能將字符轉(zhuǎn)化成數(shù)值 catch(NumberFormatException e){System.out.println(”數(shù)值格式異常,重新輸入“); } //捕獲除數(shù)為0的異常 catch(ArithmeticException e){System.out.println(”除數(shù)為0,重新輸入“);} } }(11)(1)MyException類(lèi): package exp2_11;public class MyException extends Exception{ MyException(String msg){ super(msg);} }(2)Div主類(lèi): package exp2_11;import java.io.*;public class Div { public static void main(String args[])throws MyException{ try{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print(”請(qǐng)輸入實(shí)數(shù)除法運(yùn)算的被除數(shù):“); String str = in.readLine(); double a = Double.parseDouble(str); System.out.print(”請(qǐng)輸入除數(shù):“); str = in.readLine(); double b = Double.parseDouble(str); System.out.println(”商結(jié)果:“+division(a,b)); } catch(ArithmeticException e1){ System.out.println(”商結(jié)果:Infinity“+e1); System.out.println(”商結(jié)果:NaN“+e1); } catch(NumberFormatException e2){ System.out.println(”異常:字符串不能轉(zhuǎn)換成整數(shù)!“+e2); } catch(IOException e3){ System.out.println(”異常:IO異常“+e3); } finally{ System.out.println(”程序結(jié)束!“); } } static double division(double a,double b)throws MyException{ if(a==100 &&(b==4 || b==13)) throw new MyException(”不符規(guī)范“); else return(a/b);} } 四、實(shí)驗(yàn)結(jié)果與分析(程序運(yùn)行結(jié)果及其分析) (1) 去掉Man類(lèi)的public修飾,程序運(yùn)行不出來(lái),提示缺少M(fèi)an的公開(kāi)方法。(2) (3) (4) (5) (6) (7) (8) (9) (10) (11) 五、實(shí)驗(yàn)體會(huì)(遇到問(wèn)題及解決辦法,編程后的心得體會(huì)) 學(xué)習(xí)程序設(shè)計(jì)的基本目的就是培養(yǎng)描述實(shí)際問(wèn)題的程序化解決方案的關(guān)鍵技能Java面向?qū)ο蟪绦蛟O(shè)計(jì)是一門(mén)實(shí)踐性比較強(qiáng)的課程在實(shí)際中我們必須把理論和實(shí)踐結(jié)合起來(lái)。在實(shí)驗(yàn)中我們對(duì)照課本的知識(shí)然后進(jìn)行實(shí)際的操作而后發(fā)現(xiàn)實(shí)際的運(yùn)用比課本提到的要多很多理論總是來(lái)源于實(shí)踐我們必須在現(xiàn)有的理論的基礎(chǔ)上進(jìn)行有效地實(shí)踐。而這次實(shí)驗(yàn)也讓我看到了現(xiàn)在學(xué)習(xí)的一個(gè)很大弱點(diǎn)就是實(shí)踐的實(shí)踐往往很少。在現(xiàn)實(shí)社會(huì)中我們必須懂得實(shí)際的操作才能更好的服務(wù)于社會(huì)。所以我必須在以后的學(xué)習(xí)中多動(dòng)手多實(shí)際操作爭(zhēng)取能在實(shí)踐中找到屬于自己新的感悟,終于在學(xué)習(xí)Java時(shí)達(dá)到了事半功倍的效果。 實(shí)驗(yàn)項(xiàng)目名稱(chēng): 圖形用戶(hù)界面 實(shí)驗(yàn)學(xué)時(shí): 6 同組學(xué)生姓名: ———— 實(shí)驗(yàn)地點(diǎn): 工科樓A101 實(shí)驗(yàn)日期: 17.5.9~17.5.23 實(shí)驗(yàn)成績(jī): 批改教師: 劉晶 批改時(shí)間: 實(shí)驗(yàn)3 圖形用戶(hù)界面 一、實(shí)驗(yàn)?zāi)康暮鸵?/p> (1)掌握Swing組件的使用方法; (2)熟練掌握Swing中常用布局管理器的使用方法;(3)掌握用戶(hù)界面動(dòng)作與事件的處理程序的編寫(xiě)方法;(4)熟練掌握構(gòu)造用戶(hù)界面的方法和常見(jiàn)界面元素的使用;(5)熟練掌握J(rèn)ava繪圖的主要方法。 (6)調(diào)試程序要記錄調(diào)試過(guò)程中出現(xiàn)的問(wèn)題及解決辦法; (7)編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄,不斷積累編程及調(diào)試經(jīng)驗(yàn); (8)做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。 二、實(shí)驗(yàn)儀器和設(shè)備 奔騰以上計(jì)算機(jī),Windows 操作系統(tǒng),裝有JDK1.6和Eclipse4.2軟件。 三、實(shí)驗(yàn)過(guò)程 1.計(jì)算器設(shè)計(jì) 2.整數(shù)進(jìn)制轉(zhuǎn)換 將一個(gè)十進(jìn)制整數(shù)分別轉(zhuǎn)換成二進(jìn)制、八進(jìn)制和十六進(jìn)制整數(shù)。 3.模擬裁判評(píng)分。 設(shè)計(jì)如圖所示圖形界面,顯示n個(gè)裁判的評(píng)分,根據(jù)制定規(guī)則計(jì)算出最后得分。要求:圖形界面采用表格顯示裁判評(píng)分,隨裁判人數(shù)變化而變化;指定分?jǐn)?shù)范圍,若超出,則異常處理; 得分規(guī)則有指定接口約定,由多個(gè)接口對(duì)象給出多種得分規(guī)則,如求平均數(shù)值,或去掉一個(gè)最高分和一個(gè)最低分后,再求平均值。 4.編譯運(yùn)行下例,然后修改程序,當(dāng)使用鼠標(biāo)單擊后在另一位置重新繪制月亮。【例】 在Applet中畫(huà)月亮。import java.awt.*;import java.applet.Applet;public class MoonApplet extends Applet { public void paint(Graphics g)//在Applet上繪圖 { g.setColor(Color.red);g.drawString(”The Moon“,100,20);int x=0,y=0;//圓外切矩形左上角坐標(biāo) x = this.getWidth()/4;y = this.getHeight()/4;int diameter = Math.min(this.getWidth()/2, this.getHeight()/2);//圓的直徑 g.setColor(Color.yellow);g.fillOval(x,y,diameter,diameter);//畫(huà)圓 g.setColor(this.getBackground());//設(shè)置為背景色 g.fillOval(x-20,y-20,diameter,diameter);//畫(huà)圓 } } 5.根據(jù)阿基米德螺線的極坐標(biāo)方程:r=aθ畫(huà)出相應(yīng)圖形。 要求: (1)注意選用適當(dāng)?shù)牟季止芾砥髟O(shè)計(jì)圖形用戶(hù)界面,比較它們的布局情況; (2)養(yǎng)成良好的編程習(xí)慣,嚴(yán)格按照命名規(guī)則為包、類(lèi)及類(lèi)成員命名,將每個(gè)程序打包,包的命名方式如three.num1表示實(shí)驗(yàn)三的第一題;(3)學(xué)會(huì)使用Eclipse的各種調(diào)試方法; (4)學(xué)會(huì)查閱Java API文檔,如查找事件類(lèi)的處理里方法。 程序清單: (建議程序中適當(dāng)添加注釋信息,增強(qiáng)可讀性;較長(zhǎng)程序可分欄書(shū)寫(xiě),保證報(bào)告排版整潔美觀。) 1.import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.*;import javax.swing.*; public class Calulator implements ActionListener { JTextField t1;JPanel p1;JFrame f;static int count=1;static float value=0;int p2=0;String p;public Calulator(){f=new JFrame(”Calulator“);f.setSize(400,200);p1=new JPanel();t1=new JTextField(30);t1.setHorizontalAlignment(JTextField.RIGHT);p1.setLayout(new GridLayout(5,4));f.add(t1);String str[]= {”開(kāi)根“,”+“,”-“,”清零“,”7“,”8“,”9“,”/“,”4“,”5“,”6“,”*“,”1“,”2“,”3“,”負(fù)“,”0“,”.“,”正“,”=“};for(int i=0;i<20;i++){JButton b=new JButton(str[i]);p1.add(b);b.addActionListener(this);} f.add(t1,BorderLayout.CENTER);f.add(p1,BorderLayout.SOUTH);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);} public void actionPerformed(ActionEvent e){ String c = e.getActionCommand();if(c==”0“||c==”1“||c==”2“||c==”3“||c==”4“||c==”5“||c==”6“||c==”7“||c==”8“||c==”9“||c==”.“){if(p2==0){t1.setText(c);p2++;} else {t1.setText(t1.getText()+c);p2++;}} else if(p==”清零“){value=0;t1.setText(String.valueOf(value));} else {count++;p2=0;if(count==2){p=c;value=Float.parseFloat(t1.getText());} if(c==”=“){ if(p==”開(kāi)根“){value=(float)Math.sqrt(Float.parseFloat(t1.getText()));t1.setText(String.valueOf(value));count-=2;} else if(p==”+“){value+=Float.parseFloat(t1.getText());count-=2;} else if(p==”-“){ value-=Float.parseFloat(t1.getText());count-=2;} else if(p==”*“){value*=Float.parseFloat(t1.getText());count-=2;} else if(p==”/“){ value/=Float.parseFloat(t1.getText());count-=2;} else if(p==”正”){value=Math.abs(Float.parseFloat(t1.getText()));t1.setText(String.valueOf(value));count-=2;} else if(p==”負(fù)“){value=-1*Float.parseFloat(t1.getText());t1.setText(String.valueOf(value));count-=2;} t1.setText(String.valueOf(value));value=0;}} } public static void main(String[] args){ new Calulator();}} 2.import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ZhuanH extends JFrame implements ActionListener{ TextField t1,t2,t3,t4;public ZhuanH(){super(”十進(jìn)制整數(shù)轉(zhuǎn)換“);this.setBackground(Color.BLUE);t1=new TextField(5);t2=new TextField(5);t3=new TextField(5);t4=new TextField(5);t1.setText(null);this.setSize(400,200);this.setLayout(new GridLayout(4,2));this.add(new Label(”十進(jìn)制“));this.add(t1);t1.addActionListener(this);this.add(new Label(”二進(jìn)制“));this.add(t2);this.add(new Label(”八進(jìn)制“));this.add(t3);this.add(new Label(”十六進(jìn)制“));this.add(t4);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);} public void actionPerformed(ActionEvent e){ String c = t1.getText();t2.setText(Integer.toBinaryString(Integer.parseInt(c)));t3.setText(Integer.toOctalString(Integer.parseInt(c))); t4.setText(Integer.toHexString(Integer.parseInt(c)));} public static void main(String[] args){ new ZhuanH();}} 3.import java.awt.*;import java.awt.event.*;import javax.swing.*;public class PingFen extends JFrame implements ActionListener{ JPanel p,p1;JTextField t;JTextField t1[]=new JTextField[10];JButton b;float s=0,k;int t2,t3;public PingFen(){ super(”模擬裁判評(píng)分“);this.setSize(300,120);p1=new JPanel(new GridLayout(2,5));this.add(p1,”North“);p=new JPanel(new FlowLayout(FlowLayout.RIGHT));this.add(p,”South“);b=new JButton(”平均分“);t=new JTextField(10);p.add(b);p.add(t);for(int i=0;i<10;i++){ t1[i]=new JTextField(6);t1[i].setText(null);p1.add(t1[i]);} b.addActionListener(this);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);} public void actionPerformed(ActionEvent e){float max,min;for(int i=0;i<10;i++){ try{ if(Float.parseFloat(t1[i].getText())>10.0)throw new Exception(); else continue;} catch(Exception ev){JOptionPane.showMessageDialog(this,t1[i].getText()+”超出范圍“+”,“+” 請(qǐng)重新輸入“);t1[i].setText(null);} } max=Float.parseFloat(String.valueOf(t1[0].getText()));min=Float.parseFloat(String.valueOf(t1[0].getText()));for(int i=1;i<10;i++){ if((k=Float.parseFloat(String.valueOf(t1[i].getText())))>max){max=k;t2=i;} else if((k=Float.parseFloat(String.valueOf(t1[i].getText()))) new PingFen();}} 4.import java.awt.*;import java.applet.Applet;import java.awt.event.*;public class MoonApplet extends Applet implements MouseListener { int x,y;public void init(){ x=this.getWidth()/4; y=this.getHeight()/4; addMouseListener(this);} public void paint(Graphics g)//在Applet上繪圖 { g.setColor(Color.red);g.drawString(”The Moon“,100,20);int diameter = Math.min(this.getWidth()/2, this.getHeight()/2);//圓的直徑 g.setColor(Color.yellow);g.fillOval(x,y,diameter,diameter);//畫(huà)圓 g.setColor(this.getBackground());//設(shè)置為背景色 g.fillOval(x-20,y-20,diameter,diameter);//畫(huà)圓 } public void mouseClicked(MouseEvent e){ x=e.getX();y=e.getY();repaint();} 5.package package2;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class LuoXuan extends JFrame implements ActionListener{ private static final long serialVersionUID = 1L;private LuoXuanCanvas canvas;//自定義畫(huà)布組件 public LuoXuan(){super(”阿基米德螺線“);Dimension dim=getToolkit().getScreenSize();this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2);JPanel p=new JPanel();this.add(p,”North“);JButton b=new JButton(”選擇顏色“);p.add(b);b.addActionListener(this);this.canvas=new LuoXuanCanvas();this.getContentPane().add(this.canvas, ”Center“);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);} public void actionPerformed(ActionEvent e){Color c=JColorChooser.showDialog(this, ”選擇顏色“, Color.BLUE);this.canvas.setColor(c);this.canvas.repaint();} public static void main(String[] args){ new LuoXuan();} } class LuoXuanCanvas extends Canvas {private Color color;public void LuoXuan(Color color){this.setColor(color);} public void setColor(Color color){this.color=color;} public void paint(Graphics g){int x0=this.getWidth()/2;int y0 = this.getHeight()/2;g.setColor(this.color);g.drawLine(x0, 0, x0, y0*2);g.drawLine(0, y0, x0*2, y0); for(int i=0;i<4000;i++){double angle=i*Math.PI/512;double radius=angle*0.5;int x=(int)Math.round(radius*angle*Math.cos(angle));int y=(int)Math.round(radius*angle*Math.sin(angle));g.drawOval(x0+x, y0+y, 1, 1);} }} 四、實(shí)驗(yàn)結(jié)果與分析(程序運(yùn)行結(jié)果及其分析) (分析每題采用的布局管理器、事件處理類(lèi)和主要功能實(shí)現(xiàn)方法)1.2.3.4.38 5.五、實(shí)驗(yàn)體會(huì)(遇到問(wèn)題及解決辦法,編程后的心得體會(huì)) 這次實(shí)驗(yàn)主要是對(duì)圖形用戶(hù)界面的設(shè)計(jì),這里有鼠標(biāo)觸發(fā)的事件,就要讓類(lèi)實(shí)現(xiàn)MouseListener,在類(lèi)里面實(shí)現(xiàn)MouseListener的方法,這里是選擇單擊時(shí)(mouseClicked()),這個(gè)方法,在方法體內(nèi)用getX(),getY()方法來(lái)獲取當(dāng)前坐標(biāo)。 實(shí)驗(yàn)項(xiàng)目名稱(chēng):Java高級(jí)編程 實(shí)驗(yàn)學(xué)時(shí): 4 同組學(xué)生姓名: ———— 實(shí)驗(yàn)地點(diǎn): 工科樓A101 實(shí)驗(yàn)日期: 17.5.30~17.6.6 實(shí)驗(yàn)成績(jī): 批改教師: 劉晶 批改時(shí)間: 實(shí)驗(yàn)4 Java高級(jí)編程 一、實(shí)驗(yàn)?zāi)康暮鸵?/p> (1)了解文件的概念和文件對(duì)象的創(chuàng)建方法;(2)掌握使用文件輸入輸出流讀寫(xiě)文件的方法;(3)了解線程的基本概念和多線程程序設(shè)計(jì)的基本方法;(4)掌握數(shù)據(jù)庫(kù)連接的方法; (5)創(chuàng)建SQL查詢(xún)并更新數(shù)據(jù)庫(kù)中的信息; (6)調(diào)試程序要記錄調(diào)試過(guò)程中出現(xiàn)的問(wèn)題及解決辦法; (7)編寫(xiě)程序要規(guī)范、正確,上機(jī)調(diào)試過(guò)程和結(jié)果要有記錄,不斷積累編程及調(diào)試經(jīng)驗(yàn); (8)做完實(shí)驗(yàn)后給出本實(shí)驗(yàn)的實(shí)驗(yàn)報(bào)告。 二、實(shí)驗(yàn)儀器和設(shè)備 奔騰以上計(jì)算機(jī),Windows 操作系統(tǒng),裝有JDK1.6和Eclipse4.2軟件,MySQL數(shù)據(jù)庫(kù)。 三、實(shí)驗(yàn)過(guò)程 (1)使用文件字節(jié)輸入/輸出流,合并兩個(gè)指定文件;當(dāng)文件中的數(shù)據(jù)已排序時(shí),合并后的數(shù)據(jù)也要求是已排序的。 (2)將Java的關(guān)鍵字保存在一個(gè)文本文件中,判斷一個(gè)字符串是否為Java的關(guān)鍵字。(3)編寫(xiě)在構(gòu)造方法中產(chǎn)生一個(gè)1-5之間的隨機(jī)數(shù)的繼承Thread類(lèi)的線程類(lèi)DelayPrintThread,使得線程體每休眠此隨機(jī)數(shù)時(shí)間就打印輸出線程號(hào)和休眠時(shí)間;另外編寫(xiě)應(yīng)用DelayPrintThread類(lèi)的Java應(yīng)用程序TwoThread.java,在main()方法中創(chuàng)建兩個(gè)線程,并應(yīng)用sleep()控制主應(yīng)用程序延遲一段時(shí)間。 (4)編寫(xiě)繼承Runnable接口的Applet多線程小程序類(lèi)MultiThreadApplet,編寫(xiě)繼承該類(lèi)的Applet小程序類(lèi)Clock,在Clock中重新構(gòu)造父類(lèi)的run()方法,實(shí)現(xiàn)數(shù)字時(shí)鐘的功能,要求不斷刷新顯示時(shí)、分、秒。 (5)為學(xué)生信息表stuinfo設(shè)計(jì)數(shù)據(jù)庫(kù)應(yīng)用程序,包括數(shù)據(jù)的輸入、刪除和查詢(xún)功能。 要求: (1)注意選用適當(dāng)?shù)奈募鬟M(jìn)行文件讀寫(xiě); (2)學(xué)會(huì)兩種創(chuàng)建線程的方法,并比較使用場(chǎng)合; (3)養(yǎng)成良好的編程習(xí)慣,嚴(yán)格按照命名規(guī)則為包、類(lèi)及類(lèi)成員命名,將每個(gè)程序打包,包的命名方式如four.num1表示實(shí)驗(yàn)四的第一題;(4)學(xué)會(huì)查閱Java API文檔,如查找常用工具類(lèi)。 程序清單: (建議程序中適當(dāng)添加注釋信息,增強(qiáng)可讀性;較長(zhǎng)程序可分欄書(shū)寫(xiě),保證報(bào)告排版整潔美觀。)1.import java.io.*;import java.util.Arrays;public class File { private String Filename;static int s=0,t=0;public File(String Filename){this.Filename=Filename;} public void writeToFile(byte[] buffer)throws IOException {if(s==0){FileOutputStream fout = new FileOutputStream(this.Filename);s++;Arrays.sort(buffer);fout.write(buffer);this.readFromFile();fout.close();} else { FileOutputStream fout1 = new FileOutputStream(this.Filename,true);Arrays.sort(buffer);fout1.write(buffer);fout1.close();} } public void readFromFile()throws IOException { FileInputStream fin = new FileInputStream(this.Filename);if(t==0){System.out.println(”文件名“+”:“+this.Filename+”:“);t++;} else System.out.println(”合并兩個(gè)文件后“+”:“+this.Filename+”:“);byte[] buffer = new byte[512];int count = fin.read(buffer);while(count!=-1){ for(int i=0;i byte[] buffer = {0,1,4,3,2,5,6,9,8,7};byte[] buffer1 = {10,11,12,14,15,17,16,19,20,13,18};File afile = new File(”ByteFile.dat“);afile.writeToFile(buffer);afile.writeToFile(buffer1);afile.readFromFile();} } 2.import java.io.*;public class File { private String Filename;public File(String Filename){this.Filename=Filename;} public void writerLine(String[] s)throws IOException {FileWriter f = new FileWriter(this.Filename);System.out.println(this.Filename+”文件中的java關(guān)鍵字有“+”:“);for(int i=0;i public void Text(String[] s,String[] s1){int i,j;for(i=0;i if(s1[i]==s[j]) {System.out.println(s1[i]+”是java中的關(guān)鍵字“);break;} else continue;if(j==s.length)System.out.println(s1[i]+”不是java中的關(guān)鍵字“);} } public static void main(String[] args)throws IOException { String[]s={”public“,”class“,”static“,”void“,”String“,”print“,”byte“,”boolean“,”int“,”short“,”long“,”throw“,”cath“,”continue“,”private“,”abstract“,”Interface“,”Exception“};String[] s1={”pblic“,”class“,”string“};File a=new File(”myfile.dat“);a.writerLine(s);a.Text(s,s1);} } 3.public class DelayPrintThread extends Thread{ private int number,time;public DelayPrintThread(int number) {this.number=number; time=(int)(Math.random()*5);} public void run(){ try { Thread.sleep(this.time);} catch(InterruptedException e){} System.out.println(”線程“+this.number+”:“+”休眠時(shí)間“+this.time);} public class TwoThread { public static void main(String[] args){ DelayPrintThread a=new DelayPrintThread(1);DelayPrintThread b=new DelayPrintThread(2);a.start();b.start();} } 4.import java.applet.Applet;import java.awt.*;import java.util.Date;import java.text.SimpleDateFormat;public class MultiThreadApplet extends Applet implements Runnable { Thread a;public void start(){ if(a==null){a=new Thread(this); a.start();} } public void stop(){ if(a!=null){ a.stop(); a=null;} } public void run(){} public void paint(Graphics g){SimpleDateFormat sdf=new SimpleDateFormat(”yyyy年MM月dd日E a hh時(shí) mm分ss秒“);g.drawString(sdf.format(new Date()),50,100);} } public class Clock extends MultiThreadApplet{ public void run(){ repaint();try{a.sleep(1000);} catch(InterruptedException e){} }} 5.import java.sql.*;public class Statement{ public static void main(String args[])throws Exception { Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver“);Connection conn=DriverManager.getConnection(”jdbc:odbc:student“);Statement stmt=conn.createStatement();String Sql=”SELECT stu_id,stu_name,city FROM student WHERE stu_id=5“;String Sql=”INSERT INTO student(stu_id,stu_name,city)VALUES('5','小紅','徐州')“;String Sql=”DELETE FROM student WHERE stu_id=5“;int count=stmt.executeUpdate(Sql);System.out.println(”插入“+count+”行“);ResultSet rs=stmt.executeQuery(Sql);if(rs.next()){System.out.println(”行“+rs.getRow()+”:“+rs.getString(1)+”,“+rs.getString(2)+”,“+rs.getString(3));} else System.out.println(”沒(méi)有數(shù)據(jù)“);count=stmt.executeUpdate(Sql);rs=stmt.executeQuery(Sql);if(rs.next()){System.out.println(”行“+rs.getRow()+”:“+rs.getString(1)+”,“+rs.getString(2)+”,“+rs.getString(5));} else System.out.println(”沒(méi)有數(shù)據(jù)");} } 四、實(shí)驗(yàn)結(jié)果與分析(程序運(yùn)行結(jié)果及其分析)1.45 2.3.4.五、實(shí)驗(yàn)體會(huì)(遇到問(wèn)題及解決辦法,編程后的心得體會(huì)) 通過(guò)這次實(shí)驗(yàn),我基本能夠理解線程的運(yùn)行了,學(xué)會(huì)調(diào)用Thread類(lèi)中的系統(tǒng)函數(shù)以及掌握這些函數(shù)的作用是難點(diǎn),start()是開(kāi)辟一條新線程。子類(lèi)重寫(xiě)父類(lèi)的run()方法,里面用sleep,來(lái)控制每個(gè)線程的休眠時(shí)間,但是得注意這里應(yīng)該要用try-catch語(yǔ)句來(lái)捕獲中斷異常。第四篇:java上機(jī)實(shí)驗(yàn)
第五篇:JAVA實(shí)驗(yàn)報(bào)告