JAVA编程思想课后习题答案

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

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

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

资源描述

Java编程思想(第四版)习题答案第二章练习1:publicclassPrimitiveTest{staticinti;staticcharc;publicstaticvoidmain(String[]args){System.out.println(int=+i);System.out.println(char=+c);}}练习2:publicclassHelloWorld{publicstaticvoidmain(String[]args){System.out.println(HelloWorld!);}}练习3:publicclassATNTest{publicstaticvoidmain(String[]args){classATypeName{inti;doubled;booleanb;voidshow(){System.out.println(i);System.out.println(d);System.out.println(b);}}ATypeNamea=newATypeName();a.i=3;a.d=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);System.out.println(b);}}DataOnlydata=newDataOnly();data.i=3;data.d=2.71828;data.b=false;data.show();}}练习5:publicclassDOTest2{publicstaticvoidmain(String[]args){classDataOnly{inti;doubled;booleanb;voidshow(){System.out.println(i);System.out.println(d);System.out.println(b);}}DataOnlydata=newDataOnly();data.i=234;data.d=2.1234545;data.b=true;data.show();}}练习6:publicclassStorageTest{publicstaticvoidmain(String[]args){classStoreStuff{intstorage(Strings){returns.length()*2;}}StoreStuffx=newStoreStuff();System.out.println(x.storage(hi));}}练习7:classStaticTest{staticinti=47;}classIncrementable{staticvoidincrement(){StaticTest.i++;}}publicclassITest{publicstaticvoidmain(String[]args){System.out.println(StaticTest.i=+StaticTest.i);StaticTestst1=newStaticTest();StaticTestst2=newStaticTest();System.out.println(st1.i=+st1.i);System.out.println(st2.i=+st2.i);Incrementablesf=newIncrementable();sf.increment();System.out.println(Aftersf.increment()called:);System.out.println(st1.i=+st1.i);System.out.println(st2.i=+st2.i);Incrementable.increment();System.out.println(AfterIncrementable.incrementcalled:);System.out.println(st1.i=+st1.i);System.out.println(st2.i=+st2.i);}}练习8:classStaticTest{staticinti=47;}classIncrementable{staticvoidincrement(){StaticTest.i++;}}publicclassOneStaticTest{publicstaticvoidmain(String[]args){System.out.println(StaticTest.i=+StaticTest.i);StaticTestst1=newStaticTest();StaticTestst2=newStaticTest();System.out.println(st1.i=+st1.i);System.out.println(st2.i=+st2.i);Incrementable.increment();System.out.println(AfterIncrementable.increment()called:);System.out.println(st1.i=+st1.i);System.out.println(st2.i=+st2.i);Incrementable.increment();System.out.println(AfterIncrementable.incrementcalled:);System.out.println(st1.i=+st1.i);System.out.println(st2.i=+st2.i);st1.i=3;System.out.println(Afterst1.i=3,);System.out.println(st1.i=+st1.i);System.out.println(st2.i=+st2.i);System.out.println(CreateanotherStaticTest,st3.);StaticTestst3=newStaticTest();System.out.println(st3.i=+st3.i);}}练习9:publicclassAutoboxTest{publicstaticvoidmain(String[]args){booleanb=false;charc='x';bytet=8;shorts=16;inti=32;longl=64;floatf=0.32f;doubled=0.64;BooleanB=b;System.out.println(booleanb=+b);System.out.println(BooleanB=+B);CharacterC=c;System.out.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);System.out.println(ShortS=+S);IntegerI=i;System.out.println(inti=+i);System.out.println(IntegerI=+I);LongL=l;System.out.println(longl=+l);System.out.println(LongL=+L);FloatF=f;System.out.println(floatf=+f);System.out.println(FloatF=+F);DoubleD=d;System.out.println(doubled=+d);System.out.println(DoubleD=+D);}}练习10:publicclassCommandArgTest{publicstaticvoidmain(String[]args){System.out.println(args[0]=+args[0]);System.out.println(args[1]=+args[1]);System.out.println(args[2]=+args[2]);}}练习11:publicclassRainbow{publicstaticvoidmain(String[]args){AllTheColorsOfTheRainbowatc=newAllTheColorsOfTheRainbow();System.out.println(atc.anIntegerRepresentingColors=+atc.anIntegerRepresentingColors);atc.changeColor(7);atc.changeTheHueOfTheColor(77);System.out.println(Aftercolorchange,atc.anIntegerRepresentingColors=+atc.anIntegerRepresentingColors);System.out.println(atc.hue=+atc.hue);}}classAllTheColorsOfTheRainbow{intanIntegerRepresentingColors=0;inthue=0;voidchangeTheHueOfTheColor(intnewHue){hue=newHue;}intchangeColor(intnewColor){returnanIntegerRepresentingColors=newColor;}}练习12:publicclassDocTest{/**Entrypoingtoclass&application.*@paramargsarrayofstringarguments*@throwsexceptionsNoexceptionsthrown*/publicstaticvoidmain(String[]args){System.out.println(Hello,it's:);System.out.println(newDate());}}练习13-1:publicclassDocumentation1{/**Afieldcomment*/publicinti;/**Amethodcomment*/publicvoidf(){}}2:publicclassDocumentation2{Dated=newDate();voidshowDate(){System.out.println(Date=+d);}}3:publicclassDocumentation3{publicstaticvoidmain(String[]args){Dated=newDate();System.out.println(d=+d);}}练习14:publicclassDocumentation4{publicinti=2;privateintj=3;publicstaticvoidmain(String[]args){Dated=newDate();System.out.println(d=+d);}}练习15:publicclassHelloDocTest{publicstaticvoidmain(String[]args){System.out.println(HelloWorld!);}}练习16:classTree{intheight;Tree(){System.out.println(Plantingaseedling);height=0;}Tree(intinitialHei

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

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

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

×
保存成功