java+程序设计+课后习题+答案+清华大学出版社.doc
《java+程序设计+课后习题+答案+清华大学出版社.doc》由会员分享,可在线阅读,更多相关《java+程序设计+课后习题+答案+清华大学出版社.doc(283页珍藏版)》请在冰豆网上搜索。
序言
先说在前面,如果本电子版侵犯了原书的版权,请立即删除。
本电子版用于个人学习交流,严禁用于商业出版。
本电子版答案的java源代码源自于《Java程序设计(第二版)题解与上机指导》,本人对该书的全部源代码都进行了编辑,编译,运行等工作,修改了其中的错误源代码。
不过有些题目的解答实在是莫米其妙本人也无法理解原作者的意思,本人会在最后给出其他例子用来弥补此之不足。
其中加入了大量的注释说明语句,这些是原书所没有的。
这些代码在jbuilder9.0下调试通过,部分的是在jbuilder2006下调试通过的,还有在jcreator下调试通过的。
其中大部分源代码在jbuilder9.0下就能编译运行。
还有一些源代码是需要更高的jdk版本才能编译运行,所以这些源代码就在jbuilder2006下编译运行。
Jbuilder9.0自带的jdk是jdk1.4的,jbuilder2006自带的jdk是jdk1.5的。
特别说明的是jbuilder2006所支持的字体特别少,要想加入宋体等其他字体的方法请参看839682048的空间。
ed2k:
//|file|%5BJBuilder.2006.%E4%BC%81%E4%B8%9A%E5%AE%8C%E6%95%B4%E7%89%88%5D.JB2006_CD1%5Bfree_guru%5D.rar|687823710|43a2e30e546edf87edcbe426fb9fe600|h=sbmjqn54tio7msl33lvabc4jtuz4lgcn|/
以上是jbuilder2006Windows平台的下载地址,复制以上代码,打开迅雷,新建,就可以下载了,里面有破解补丁。
安装需要虚拟光驱。
众所周知,程序设计题目的答案是不唯一的,读者发挥的潜能很大。
本电子版是为了方便读者,使其免去输代码这一关键步骤。
因为原书的作者的水平有限,本人的水平也有限,难免有错误和不妥之处,实现的代码也不一定是最优的,读者可以参考本电子版答案和其他参考书的内容,给出更全面的答案,恳请广大读者特别是同行专家批评指正。
(通过阅读原书,我发现作者编写此书的态度不是很认真,不像此书同系列的谭浩强的《c程序设计》一书那样认真严谨。
不过还是要感谢原书作者。
在此也恳请所有出书的人要抱着一种对读者,特别是对中国的读者负责的认真态度。
)
如果你有什么疑问可以和本人联系839682048。
最后,在此祝愿所有读者,特别是中国的读者:
学业有成,事业更上一层楼。
让我们一起为中国的计算机发展而努力奋斗吧!
!
!
//习题2.2
importjava.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()
}//endclassMyDate
classEmployee{
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;
}//end
publicinthireYear(){
returnhireDay.getYear();
}
}//endclassEmployee
publicclassMyTestClass{
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.4
importjava.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.9
importjava.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.10
importjava.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()
}//endclassDate
publicclassTeacher{
Stringname;//教师名字
booleansex;//性别,t