




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、1.隨機產生20個50100之間的整數,輸出這20個數并找出最大數及最小數輸出public class Test1 public static void main(String args) int math = new int20;int max = 0;int min = 100;for(int i = 0;i<20;i+)mathi = (int)(Math.random()*50+50);(mathi+" ");();for(int i = 0;i<20;i+)if(max<mathi) max=mathi;if(min>mathi) min=
2、mathi;("max:"+max);("min:"+min); 2.創建一個圖書類,類中包含的屬性有:書名、作者、出版社;包含的方法有:構造方法,設置書籍狀態,查看書籍狀態。書籍狀態有在館和外借兩種。 public class Test2 public static void main(String args)Book book=new Book("Java程序設計","李偉","清華大學出版社");book.setzt(true);book.getzt();class Bookprivate
3、 String Bname;private String 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)("在館");else("外借");3.設計一個Birthday類
4、,其成員變量有:year,month,day;提供構造方法、輸出Birthday對象值的方法和計算年齡的方法。編寫程序測試這個類。public class Test3 public static void main(String args)Birthday b=new Birthday(2010,6,8);b.printBirthDay();(b.printAge(); class Birthdayprivateint year;privateint month;privateint day;public Birthday(intyear,intmonth,int day)this.year=
5、year;this.month=month;this.day=day;public void printBirthDay()(year+"-"+month+"-"+day);publicintprintAge()return 2017-year;4. 編寫一個類,描述汽車,其中用字符型描述車的牌號,用浮點型描述車的價格。編寫一個測試類,其中有一個修改價格的方法,對汽車對象進行操作,根據折扣數修改汽車的價格,最后在main()方法中輸出修改后的汽車信息public class Test4 public static void main(String arg
6、s)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()("這輛車的品牌是"+chePai+"原價是"+price+"打折后為"+price1); 5編寫一個異常類MyExce
7、ption,再編寫一個類Student,該類有一個產生異常的方法speak(int m)。要求參數m的值大于1000時,方法拋出一個MyException對象。最后編寫主類,在主方法中創建Student對象,讓該對象調用speak()方法。classMyException extends Exceptionprivateint m;MyException(int m)this.m=m; public String getMessage()return "出現異常:參數"+this.m+"大于1000" class Studentpublicint spe
8、ak(int m)throws MyExceptionif(m>1000) throw new MyException(m);return m; public class Test5 public static void main(String args) tryStudent s=new Student();("輸出的結果是:"+s.speak(5);("輸出的結果是:"+s.speak(5000);catch(MyException e)(e.getMessage(); 6. 單擊窗體的關閉按鈕時,跳出如下對話框,選擇“是”窗體關閉,選擇“否”
9、,窗體不關閉importjavax.swing.*;importjava.awt.*;.*;class JFrame6JFrame frame=new JFrame();JFrame15()frame.setTitle("關閉窗體時,問一聲");frame.setBounds(100,100,300,200);frame.setVisible(true);frame.addWindowListener(new myWindowListener(); classmyWindowListener extends WindowAdapterpublic void windowCl
10、osing(WindowEvent e)int result=JOptionPane.showConfirmDialog(frame,"你確定要關閉窗體?","確認對話框",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 Text6 pu
11、blic static void main(String args) new JFrame6();7. 創建一個File類的對象,首先判斷此配置文件是否存在,如果不存在,則調用createNewFile方法創建一個文件,然后從鍵盤輸入字符存入數組里,創建文件輸出流,把數組里的字符寫入到文件中,最終保存在“Example7.txt”文件中import java.io.*;public class Test7 public static void main(String args) File file=new File("D:","Example7.txt"
12、);byte b=new byte1000;int n;tryif(!file.exists()file.createNewFile();FileOutputStreamfos=new FileOutputStream(file,true);n=(b);fos.write(b,0,n);fos.close();catch(Exception e)e.getMessage();9. 編寫MyThread線程類,在該類中實現九九乘法表的動態輸出,每隔1秒輸出乘法表中的一個運算結果。public class Test9 public static void main(String args) Thr
13、ead t=new MyThread();t.start();classMyThread extends Threadpublic void run()inti,j;tryfor(i=1;i<=9;i+)for(j=1;j<=i;j+)(j+"*"+i+"="+i*j+"t");sleep(1000);();catch(Exception e)e.toString();9.編寫類OverLoading,在該類中定義3個方法:一個info()方法是沒有參數的,一個info()方法需要使用一個整形參數,一個info()方法需要
14、使用一個String類型參數。在main方法中進行測試。運行結果如下: public class Test9 public static void main(String args) OverLoadingol=new OverLoading();();(5);("HelloWorld"); classOverLoadingpublic void info()("您調用的是無參數的方法");public void info(int n)("您調用的是整形類型參數的方法,參數是:"+n);public void info(String s)("您調用的是String類型參數的方法,參數是:"+s);10. 編寫類Shape,該類是一個抽象類。在該類中定義一個抽象方法:getArea()。編寫類Circle,該類繼承自Shape并實現了其抽象方法getArea()。在該類的構造方法中,獲得了圓形的半徑,以此在getArea()中計算面積。abstract class Shapepublic abstract double getArea();class Circle exten
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 隔熱膜市場前景與發展趨勢深度分析
- 法學概論知識綜述試題及答案
- 2025年計算機二級VB考試難點關注及試題及答案
- 高考數學試題及答案密集復習
- 行政管理復習中個人經驗分享與反思:試題及答案
- 企業核心競爭力與戰略風險管理的相互影響試題及答案
- 法學概論學術研究趨勢試題及答案
- AI+醫藥行業趨勢及市場前景報告分析
- 私有云與公有云的區別與優勢試題及答案
- 新質生產力的六個內涵特征是什么
- 稅務審計理論試題及答案解析
- 智能海洋牧場裝備行業跨境出海戰略研究報告
- 麻醉鎮靜藥與阿片類
- 中考化學第一輪復習 物質的性質與應用(常見的酸堿鹽)測試題(解析版)
- 病理學課件-炎癥的機制
- 2025世界高血壓日控住血壓穩住幸福高血壓健康講座
- 安徽卓越縣中聯盟2024-2025學年高三下學期5月份檢測政治試卷+答案
- 廣東省珠海市2024-2025學年下學期期中八年級數學質量監測試卷(含答案)
- 焊接工程師職業技能考核試題及答案
- 《神經網絡模型》課件
- 中小學教師資格筆試2024年考試真題解析
評論
0/150
提交評論