JAVA编程思想课后习题答案_第1页
JAVA编程思想课后习题答案_第2页
JAVA编程思想课后习题答案_第3页
JAVA编程思想课后习题答案_第4页
JAVA编程思想课后习题答案_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java编程思想(第四版)习题答案

第二章

练习1:publicclassPrimitiveTest(

staticinti;

sUticcharc;

publicstaticvoidmain(String[]args){

System.out.prin(ln("int="+i);

System.out.println("char="+c);

)

)

练习2:publicclassHelloWorld{

publicstaticvoidmain(String[]args'{

System.out.println("HelloWorld!");

}

}

练习3:publicclassATNTcst(

publicstaticvoidmain(String[]args]{

classATypeName{

inii;

doubled;

booleanb;

voidshow(){

Systein.out.println(i);

System.out.println(d);

System.out.println(b);

}

)

AiypeNamca=newATypeName。;

a.i=3;

axl=2.71828;

a.b=false;

a.show();

)

}

练习4:publicclassDataOnlyTest{

publicstaticvoidmain(String[]args){

classDataOnly{

inti;

doubled;

booleanb;

voidshow(){

System.out.println(i);

System.out.println(d);

Systcm.out.println(b);

)

)

DataOnlydata=newDataOnlyO;

data.i=3;

data.d=2.71828;

data.b=false;

data.showO;

}

)

练习5:publicclassDOTest2(

publicstaticvoidmain(String(]args){

classDataOnly{

inti;

doubled;

booleanb;

voidshow(){

System.out.println(i);

System.out.println(d);

System.out.println(b);

)

]

DataOnlydata=newDataOnlyO;

data.i=234;

data.d=2.1234545;

data.b=true;

data.showO;

}

}

练习6:publicclassStorageTest{

publicstaticvoidmain(String[]args)(

classStoreStuff{

intstorage(Strings){

returns.length()*2;

}

}

StoreStuffx=newStoreStuff(j;

System.oul.println(x.storage("hi"));

}

}

练习7:

classStaticTest(

staticinti=47;

classIncrcmcntablc{

staticvoidincrement(){StaticTest.i++;}

)

publicclassITest(

publicstaticvoidmain(String[]args){

System.ou(.prinlln("StalicTes(.i="+StaticTest.i);

StaticTeststl=newStaticTest();

StaticTestst2=newSta(icTest():

System.out.println("st1.i="+stl.i);

System.out.println("st2.i="+st2.i);

Incrementablesf=newIncrementable();

sf.increment();

System.out.println("Aftersf.increment()called:");

System.out.printlnC'stl.i="+stl.i);

System.out.printlnC'stl.i="+st2.i);

Incrementable.incrementO;

SystCin.out.priiHln(,'AfterIncrcmcntablc.incrcincntcalled;"),

System.out.println("st1.i="+stl.i);

System.outprintln("st2.i="+st2.i);

)

)

练习8:classStaticTest{

staticinti=47;

classIncrementable{

staticvoidincrement(){StaticTest,i++;}

}

publicclassOneStaticTest{

publicstaticvoidmain(String[]args)(

System.out.println("StaticTest.i="+StaticTest.i);

StaticTeststl=newStaticTesti);

StaticTestst2=newStaticTesti);

System.out.println("st1.i="+stl.i);

System.oul.println("st2.i="+st2.i);

Incrementable.incrementO;

System.oul.println("AfterIncrcmentable.increment()called:");

System.out.printlnC'stl.i="+stl.i);

System.oul.println("st2.i="+st2.i);

Incrementable.incrementO;

System.out.prin(ln("AfterIncrementable.incrementcalled:");

System.out.println("stl.i="+stl.i);

System.out.println("st2.i="+st2.i);

stl.i=3;

System.out.println("Aiterstl.i=3,");

Sys(em.oui.priniln("si1.i='*+sd.i);

System.out.println("st2.i="+st2.i);

System.out.println("CreateanotherStaticTest,st3.");

StalicTestst3=newStatic'Iesti;);

System.out.println("st3.i="+st3.i);

)

)

练习9:publicclassAutoboxTest{

publicstaticvoidmain(String[]args){

booleanb=false;

charc='x';

bytet=8;

shorts=16;

inti=32;

longI-64,

floatf=O.32f;

doubled=0.64;

BooleanB=b;

System.out.println("booleanb="+b);

System.out.println("BooleanB="+B);

CharacterC=c;

System.oul.println("charc="+c);

System.out.println("CharacterC="+C);

ByteT=t;

System.out.println("bytet="-t);

System.out.println("ByteT="+T);

ShortS=s;

System.out.println("shorts="+s);

Systcm.oul.println("ShortS=*+S);

IntegerI=i;

System.out.println("inti="+i);

System.out.println("IntegerI="+I);

LongL=1;

System.out.println("long[="+]);

System.out.println("LongL="+L);

FloatF=f;

Systeni.out.println("floatf="4-f);

Systcm.oul.println("FloalF="+F);

DoubleD=d;

System.out.println("doubled="+d);

System.out.println("DoubleD="+D);

}

练习10:publicclassCommandArgTestI

publicstaticvoidmain(String[]args){

System.out.println("args[OJ="+args[OJ);

System.out.println("argsf1]="+args(lj);

System.oul.print1n("args[2]="+args⑵);

)

}

练习11:

publicclassRainbow{

publicstaticvoidmain(String|]args)(

AllTlieCoIorsOfTheRainbowate=newAllTheColorsOfTlwRainbowO;

System.oul.println("atc.anIntegerRepresentingColors="+alc.anlntegerRepresenlingColors);

atc.changeColor(7);

atc.changeTheHueOfTheColor(77);

System.out.println("Aftercolorchange.atc.anlntegerRepresentmgColors="+

atc.anIiHcgcrRcprc6CiHingColois),

System.out.println("atc.hue="+ate.hue);

}

}

classAllTheColorsOfTheRainbow{

intanlntegerRepresentingColors=0;

inthue=0;

voidchangeTheHueOfTheColor(intnewHue){

hue=newHue;

)

intchangeColor(intnewColor){

returnanIntegerRepresenlingG)lors=newColor;

)

1

练习12:publicclassDocTest(

/**Entrypoingtoclass&application.

*@paramargsarrayofstringarguments

*@throwsexceptionsNoexceptionsthrown

*/

publicstaticvoidmain(String(]args]{

System.oul.println("Hello,it's:");

System.out.println(newDate());

}

}

练习13-1:publicclassDocumentation1{

/**Afieldcomment*/

publicinti;

/**Amethodcomment*/

publicvoidf()()

}

2:publicclassDocumentation2{

Dated=newDate();

voidshowDale(){

System.out.println("Date="+d);

}

I

3:publicclassDocumentation3{

publicstaticvoidmain(String|]args){

Dated=newDate();

System.oul.println("d="+d);

}

}

练习14:publicclassDocumentation4{

publicinti-2,

privateintj=3;

publicstaticvoidmain(String[]args){

Dated=newDate();

System.out.println("d="+d);

}

I

练习15:publicclassHclloDocTesl(

publicstaticvoidmain(String|]args){

System.out.println("HelloWorld!");

)

)

练习16:

classTree{

intheight;

Tree()(

System.out.println("Plantingaseedling");

height=0;

}

Tree(intinitialHeight){

height=initialHeight;

System.out.println("Creatingnewtreethatis"+height+"feettall");

}

voidinfo(){

System.out.println("Treeis"+height+"feettall");

}

voidinfb(Strings){

System.out.println(s+Treeis"+height+"feettall");

}

publicclassOverloading{

publicstaticvoidmain(Slring(]args]{

fbr(inti=0;i<5;i++){

Tree(=newTrcc(i);

t.infb();

("over)oadingmethod");

)

HOverloadedconstructor:

newTree。;

}

}

第三章

练习1:publicclassPrintTest(

publicstaticvoidmain(String[]args){

print("Hello,fromshortform.");

P.rintln("Hellofromgreggordonform.");

Systcm.oul.println("Hcllofromlongform.");

}

)

练习2:classTube(

floatlevel;

}

publicclassAssign{

publicstaticvoidmain(String[]args){

Tubetl=newTube();

Tubet2=newTube();

tl.level=0.9f;

t2.1evel=0.47f;

P.rintln("l:tl.level:"+tl.level+n,t2.level:"+12.level);

tl=12;

P.rintln("2:tl.level:"+tl.level+",t2.level:"+12.level);

tl.level=0.27f;

P.rintln("3:tl.level:"+tl.level+M,t2.level:"+t2.level);

}

}

练习3:classBox{

floata;

}

publicclassPassObject2{

staticvoidf(Boxy){

y.a=2.71828f;

}

publicstaticvoidmain(Slring(]args]{

Boxx=newBox();

x.a=3.l416f;

print(Ml:x.a="+x.a);

f(x);

print(M2:x.a="+x.a);

)

}

练习4:classVelocitycalculator{

staticfloatvelocity(floatd.floatt){

if(t==0)returnOf;

elseictuind/t,

I

}

publicclassVelocityTester{

publicstaticvoidmain(String(]args){

floatd=565.3f;

floatl=3.6f;

System.out.println("Distance:*+d);

System.out.println("Time:"+1);

floatv=VelocityCalculatonvelocityCd,t);

System.out.println("Ve!ocity:"+v);

)

)

练习5;classDog(

Stringname;

Stringsays;

voidsetName(Stringn){

name=n;

)

voidsetSays(Strings){

says=s;

}

voidshowNamc(){

P.rintln(nanie);

}

voidspeak。(

P.rintln(says);

J

publicclassDogTest{

publicstaticvoidmain(String[]args){

Dogspot=newDog();

spot.setName("Spot");

spot.sctSays("Ruffl");

Dogscruffy=newDog();

scruffy.setNameC'Scruffy");

scrufify.setSays("Wurf!M);

spot.showName();

spol.speak();

scruffy.showName();

scruffy.speak();

}

}

练习6:classDog(

Stringname;

Stringsays;

voidsetName(Stringn){

name=n;

}

voidsetSays(Strings){

says=s;

}

voidshowName(){

P.rintln(name);

)

voidspeak(){

P.rinlln(says);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论