《Java程序设计》作业二(8页).doc
-一、二、三、四、 Java程序设计作业二-第 8 页五、 9.2(1) 题目设计一个Stock的类,这个类包括:一个名为symbol的字符串数据域表示股票代码一个名为name的字符串数据域表示股票名字 一个名为previousClosingPrice的double型数据域,它存储的是前一日的股票值一个名为currentPrice的double型数据域,它存储的是当时的股票值。创建一支有特定代码和名字的股票的构造方法。一个名为getChangePercent()的方法返回从previousClosingPrice变化到currentPrice的百分比。实现这个类,编写一个测试程序,创建一个Stock对象,它的股票代码是ORCL股票名字为Oracle Corporation,前一日收盘价是34.5。设置新的当前值为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=""previousClosingPrice=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.li.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("The ChangPercent is:"+s1.getChangPercent();(4) 运行结果The symbol is: ORCLThe name is: Oracle CorporationThe ChangPercent is:0.9956521739六、 9.8(1) 题目设计一个名为Fan的类表示风扇。这个类包括: 1 三个常量SLOW,MEDIUM和FAST,其值分别为1,2,3,表示风扇的速度;2 int类型的数据域speed表示风扇的速度;默认值为SLOW3 boolean型的数据域on表示风扇是否打开;默认值为false4 double型的数据域radius表示风扇的半径;默认值为55 string型的数据域color表示风扇的颜色;默认值为blue6 无参构造方法创建默认风扇;7 全部四个数据域的访问器和修改器;9 toString()方法返回描述风扇的字符串。如果风扇打开,该方法用一个组合的字符串返回风扇的速度,颜色和半径;否则,用一个组合的字符串和“fan is off”一起返回风扇的颜色和半径。画出该类的UML图并实现它。编写一个测试程序,创建两个Fan对象,将第一个对象设置为最大速度,半径为10,颜色为yellow,打开状态;第二个对象为中等速度,半径为5,颜色blue,关闭状态。通过调用toString方法显示该对象(2) UML图(3) 代码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 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(double 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;elsereturn "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.toString();(4) 运行结果:the fan is: true the speed is: 3 the color: yellow the radius: 10.0七、 10.4(1) 题目设计名为MyPoint的类表示平面中的一个坐标(x,y)两个私有属性:x、y表示横、纵坐标无参数构造方法:用于创建原点(0,0)根据指定坐标(x,y)创建一个点的(带参数)构造方法属性的getter和setter方法【注意使用this关键字】distance方法:返回任意两点间的距离distance方法:返回本坐标和任意一点间的距离(2) UML图(3) 代码package edu.neu.li.test;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 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(String args) MyPoint m=new MyPoint(); MyPoint m1=new MyPoint(10,30.5);System.out.println("The distance is:"+m.distance(m,m1);(4) 运行结果The symbol is:32.09750769140807八、 11.2(1) 题目(Person、Student、Employee、Faculty和Staff类)设计一个名为Person的类和它的两个名为Stude和Employee子类。Employee类又有子类:教员类Faculty和职员类Staff。每个人都有姓名、地址、电话号码和电子邮箱地址。学生有班级状态(大一、大二、大三或大四)。将这些状态定义为常量。一个雇员有办公室、工资和受聘日期。定义一个名为MyDate的类,包含数据域:year(年)、month(月)和day(日)。教员有办公时间和级别。职员有职务称号。覆盖每个类中的toString方法,显示相应的类名和人名。 画出这些类的UML图。实现这些类。编写一个测试程序,创建Person、Student、Employee、Faculty和Staff,并且调用它们的toSting()方法。(2) UML图(3) 代码class Person String name; String address; String telphone; public Person(String n,String a,String t) name=n; address=a; telphone=t; public String toString() return name+" Person" class Student extends Person final String class1="一年级" final String class2="二年级" final String class3="三年级" final String class4="四年级" public Student(String n,String a,String t) super(n,a,t); public String toString() return name+" Student" 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; salary=s; public String toString() return name+" Employee"class Faculty extends Employee int Level; public Faculty(String n,String 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 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() return name+" Staff" public class ff public static void main(String args) Person p=new Person ("柯雅心","陕西省","18749601221");display(p); Student s=new Student ("刘子航","陕西省","15839652309"); display(s); Employee e=new Employee ("王珺","陕西省","0395112222","人事局",222.00); display(e); Faculty f=new Faculty ("王影","陕西省","13233344666","办公室",345.00,1); display(f); Staff sta=new Staff ("小明","陕西省","13849472334","人事科",345.00,"副局长"); display(sta); public static void display(Person person) System.out.println(person); (4) 运行结果柯雅心 Person刘子航 Student王珺 Employee王影 Faculty小明 Staff