


下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、1. 隨機(jī)產(chǎn)生 20 個(gè) 50100 之間的整數(shù),輸出這 20 個(gè)數(shù)并找出最大數(shù)及最小數(shù)輸出public class test1 public static void main(string args) int math = new int20;int max = 0;int min = 100; for(int i = 0;i20;i+)mathi = (int)(math.random()*50+50); system.out.print(mathi+ );system.out.println(); for(int i = 0;i20;i+)if(maxmathi) min=mathi;s
2、ystem.out.println(max:+max); system.out.println(min:+min);2. 創(chuàng)建一個(gè)圖書類,類中包含的屬性有:書名、作者、出版社;包含的方法有:構(gòu)造方法,設(shè)置書籍狀態(tài),查看書籍狀態(tài)。書籍狀態(tài)有在館和外借兩種。public class test2 public static void main(string args)book book=new book(java 程序設(shè)計(jì),李偉,清華大學(xué)出版社);book.setzt(true); book.getzt();class bookprivate string bname; private string
3、 aname; private string baddress;book(string bname,stringaname,stringbaddress) this.bname=bname;this.aname=aname; this.baddress=baddress;privatebooleanzt;public void setzt(booleanzt) this.zt=zt;public void getzt() if(zt=true)system.out.println(在館); elsesystem.out.println(外借);3. 設(shè)計(jì)一個(gè) birthday 類,其成員變量有
4、:year,month,day;提供構(gòu)造方法、輸出 birthday 對(duì)象值的方法和計(jì)算年齡的方法。編寫程序測(cè)試這個(gè)類。public class test3 public static void main(string args) birthday b=new birthday(2010,6,8); b.printbirthday(); system.out.println(b.printage();class birthday privateint year; privateint month; privateint day;public birthday(intyear,intmonth,
5、int day) this.year=year;this.month=month; this.day=day;public void printbirthday() system.out.println(year+-+month+-+day);publicintprintage() return 2017-year;4. 編寫一個(gè)類,描述汽車,其中用字符型描述車的牌號(hào),用浮點(diǎn)型描述車的價(jià)格。編寫一個(gè)測(cè)試類,其中有一個(gè)修改價(jià)格的方法,對(duì)汽車對(duì)象進(jìn)行操作,根據(jù)折扣數(shù)修改汽車的價(jià)格,最后在 main()方法中輸出修改后的汽車信息public class test4 public static voi
6、d main(string args)car c=new car(奔馳 s6oo,50000); c.dismessage();class carstring chepai; float price; float price1;car(string chepai,float price) this.chepai=chepai; this.price1=price*4/5; this.price=price;void dismessage()system.out.println(這輛車的品牌是+chepai+原價(jià)是+price+打折后為+price1);5. 編寫一個(gè)異常類 myexceptio
7、n,再編寫一個(gè)類 student,該類有一個(gè)產(chǎn)生異常的方法 speak(int m)。要求參數(shù) m 的值大于 1000 時(shí),方法拋出一個(gè) myexception 對(duì)象。最后編寫主類,在主方法中創(chuàng)建 student 對(duì)象,讓該對(duì)象調(diào)用 speak()方法。classmyexception extends exception privateint m;myexception(int m) this.m=m; public string getmessage()return 出現(xiàn)異常:參數(shù)+this.m+大于 1000; class studentpublicint speak(int m)thro
8、wsmyexception if(m1000) throw new myexception(m); return m; public class test5 publicstatic void main(string args) trystudent s=new student();system.out.println(輸出的結(jié)果是:+s.speak(5); system.out.println(輸出的結(jié)果是:+s.speak(5000);catch(myexception e) system.out.println(e.getmessage(); 6. 單擊窗體的關(guān)閉按鈕時(shí),跳出如下對(duì)話框,
9、選擇“是”窗體關(guān)閉, 選擇“否”,窗體不關(guān)閉importjavax.swing.*; importjava.awt.*; importjava.awt.event.*; class jframe6jframe frame=new jframe(); jframe15()frame.settitle(關(guān)閉窗體時(shí),問一聲); frame.setbounds(100,100,300,200); frame.setvisible(true);frame.addwindowlistener(new mywindowlistener(); classmywindowlistener extends win
10、dowadapterpublic void windowclosing(windowevent e)int result=joptionpane.showconfirmdialog(frame,你確定要關(guān)閉窗體?,確認(rèn)對(duì)話框,joptionpane.yes_no_option);if(result=joptionpane.ok_option) system.exit(0);else if(result=joptionpane.no_option) frame.setdefaultcloseoperation(jframe.do_nothing_on_close);public class te
11、xt6 public static void main(string args) new jframe6();7. 創(chuàng)建一個(gè) file 類的對(duì)象,首先判斷此配置文件是否存在,如果不存在,則調(diào)用 createnewfile 方法創(chuàng)建一個(gè)文件,然后從鍵盤輸入字符存入數(shù)組里,創(chuàng)建文件輸出流,把數(shù)組里的字符寫入到文件中, 最終保存在“example7.txt”文件中import java.io.*; public class test7 public static void main(string args) file file=new file(d:,example7.txt); byte b=ne
12、w byte1000;int n; tryif(!file.exists() file.createnewfile();fileoutputstreamfos=new fileoutputstream(file,true); n=system.in.read(b);fos.write(b,0,n);fos.close(); catch(exception e)e.getmessage();9. 編寫 mythread 線程類,在該類中實(shí)現(xiàn)九九乘法表的動(dòng)態(tài)輸出,每隔 1 秒輸出乘法表中的一個(gè)運(yùn)算結(jié)果。public class test9 public static void main(strin
13、g args) thread t=new mythread(); t.start();classmythread extends thread public void run()inti,j; tryfor(i=1;i=9;i+) for(j=1;j=i;j+)system.out.print(j+*+i+=+i*j+t); sleep(1000);system.out.println(); catch(exception e)e.tostring();9. 編寫類 overloading,在該類中定義 3 個(gè)方法:一個(gè) info()方法是沒有參數(shù)的,一個(gè) info()方法需要使用一個(gè)整形參數(shù)
14、,一個(gè) info()方法需要使用一個(gè) string 類型參數(shù)。在 main 方法中進(jìn)行測(cè)試。運(yùn)行結(jié)果如下:public class test9 public static void main(string args) overloadingol=new overloading(); ();(5); (helloworld);classoverloading public void info()system.out.println(您調(diào)用的是無參數(shù)的方法);public void info(int n)system.out.println(您調(diào)用的是整形
15、類型參數(shù)的方法,參數(shù)是:+n);public void info(string s)system.out.println(您調(diào)用的是string 類型參數(shù)的方法,參數(shù)是:+s);10. 編寫類 shape,該類是一個(gè)抽象類。在該類中定義一個(gè)抽象方法: getarea()。編寫類 circle,該類繼承自 shape 并實(shí)現(xiàn)了其抽象方法 getarea()。在該類的構(gòu)造方法中,獲得了圓形的半徑,以此在 getarea()中計(jì)算面積。abstract class shapepublic abstract double getarea();class circle extends shape pri
16、vate double r; public circle(double r)this.r=r;public double getarea()/return math.pi*math.pow(r,2); return 3.14*r*r;public class test10 public static void main(string args) circle c=new circle(3);system.out.println(圖形的面積是:+c.getarea();“”“”at the end, xiao bian gives you a passage. minand once said,
17、 people who learn to learn are very happy people. in every wonderful life, learning is an eternal theme. as a professional clerical and teaching position, i understand the importance of continuous learning, life is diligent, nothing can be gained, only continuous learning can achieve better self. only by constantly learning and mastering the latest relevant knowledge,
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 培養(yǎng)未來科技人才商業(yè)計(jì)劃
- 倉(cāng)庫消防安全管理制度
- 焊工車間員工管理制度
- 消防安全學(xué)習(xí)總結(jié)
- 企業(yè)安全生產(chǎn)主體責(zé)任的主要內(nèi)容
- 中國(guó)電子商務(wù)軟件行業(yè)發(fā)展運(yùn)行現(xiàn)狀及投資潛力預(yù)測(cè)報(bào)告
- 2025年飛機(jī)空難事故
- 中國(guó)鋁空氣電池市場(chǎng)供需現(xiàn)狀及投資戰(zhàn)略研究報(bào)告
- 特教語前技能訓(xùn)練課件
- 牛繁殖技術(shù)課件
- 2025年中遠(yuǎn)海運(yùn)集團(tuán)招聘筆試備考題庫(帶答案詳解)
- 北京豐臺(tái)區(qū)“青苗培優(yōu)”招聘考試真題2024
- 2025-2030中國(guó)遙控武器站行業(yè)現(xiàn)狀調(diào)研與前景趨勢(shì)預(yù)測(cè)報(bào)告
- 內(nèi)蒙古呼倫貝爾能源投資開發(fā)有限責(zé)任公司招聘筆試真題2024
- 孤獨(dú)癥相關(guān)培訓(xùn)課件
- 2025至2030中國(guó)數(shù)據(jù)中心液冷行業(yè)發(fā)展趨勢(shì)分析與未來投資戰(zhàn)略咨詢研究報(bào)告
- Unit 2 Home Sweet Home 第5課時(shí)(Section B 2a-3c) 2025-2026學(xué)年人教版英語八年級(jí)下冊(cè)
- 2025至2030中國(guó)大型連鎖超市行業(yè)發(fā)展趨勢(shì)分析與未來投資戰(zhàn)略咨詢研究報(bào)告
- 高水平研究型大學(xué)建設(shè)中教育、科技與人才的協(xié)同發(fā)展研究
- 山西省2025年普通高中學(xué)業(yè)水平合格性考試適應(yīng)性測(cè)試化學(xué)試卷(含答案)
- 安全生產(chǎn)法律法規(guī)匯編(2025版)
評(píng)論
0/150
提交評(píng)論