《Java程序设计》作业二

上传人:日度 文档编号:136710628 上传时间:2020-07-01 格式:DOC 页数:13 大小:98.50KB
返回 下载 相关 举报
《Java程序设计》作业二_第1页
第1页 / 共13页
《Java程序设计》作业二_第2页
第2页 / 共13页
《Java程序设计》作业二_第3页
第3页 / 共13页
《Java程序设计》作业二_第4页
第4页 / 共13页
《Java程序设计》作业二_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《《Java程序设计》作业二》由会员分享,可在线阅读,更多相关《《Java程序设计》作业二(13页珍藏版)》请在金锄头文库上搜索。

1、一、 9.2(1) 题目设计一个Stock的类,这个类包括:一个名为symbol的字符串数据域表示股票代码一个名为name的字符串数据域表示股票名字 一个名为previousClosingPrice的double型数据域,它存储的是前一日的股票值一个名为currentPrice的double型数据域,它存储的是当时的股票值。创建一支有特定代码和名字的股票的构造方法。一个名为getChangePercent()的方法返回从previousClosingPrice变化到currentPrice的百分比。实现这个类,编写一个测试程序,创建一个Stock对象,它的股票代码是ORCL股票名字为Oracl

2、e 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; publ

3、ic 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 p

4、ublic 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

5、 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()方法

6、返回描述风扇的字符串。如果风扇打开,该方法用一个组合的字符串返回风扇的速度,颜色和半径;否则,用一个组合的字符串和“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 MEDIU

7、M=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 sp

8、eed; 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 colo

9、r) 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

10、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方法【注意使用th

11、is关键字】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.

12、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

13、 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类)设计一个名为

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 中学学案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号