




已阅读5页,还剩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) 代码classPersonStringname;Stringaddress;Stringtelphone;publicPerson(Stringn,Stringa,Stringt)name=n;address=a;telphone=t;publicStringtoString()returnname+Person;classStudentextendsPersonfinalStringclass1=一年级;finalStringclass2=二年级;finalStringclass3=三年级;finalStringclass4=四年级;publicStudent(Stringn,Stringa,Stringt)super(n,a,t);publicStringtoString()returnname+Student;classEmployeeextendsPersonStringoffice;doublesalary;publicEmployee(Stringn,Stringa,Stringt,Stringo,doubles)super(n,a,t);office=o;salary=s;publicStringtoString()returnname+Employee;classFacultyextendsEmployeeintLevel;publicFaculty(Stringn,Stringa,Stringt,Stringo,doublew,intlevel)super(n,a,t,o,w);Level=level;publicStringtoString()returnname+Faculty;classStaffextendsEmployeeStringposition;publicStaff(Stringn,Stringa,Stringt,Stringo,doublew,Stringp)super(n,a,t,o,w);position=p;publicStringtoString()returnname+Staff;publicclassffpublicstaticvoidmain(Stringargs)Personp=newPerson(柯雅心,陕西省;display(p);Students=newStudent(刘子航,陕西省;display(s);Employeee=newEmployee(王珺,陕西省,0395112222,人事局,222.00);di
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 实验室安全教育心得体会
- 2024年水利水电工程考试难点解析与试题及答案
- 2025届平舆县数学三年级第一学期期末监测试题含解析
- 小学一年级成长教育故事案例
- 水利水电工程现场勘察试题及答案
- 提升城市建设项目的试题及答案
- 绿色农业生态农场种植技术合作协议
- 农民农技培训服务协议
- 中级经济师考试的相关政策与法规试题及答案
- 信息技术网络安全知识测试卷
- 科普项目申报书-中国科协
- 食蚜蝇课件完整版
- 主题班会《中国梦我的梦》课件
- 义务教育数学新课程标准选择题题库测试卷精选450题(2022版)含答案
- 古诗词诵读《客至》-统编版高中语文选择性必修下册
- 建筑材料分类整理
- YY/T 0801.2-2010医用气体管道系统终端第2部分:用于麻醉气体净化系统的终端
- 人民币发展史-课件(PPT演示)
- GB/T 31349-2014节能量测量和验证技术要求中央空调系统
- 武汉大学管理学全套课件龚丽敏老师版
- 泗洪县国土空间规划近期实施方案
评论
0/150
提交评论