Java程序设计作业二.docx
《Java程序设计作业二.docx》由会员分享,可在线阅读,更多相关《Java程序设计作业二.docx(13页珍藏版)》请在冰豆网上搜索。
Java程序设计作业二
一、
(1)题目
设计一个Stock的类,这个类包括:
一个名为symbol的字符串数据域表示股票代码
一个名为name的字符串数据域表示股票名字
一个名为previousClosingPrice的double型数据域,它存储的是前一日的股票值
一个名为currentPrice的double型数据域,它存储的是当时的股票值。
创建一支有特定代码和名字的股票的构造方法。
一个名为getChangePercent()的方法返回从previousClosingPrice变化到currentPrice的百分比。
实现这个类,编写一个测试程序,创建一个Stock对象,它的股票代码是ORCL股票名字为OracleCorporation,前一日收盘价是。
设置新的当前值为,然后显示市值变化的百分比。
(2)UML图
(3)代码
packageclassStock{
privateStringsymbol="";
privateStringname;
privatedoublepreviousClosingPrice;
privatedoublecurrentPrice;
publicStock(){
symbol="";
name="";
previousClosingPrice=;
currentPrice=;
}
publicStock(Stringnewsymble,Stringnewname){
symbol=newsymble;
name=newname;
}
publicStringgetsymbol()
{
returnsymbol;
}
publicStringgetname()
{
returnname;
}
publicdoublegetChangPercent(){
returncurrentPrice/previousClosingPrice;
}
}
packageclasstest1{
publicstaticvoidmain(String[]args)
{
Stocks1=newStock();
Stocks=newStock("ORCL","OracleCorporation");
"Thesymbolis:
"+());
"Thenameis:
"+());
"TheChangPercentis:
"+());
}
}
(4)运行结果
Thesymbolis:
ORCL
Thenameis:
OracleCorporation
TheChangPercentis:
二、
(1)题目
设计一个名为Fan的类表示风扇。
这个类包括:
1三个常量SLOW,MEDIUM和FAST,其值分别为1,2,3,表示风扇的速度;
2int类型的数据域speed表示风扇的速度;默认值为SLOW
3boolean型的数据域on表示风扇是否打开;默认值为false
4double型的数据域radius表示风扇的半径;默认值为5
5string型的数据域color表示风扇的颜色;默认值为blue
6无参构造方法创建默认风扇;
7全部四个数据域的访问器和修改器;
9toString()方法返回描述风扇的字符串。
如果风扇打开,该方法用一个组合的字符串返回风扇的速度,颜色和半径;否则,用一个组合的字符串和“fanisoff”一起返回风扇的颜色和半径。
画出该类的UML图并实现它。
编写一个测试程序,创建两个Fan对象,将第一个对象设置为最大速度,半径为10,颜色为yellow,打开状态;第二个对象为中等速度,半径为5,颜色blue,关闭状态。
通过调用toString方法显示该对象
(2)UML图
(3)代码
packageclassFan{
privatefinalintSLOW=1;
privatefinalintMEDIUM=2;
privatefinalintFAST=3;
privateintspeed=SLOW;
privatebooleanon=false;
privatedoubleradius=5;
privateStringcolor="blue";
publicFan(){
}
publicFan(intspeed,booleanon,doubleradius,Stringcolor){
=speed;
=on;
=radius;
=color;
}
publicintgetspeed(){
returnspeed;
}
publicvoidsetspeed(intspeed){
=speed;
}
publicbooleangeton(){
returnon;
}
publicvoidseton(booleanon){
=on;
}
publicdoublegetradius(){
returnradius;
}
publicvoidsetradius(doubleradius){
=radius;
}
publicStringgetcolor(){
returncolor;
}
publicvoidsetcolor(Stringcolor){
=color;
}
publicStringtoString(){
if(on==true)
{
return"thefanis:
"+on+"thespeedis:
"+speed+"thecolor:
"+color+"theradius:
"+radius;}
else
{
return"fanisoff"+"thecolor:
"+color+"theradius:
"+radius;
}
}
}
packageclassFan2{
publicstaticvoidmain(String[]args)
{
FanF=newFan();
FanF2=newFan(3,true,10,"yellow");
"TheFan:
"+());
}
}
(4)运行结果:
thefanis:
truethespeedis:
3thecolor:
yellowtheradius:
三、
(1)题目
设计名为MyPoint的类表示平面中的一个坐标(x,y)
两个私有属性:
x、y表示横、纵坐标
无参数构造方法:
用于创建原点(0,0)
根据指定坐标(x,y)创建一个点的(带参数)构造方法
属性的getter和setter方法【注意使用this关键字】
distance方法:
返回任意两点间的距离
distance方法:
返回本坐标和任意一点间的距离
(2)UML图
(3)代码
packageclassMyPoint{
privatedoublex;
privatedoubley;
publicMyPoint(){
x=0;
y=0;
}
publicMyPoint(doublex,doubley){
super();
=x;
=y;
}
publicdoublegetX(){
returnx;
}
publicvoidsetX(doublex){
=x;
}
publicdoublegetY(){
returny;
}
publicvoidsetY(doubley){
=y;
}
publicdoubledistance(MyPointp1,MyPointp2){
doubled=0;
d=(()()),()()));
returnd;
}
publicdoubledistance(MyPointp1){
doubled=0;
d=(()),()));
returnd;
}
}
packageclasstest{
publicstaticvoidmain(String[]args)
{
MyPointm=newMyPoint();
MyPointm1=newMyPoint(10,;
"Thedistanceis:
"+(m,m1));
}
}
(4)运行结果
Thesymbolis:
四、
(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 ("柯雅心","陕西省","");
display(p);
Student s=new Student ("刘子航","陕西省","");
display(s); Employee e=new Employee ("王珺","陕西省","02","人事局",;
display(e); Faculty f=new Faculty ("王影","陕西省","","办公室",,1);
display(f); Staff sta=new Staff ("小明","陕西省","","人事科",,"副局长");
display(sta);
}
public static void display(Person person){
}
}
(4)运行结果
柯雅心Person
刘子航Student
王珺Employee
王影Faculty
小明Staff