《Java程序設(shè)計(jì)》作業(yè)二_第1頁
《Java程序設(shè)計(jì)》作業(yè)二_第2頁
《Java程序設(shè)計(jì)》作業(yè)二_第3頁
《Java程序設(shè)計(jì)》作業(yè)二_第4頁
《Java程序設(shè)計(jì)》作業(yè)二_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、一、 9.2(1) 題目設(shè)計(jì)一個Stock的類,這個類包括:一個名為symbol的字符串?dāng)?shù)據(jù)域表示股票代碼一個名為name的字符串?dāng)?shù)據(jù)域表示股票名字 一個名為previousClosingPrice的double型數(shù)據(jù)域,它存儲的是前一日的股票值一個名為currentPrice的double型數(shù)據(jù)域,它存儲的是當(dāng)時的股票值。創(chuàng)建一支有特定代碼和名字的股票的構(gòu)造方法。一個名為getChangePercent()的方法返回從previousClosingPrice變化到currentPrice的百分比。實(shí)現(xiàn)這個類,編寫一個測試程序,創(chuàng)建一個Stock對象,它的股票代碼是ORCL股票名字為Oracl

2、e Corporation,前一日收盤價是34.5。設(shè)置新的當(dāng)前值為34.35,然后顯示市值變化的百分比。(2) UML圖(3) 代碼package edu.neu.li.test;public class Stock private String symbol="" private String name; private double previousClosingPrice; private double currentPrice; public Stock() symbol=""name=""previousClosingPr

3、ice=34.5;currentPrice=34.35; public Stock(String newsymble, String newname) symbol=newsymble;name=newname; public String getsymbol()return symbol;public String getname()return name; public double getChangPercent() return currentPrice/previousClosingPrice; package edu.neu.li.test.run;import edu.neu.l

