java程序设计(辛运帏_饶一梅_马素霞_第二版)课后习题答案

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

第二章习题2.2importjava.util.*;classMyDate{privateintyear;privateintmonth;privateintday;publicMyDate(inty,intm,intd){//构造函数,构造方法year=y;month=m;day=d;}//endpublicMyDate(inty,intm,intd)publicintgetYear(){//返回年returnyear;}//endgetYear()publicintgetMonth(){//返回月returnmonth;}//endgetMonth()publicintgetDay(){//返回日returnday;}//endgetDay()}//endclassMyDateclassEmployee{privateStringname;privatedoublesalary;privateMyDatehireDay;publicEmployee(Stringn,doubles,MyDated){name=n;salary=s;hireDay=d;}//endpublicEmployee(Stringn,doubles,MyDated)publicvoidprint(){System.out.println(名字:+name+\n工资:+salary+\n雇佣年份:+hireYear()+\n);}//endprint()publicvoidraiseSalary(doublebyPercent){salary*=1+byPercent/100;}//endpublicinthireYear(){returnhireDay.getYear();}}//endclassEmployeepublicclassMyTestClass{publicstaticvoidmain(String[]args){Employee[]staff=newEmployee[3];staff[0]=newEmployee(HarryHacker,35000,newMyDate(1989,10,1));staff[1]=newEmployee(CarlCarcker,75000,newMyDate(1987,12,15));staff[2]=newEmployee(TonyTester,38000,newMyDate(1990,3,12));intintegerValue;System.out.println(Theinformationofemployeeare:);for(integerValue=0;integerValue=2;integerValue++){staff[integerValue].raiseSalary(5);}//endfor()for(integerValue=0;integerValue=2;integerValue++){staff[integerValue].print();}//endfor()}//endmain()}//endclassMyTestClass//习题2.4importjava.util.*;publicclassDataType{publicstaticvoidmain(String[]args){booleanflag;charyesChar;bytefinByte;intintValue;longlongValue;shortshortValue;floatfloatValue;doubledoubleValue;flag=true;yesChar='y';finByte=30;intValue=-7000;longValue=200l;shortValue=20000;floatValue=9.997E-5f;doubleValue=floatValue*floatValue;System.out.println(thevaluesare:);System.out.println(布尔类型变量flag=+flag);System.out.println(字符型变量yesChar=+yesChar);System.out.println(字节型变量finByte=+finByte);System.out.println(整型变量intValue=+intValue);System.out.println(长整型变量longValue=+longValue);System.out.println(短整型变量shortValue=+shortValue);System.out.println(浮点型变量floatValue=+floatValue);System.out.println(双精度浮点型变量doubleValue=+doubleValue);}//endmain()}//习题2.9importjava.util.*;classPubTest1{privateintivar1;privatefloatfvar1,fvar2;publicPubTest1(){fvar2=0.0f;}publicfloatsum_f_I(){fvar2=fvar1+ivar1;returnfvar2;}publicvoidprint(){System.out.println(fvar2=+fvar2);}publicvoidsetIvar1(intivalue){ivar1=ivalue;}publicvoidsetFvar1(floativalue){fvar1=ivalue;}}publicclassPubMainTest{publicstaticvoidmain(String[]args){PubTest1pubt1=newPubTest1();pubt1.setIvar1(10);pubt1.setFvar1(100.02f);pubt1.sum_f_I();pubt1.print();}}//习题2.10importjava.util.*;classDate{privateintyear;privateintmonth;privateintday;publicDate(intday,intmonth,intyear){//构造函数,构造方法this.year=year;this.month=month;this.day=day;}//endpublicMyDate(inty,intm,intd)publicintgetYear(){//返回年returnyear;}//endgetYear()publicintgetMonth(){//返回月returnmonth;}//endgetMonth()publicintgetDay(){//返回日returnday;}//endgetDay()}//endclassDatepublicclassTeacher{Stringname;//教师名字booleansex;//性别,true表示男性Datebirth;//出生日期StringsalaryID;//工资号Stringdepart;//教师所在系所Stringposit;//教师职称StringgetName(){returnname;}voidsetName(Stringname){this.name=name;}booleangetSex(){returnsex;}voidsetSex(booleansex){this.sex=sex;}DategetBirth(){returnbirth;}voidsetBirth(Datebirth){this.birth=birth;}StringgetSalaryID(){returnsalaryID;}voidsetSalaryID(StringsalaryID){this.salaryID=salaryID;}StringgetDepart(){returndepart;}voidsetDepart(Stringdepart){this.depart=depart;}StringgetPosit(){returnposit;}voidsetPosit(Stringposit){this.posit=posit;}publicTeacher(){System.out.println(父类无参数的构造方法!!!!!!!);}//如果这里不加上这个无参数的构造方法将会出错!!!!publicTeacher(Stringname,booleansex,Datebirth,Stringsalaryid,Stringdepart,Stringposit){this.name=name;this.sex=sex;this.birth=birth;this.salaryID=salaryid;this.depart=depart;this.posit=posit;}//endTeacher()publicvoidprint(){System.out.print(theteacher'name:);System.out.println(this.getName());System.out.print(theteacher'sex:);if(this.getSex()==false){System.out.println(女);}else{System.out.println(男);}System.out.print(theteacher'birth:);System.out.println(this.getBirth().getYear()+-+this.getBirth().getMonth()+-+this.getBirth().getDay());System.out.print(theteacher'salaryid:);System.out.println(this.getSalaryID());System.out.print(theteacher'posit:);System.out.println(this.getPosit());System.out.print(theteacher'depart:);System.out.println(this.getDepart());}//endprint()publicstaticvoidmain(String[]args){Datedt1=newDate(11,23,1989);Datedt2=newDate(2,6,1975);Datedt3=newDate(11,8,1964);Datedt4=newDate(10,4,1975);Datedt5=newDate(8,9,1969);//创建各系教师实例,用来测试Teachert1=newTeacher(王莹,false,dt1,123,经济学,prefessor);ResearchTeacherrt=newResearchTeacher(杨zi青,true,dt2,421,软件工程,associateprefessor,software);LabTeacherlat=newLabTeacher(王夏瑾,false,dt3,163,外语,pinstrucor,speechlab);LibTeacherlit=newLibTeacher(马二孩,true,dt4,521,大学物理,prefessor,physicalLib);AdminTeacherat=newAdminTeacher(王xi,false,dt5,663,环境,prefessor,dean);/////////分别调用各自的输出方法,输出相应信息////////////////////////////System.out.println(-------------------------------);t1.print();//普通教师信息System.o

1 / 247
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功