JAVA編程思想課后習(xí)題答案_第1頁
JAVA編程思想課后習(xí)題答案_第2頁
JAVA編程思想課后習(xí)題答案_第3頁
JAVA編程思想課后習(xí)題答案_第4頁
JAVA編程思想課后習(xí)題答案_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

Java編程思想(第四版)習(xí)題答案

第二章

練習(xí)1:publicclassPrimitiveTest(

staticinti;

sUticcharc;

publicstaticvoidmain(String[]args){

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

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

)

)

練習(xí)2:publicclassHelloWorld{

publicstaticvoidmain(String[]args'{

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

}

}

練習(xí)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();

)

}

練習(xí)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;

}

)

練習(xí)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;

}

}

練習(xí)6:publicclassStorageTest{

publicstaticvoidmain(String[]args)(

classStoreStuff{

intstorage(Strings){

returns.length()*2;

}

}

StoreStuffx=newStoreStuff(j;

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

}

}

練習(xí)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);

)

)

練習(xí)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);

)

)

練習(xí)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);

}

練習(xí)10:publicclassCommandArgTestI

publicstaticvoidmain(String[]args){

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

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

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

)

}

練習(xí)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

練習(xí)12:publicclassDocTest(

/**Entrypoingtoclass&application.

*@paramargsarrayofstringarguments

*@throwsexceptionsNoexceptionsthrown

*/

publicstaticvoidmain(String(]args]{

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

System.out.println(newDate());

}

}

練習(xí)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);

}

}

練習(xí)14:publicclassDocumentation4{

publicinti-2,

privateintj=3;

publicstaticvoidmain(String[]args){

Dated=newDate();

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

}

I

練習(xí)15:publicclassHclloDocTesl(

publicstaticvoidmain(String|]args){

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

)

)

練習(xí)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。;

}

}

第三章

練習(xí)1:publicclassPrintTest(

publicstaticvoidmain(String[]args){

print("Hello,fromshortform.");

P.rintln("Hellofromgreggordonform.");

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

}

)

練習(xí)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);

}

}

練習(xí)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);

)

}

練習(xí)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);

)

)

練習(xí)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();

}

}

練習(xí)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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論