4、i.test.Stock;public class test1 public static void main(String args) Stock s1=new Stock(); Stock s=new Stock("ORCL","Oracle Corporation");System.out.println("The symbol is:"+s.getsymbol();System.out.println("The name is:"+s.getname();System.out.println("T

5、he ChangPercent is:"+s1.getChangPercent();(4) 運(yùn)行結(jié)果The symbol is: ORCLThe name is: Oracle CorporationThe ChangPercent is:0.9956521739二、 9.8(1) 題目設(shè)計(jì)一個名為Fan的類表示風(fēng)扇。這個類包括: 1 三個常量SLOW,MEDIUM和FAST,其值分別為1,2,3,表示風(fēng)扇的速度;2 int類型的數(shù)據(jù)域speed表示風(fēng)扇的速度;默認(rèn)值為SLOW3 boolean型的數(shù)據(jù)域on表示風(fēng)扇是否打開;默認(rèn)值為false4 double型的數(shù)據(jù)域radius表

6、示風(fēng)扇的半徑;默認(rèn)值為55 string型的數(shù)據(jù)域color表示風(fēng)扇的顏色;默認(rèn)值為blue6 無參構(gòu)造方法創(chuàng)建默認(rèn)風(fēng)扇;7 全部四個數(shù)據(jù)域的訪問器和修改器;9 toString()方法返回描述風(fēng)扇的字符串。如果風(fēng)扇打開,該方法用一個組合的字符串返回風(fēng)扇的速度,顏色和半徑;否則,用一個組合的字符串和“fan is off”一起返回風(fēng)扇的顏色和半徑。畫出該類的UML圖并實(shí)現(xiàn)它。編寫一個測試程序,創(chuàng)建兩個Fan對象,將第一個對象設(shè)置為最大速度,半徑為10,顏色為yellow,打開狀態(tài);第二個對象為中等速度,半徑為5,顏色blue,關(guān)閉狀態(tài)。通過調(diào)用toString方法顯示該對象(2) UML圖(3

7、) 代碼package edu.neu.li.test;public class Fan private final int SLOW=1;private final int MEDIUM=2;private final int FAST=3;private int speed=SLOW; private boolean on=false; private double radius=5; private String color="blue" public Fan() public Fan(int speed,boolean on,double radius,String

8、 color) this.speed=speed;this.on=on;this.radius=radius;this.color=color; public int getspeed() return speed; public void setspeed(int speed) this.speed=speed; public boolean geton() return on; public void seton(boolean on) this.on=on; public double getradius() return radius; public void setradius(do

9、uble radius) this.radius=radius; public String getcolor() return color; public void setcolor(String color) this.color=color; public String toString() if(on=true)return "the fan is:" +on+ "the speed is:" +speed+ "the color:" +color+ "the radius:" +radius;elsere

10、turn "fan is off"+"the color:"+color+"the radius:"+radius; package edu.neu.li.run;import edu.neu.li.test.Fan;public class Fan2 public static void main(String args)Fan F=new Fan();Fan F2=new Fan(3,true,10,"yellow");System.out.println("The Fan:"+F2.toS

11、tring();(4) 運(yùn)行結(jié)果:the fan is: true the speed is: 3 the color: yellow the radius: 10.0三、 10.4(1) 題目設(shè)計(jì)名為MyPoint的類表示平面中的一個坐標(biāo)(x,y)兩個私有屬性:x、y表示橫、縱坐標(biāo)無參數(shù)構(gòu)造方法:用于創(chuàng)建原點(diǎn)(0,0)根據(jù)指定坐標(biāo)(x,y)創(chuàng)建一個點(diǎn)的(帶參數(shù))構(gòu)造方法屬性的getter和setter方法【注意使用this關(guān)鍵字】distance方法:返回任意兩點(diǎn)間的距離distance方法:返回本坐標(biāo)和任意一點(diǎn)間的距離(2) UML圖(3) 代碼package edu.neu.li.tes

12、t;public class MyPoint private double x;private double y;public MyPoint() x=0; y=0;public MyPoint(double x, double y) super(); this.x = x; this.y = y;public double getX() return x;public void setX(double x) this.x = x;public double getY() return y;public void setY(double y) this.y = y;public double

13、distance(MyPoint p1,MyPoint p2) double d=0; d=Math.hypot(p1.getX()-p2.getX(), (p1.getY()-p2.getY(); return d;public double distance (MyPoint p1) double d=0; d=Math.hypot(x-p1.getX(),(y-p1.getY(); return d;package edu.neu.li.run;import edu.neu.li.test.MyPoint;public class test public static void main

14、(String args) MyPoint m=new MyPoint(); MyPoint m1=new MyPoint(10,30.5);System.out.println("The distance is:"+m.distance(m,m1);(4) 運(yùn)行結(jié)果The symbol is:32.09750769140807四、 11.2(1) 題目(Person、Student、Employee、Faculty和Staff類)設(shè)計(jì)一個名為Person的類和它的兩個名為Stude和Employee子類。Employee類又有子類:教員類Faculty和職員類Staff。

15、每個人都有姓名、地址、電話號碼和電子郵箱地址。學(xué)生有班級狀態(tài)(大一、大二、大三或大四)。將這些狀態(tài)定義為常量。一個雇員有辦公室、工資和受聘日期。定義一個名為MyDate的類,包含數(shù)據(jù)域:year(年)、month(月)和day(日)。教員有辦公時間和級別。職員有職務(wù)稱號。覆蓋每個類中的toString方法,顯示相應(yīng)的類名和人名。 畫出這些類的UML圖。實(shí)現(xiàn)這些類。編寫一個測試程序,創(chuàng)建Person、Student、Employee、Faculty和Staff,并且調(diào)用它們的toSting()方法。(2) UML圖(3) 代碼class Person  String

16、 name; String address;  String telphone;  public Person(String n,String a,String t)   name=n;   address=a;   telphone=t;    public String toString()   

17、;return name+" Person"      class Student extends Person   final String class1="一年級"  final String class2="二年級"  final String class3="三年級"&#

18、160; final String class4="四年級"   public Student(String n,String a,String t)   super(n,a,t);       public String toString()   return name+" Student&quo

19、t;     class Employee extends Person String office;  double salary;  public Employee(String n,String a,String t,String o,double s)   super(n,a,t);   office=o; &

20、#160; salary=s;    public String toString()   return name+" Employee"      class Faculty extends Employee  int Level;  public Faculty(String n,String&

21、#160;a,String t,String o,double w,int level)   super(n,a,t,o,w);   Level=level;    public String toString()   return name+" Faculty"      class Staff&

22、#160;extends Employee  String position;  public Staff(String n,String a,String t,String o,double w,String p)   super(n,a,t,o,w);   position=p;    public String toString() 

23、;  return name+" Staff"       public class ff    public static void main(String args)        Person p=new Person ("柯雅心","陜西省","quot;);display(p);    Student s=new Student ("劉子航","陜西省","quot;);    display(s);    Employee e=new Employee (&

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論