




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、精選優(yōu)質文檔-傾情為你奉上精選優(yōu)質文檔-傾情為你奉上專心-專注-專業(yè)專心-專注-專業(yè)精選優(yōu)質文檔-傾情為你奉上專心-專注-專業(yè)Java大作業(yè)實踐報告Animal公司的分析與設計 廣東藥學院一Animal類的設計要求:根據實際需求設計Animal類,并完成以下功能:輸出全部信息對2個實例進行比較使用static對共同的屬性進行修飾實現從鍵盤輸入的功能實現單態(tài)設計模式加異常信息加入多線程使用集合(可選)二設計場景,實現其要求場景設計在一間公司,擁有很多的小貓小狗等寵物可以出賣。輸出9只動物的全部信息,舉例代表全部動物的信息,并用List集合存放所有信息。可以選擇輸入動物的信息來增加動物的數量,其中
2、輸入錯誤不能添加,并且進行了異常處理??梢詫游锏膶傩赃M行比較,例如年齡,名字等。用到了方法的重載。公司的動物的總數為共同屬性和來源,用static修飾。公司實現單態(tài)模式設計,所有動物都是屬于這間公司的。可以從鍵盤輸入許多不同的選擇,選擇對動物進行不同的操作,并且所有的輸入都進行了異常的處理。采用多線程售賣動物,實現三個窗口同時售賣,并且進行了同步設計,保證安全。利用ArrayList集合收集動物的年齡信息,并對其進行分析。三.接口與類的基本設計接口Animal(包涵四個抽象方法):String print();/姓名+年齡String talk();/姓名+他的聲音String all();
3、/全部信息void input();/輸入數據抽象父類A:實現接口Animal1.私有屬性private String name;private int age;private String voice;private String beizhu;/備注共同屬性static String house=KFC;static int num=200;2.構造方法3.抽象方法public abstract String print();4. 重寫接口的部分抽象方法5.對于封裝的屬性對外提供函數Dog類:繼承A類private String spec;/這個對象含有自己獨特屬性構造方法重寫父類的方法:
4、public String talk()重寫父類的抽象方法:public String print()Cat類:繼承A類1.構造方法2.重寫父類的抽象方法:public String print()MyTh類(實現 Runnable):進行多線程售賣動物Company類:單態(tài)設計模式T類:測試功能(主函數)四其他函數功能塊1. public static void anlyage(ArrayList age) /對年齡進行分析2. public static void randonsale(List name,Dog d)/隨即賣掉X只動物3. public static void duosa
5、le(int num) /開創(chuàng)3個窗口,多線程同步售賣4. public static void campare(String a,String b) /兩個實例的對比 public static void campare(int a,int b)/ 方法的重載五全部代碼塊import java.util.*;/.總接口.interface Animal/總接口String print();/姓名+年齡String talk();/姓名+他的聲音String all();/全部信息void input();/輸入數據/.動物的父類. abstract class A implements An
6、imal/動物的父類 private String name;private int age;private String voice;private String beizhu;/備注static String house=KFC;static int num=200;/父類的構造方法public A()public A(String name)setname(name);public A(String name,int age)setname(name);setage(age);public A(String name,int age,String voice,String beizhu)
7、setname(name);setage(age);setvoice(voice);setbeizhu(beizhu);/重寫抽象方法public abstract String print();/留給子類喜歡獨自擴充的抽象方法public String talk()return getname()+ +getvoice();public String all() return getname()+ +getage()+ +getvoice()+ +getbeizhu()+ +this.house;public void input()/輸入數據Scanner input1=new Scann
8、er(System.in);try /=input1.next();setname(input1.next();setage(input1.nextInt();setvoice(input1.next();setbeizhu(input1.next();A.num+;catch( Exception e)System.out.println(e);setage();System.out.println(輸入有誤);/A.num-;/封裝void setname(String name) = name;String getname()return name;v
9、oid setage(int age)this.age=-;if(age=0)this.age= age;elseA.num-;trythrow new IllegalArgumentException(年齡輸入有誤,不能為負數);/在方法內部拋出異常IllegalArgumentExceptioncatch(IllegalArgumentException e)System.out.println(e.getMessage();int getage()return age;void setvoice(String voice) this.voice=voice;String getvoice
10、()return voice;void setbeizhu(String beizhu) this.beizhu=beizhu;String getbeizhu()return beizhu;/.單態(tài)設計模式.class Company/單態(tài)設計模式static String cname;private Company(String cname)ame=cname; /單態(tài)設計模式核心static Company p=new Company(CKFC);static public Company getC() return p; ;/.大狗類.class Dog extends A/繼承pri
11、vate String spec;/這個對象含有自己獨特屬性void setspec(String spec) this.spec=spec;String getspec()return spec;Company p1=Company.getC();/小狗的構造方法public Dog()public Dog(String name)super(name);public Dog(String name,int age)super(name,age);public Dog(String name,int age,String voice,String beizhu)super(name,age,
12、voice,beizhu);public Dog(String name,int age,String voice,String beizhu,String spec)super(name,age,voice,beizhu);setspec(spec);public String talk()/子類“重寫”父類的方法 return getname()+:+getvoice()+:+getspec()+ +ame;/重寫抽象方法public String print() return getname()+ +getage();/.貓咪類.class Cat extends Apublic Cat
13、()public Cat(String name)super(name);public Cat(String name,int age)super(name,age);public Cat(String name,int age,String voice,String beizhu)super(name,age,voice,beizhu);/重寫抽象方法public String print() return getname()+ +getbeizhu();/.多線程.class MyTh implements Runnableint num;MyTh();MyTh(int num)this.
14、num=num;public void run()while (num0)sale();System.out.println(sale stop!);synchronized void sale()/同步方法 if ( num0) tryThread.sleep(0,1);catch(Exception e) System.out.println(sale :+Thread.currentThread().getName()+ :+ num-); ;class T/.主函數.public static void main(String args) System.out.println(輸出動物
15、的部分或全部信息);System.out.println(姓名 +年齡 +聲音 +備注 +公司 );Dog d1=new Dog(dwangcai,6,wanwan,nothing, i am a good dog!);Dog d2=new Dog(dxiaohei,9,wanwan,weakness);Dog d3=new Dog(dxiaocai,3,wanwan,young);Dog d4=new Dog(dming,3,wanwan,young);Cat c1=new Cat(cmiao,5,miaomiao,nothing);Cat c2=new Cat(cxiaomi,1,miao
16、miao,nothing);Cat c3=new Cat(capple,3,miaomiao,nothing);Cat c4=new Cat(cwindy,4,miaomiao,nothing);Cat c5=new Cat(cmm,2,miaomiao,nothing);/信息數組List name=new ArrayList();name.add(d1.all();name.add(d2.all();name.add(d3.all();name.add(d4.all();name.add(c1.all(); name.add(c2.all(); name.add(c3.all(); nam
17、e.add(c4.all(); name.add(c5.all(); for(int i=0;iname.size();i+)System.out.println(name.get(i);/測試輸入對象System.out.println(n增加動物數量);int flag=0;System.out.print(輸入0拒絕添加,輸入1增加:);Scanner input1=new Scanner(System.in);tryflag=input1.nextInt();catch( Exception e)System.out.println(e);System.out.println(輸入有誤
18、);int j=flag;if(j=1)Cat c10=new Cat();c10.input();System.out.println(c10.all();else System.out.println(No Need!);/年齡數組ArrayList age=new ArrayList();age.add(d1.getage();age.add(d2.getage();age.add(d3.getage();age.add(d4.getage();age.add(c1.getage(); age.add(c2.getage(); age.add(c3.getage(); age.add(c
19、4.getage(); age.add(c5.getage(); /對年齡進行分析anlyage( age);/隨即賣掉X只動物randonsale(name,d1); /多線程duosale(d1.num);/.函數功能塊./方法的重載public static void campare(String a,String b)System.out.println(a.equals(b);public static void campare(int a,int b)System.out.println(較大的值:+Math.max(a,b);System.out.println(較小的值:+Ma
20、th.min(a,b);/多線程public static void duosale(int num) System.out.print(多線程需要買所有的動物嗎?按0否認,其他數字確定:);int i=0;Scanner input1=new Scanner(System.in);tryi=input1.nextInt();catch( Exception e)System.out.println(e);System.out.println(輸入有誤);int j=i;if(j!=0)Runnable r=new MyTh(num);new Thread(r).start();new Thr
21、ead(r).start();new Thread(r).start();elseSystem.out.println(over!);/賣多少只動物(隨即抽?。﹑ublic static void randonsale(List name,Dog d)System.out.print(n需要賣多少只動物,請輸入:(數量少于+d.num+):);int num=0;Scanner input1=new Scanner(System.in);trynum=input1.nextInt();catch( Exception e)System.out.println(e);System.out.pri
22、ntln(輸入有誤);d.num=d.num-num;System.out.println(賣+num+只動物:);Collections.shuffle(name);tryfor(int i=0;inum;i+)System.out.println(name.get(i);catch (Exception e)System.out.println(e);System.out.println(最后剩余的數目為:+d.num+nn);/對動物年齡進行分析public static void anlyage(ArrayList age)System.out.println(n對動物年齡進行分析);
23、Collections.sort(age);System.out.println(n對動物年齡從低到高排序結果:);for(int i=0;iage.size();i+)System.out.print(age.get(i)+);System.out.println( n動物最大的是年齡:+Collections.max(age);/最大數值System.out.println(n動物最小的是年齡:+Collections.min(age);/最小數值System.out.println(n綜合分析“);/*int a=(int)Collections.max(age);for(int i=0
24、;i=a;i+)System.out.println(年齡為+i+歲的有:+Collections.frequency(age,i);*/*/增加動物數量public static void addanimal()System.out.println(n增加動物數量);int i=0;System.out.print(請輸入需要添加的數目:);Scanner input1=new Scanner(System.in);tryi=input1.nextInt();catch( Exception e)System.out.println(e);System.out.println(輸入有誤);f
25、or(int j=0;ji;j+)Cat c10=new Cat();c10.input();System.out.println(c10.all()*/*/輸出兩個實例比較System.out.println(輸出兩個實例比較);campare(d1.getname(),c1.getname();campare(d1.getage(),c1.getage();System.out.println();*/*/測試單態(tài)設計模式System.out.println(測試單態(tài)設計模式);Company p1=Company.getC();Company p2=Company.getC();Company p3=Company.getC();System.out.println(ame);System.out.println(p1=p2);System.out.println(p2=p3);ame=xxx;System.out.println(ame);*/*小貓Cat
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 長春大學《第二外語(日、德)(2)》2023-2024學年第一學期期末試卷
- 和田師范專科學?!吨嗅t(yī)護理學基礎Ⅱ實驗》2023-2024學年第一學期期末試卷
- 新鄉(xiāng)學院《農業(yè)資源與環(huán)境專業(yè)英語》2023-2024學年第二學期期末試卷
- 平臺服務合同(2025年版)
- 公司團建活動合同
- 學校門窗維修合同
- 外墻貼磚勞務分包合同
- 工廠物業(yè)管理合同書
- 事業(yè)單位終止合同協議書
- 合同斷橋鋁門窗合同
- 健身會籍顧問
- 電力系統(tǒng)分析知到智慧樹章節(jié)測試課后答案2024年秋東北電力大學
- 2025年中航證券有限公司招聘筆試參考題庫含答案解析
- 2024年中考歷史真題匯編專題13 材料分析題(中國史部分)-教師
- 2025年上半年甘肅省林業(yè)和草原局事業(yè)單位招聘筆試重點基礎提升(共500題)附帶答案詳解
- 化工單元操作知到智慧樹章節(jié)測試課后答案2024年秋煙臺職業(yè)學院
- 談黑色變-認識色素痣與黑素瘤.課件
- 電信運營商網絡安全管理制度
- 魏晉風度課件
- 【MOOC】英國小說-南京大學 中國大學慕課MOOC答案
- 【讀后續(xù)寫】2021年11月稽陽聯考讀后續(xù)寫講評:Saving the Daisies 名師課件-陳星可
評論
0/150
提交評論