thinking injava习题答案4.docx

上传人:b****2 文档编号:2252776 上传时间:2023-05-03 格式:DOCX 页数:16 大小:18.74KB
下载 相关 举报
thinking injava习题答案4.docx_第1页
第1页 / 共16页
thinking injava习题答案4.docx_第2页
第2页 / 共16页
thinking injava习题答案4.docx_第3页
第3页 / 共16页
thinking injava习题答案4.docx_第4页
第4页 / 共16页
thinking injava习题答案4.docx_第5页
第5页 / 共16页
thinking injava习题答案4.docx_第6页
第6页 / 共16页
thinking injava习题答案4.docx_第7页
第7页 / 共16页
thinking injava习题答案4.docx_第8页
第8页 / 共16页
thinking injava习题答案4.docx_第9页
第9页 / 共16页
thinking injava习题答案4.docx_第10页
第10页 / 共16页
thinking injava习题答案4.docx_第11页
第11页 / 共16页
thinking injava习题答案4.docx_第12页
第12页 / 共16页
thinking injava习题答案4.docx_第13页
第13页 / 共16页
thinking injava习题答案4.docx_第14页
第14页 / 共16页
thinking injava习题答案4.docx_第15页
第15页 / 共16页
thinking injava习题答案4.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

thinking injava习题答案4.docx

《thinking injava习题答案4.docx》由会员分享,可在线阅读,更多相关《thinking injava习题答案4.docx(16页珍藏版)》请在冰点文库上搜索。

thinking injava习题答案4.docx

thinkinginjava习题答案4

这是第5章的答案:

Chapter5

Notethatmanyoftheseexercisessimplytakeyouthroughthestepsofdiscoveringissueswithpackages.Thisiswhysomanyofthemarelefttothereader.

Exercise1

//:

c05:

E01_ImplicitImports.java

//+MjavaE01_ImplicitImports

/******************Exercise1******************

*WriteaprogramthatcreatesanArrayList

*objectwithoutexplicitlyimporting

*java.util.*.

***********************************************/

publicclassE01_ImplicitImports{

publicstaticvoidmain(Stringargs[]){

java.util.ArrayListal=

newjava.util.ArrayList();

}

}///:

~

Todothis,anyreferencestotheobjectmustbemadeusingitsfullpackagename.

Exercise2

//:

c05:

E02_LeftToReader.java

//+MjavaE02_LeftToReader

/******************Exercise2******************

*Inthesectionlabeled"package:

thelibrary

*unit,"turnthecodefragmentsconcerning

*mypackageintoacompilingandrunningsetof

*Javafiles.

***********************************************/

publicclassE02_LeftToReader{

publicstaticvoidmain(Stringargs[]){

System.out.println("Exerciselefttoreader");

}

}///:

~

Exercise3

//:

c05:

E03_LeftToReader.java

//+MjavaE03_LeftToReader

/******************Exercise3******************

*Inthesectionlabeled"Collisions,"takethe

*codefragmentsandturnthemintoaprogram,

*andverifythatcollisionsdoinfactoccur.

***********************************************/

publicclassE03_LeftToReader{

publicstaticvoidmain(Stringargs[]){

System.out.println("Exerciselefttoreader");

}

}///:

~

Exercise4

//:

c05:

E04_LeftToReader.java

//+MjavaE04_LeftToReader

/******************Exercise4******************

*GeneralizetheclassPdefinedinthischapter

*byaddingalltheoverloadedversionsof

*rint()andrintln()necessarytohandleall

*thedifferentbasicJavatypes.

***********************************************/

publicclassE04_LeftToReader{

publicstaticvoidmain(Stringargs[]){

System.out.println("Exerciselefttoreader");

}

}///:

~

Exercise5

//:

c05:

E05_LeftToReader.java

//+MjavaE05_LeftToReader

/******************Exercise5******************

*ChangetheimportstatementinTestAssert.java

*toenableanddisabletheassertionmechanism.

***********************************************/

publicclassE05_LeftToReader{

publicstaticvoidmain(Stringargs[]){

System.out.println("Exerciselefttoreader");

}

}///:

~

Exercise6

//:

c05:

E06_AccessControl.java

//+Mjavac05.E06_AccessControl

/******************Exercise6******************

*Createaclasswithpublic,private,

*protected,and"friendly"datamembersand

*methodmembers.Createanobjectofthisclass

*andseewhatkindofcompilermessagesyouget

*whenyoutrytoaccessalltheclassmembers.

*Beawarethatclassesinthesamedirectory

*arepartofthe"default"package.

***********************************************/

packagec05;

publicclassE06_AccessControl{

publicinta;

privateintb;

protectedintc;

intd;//"Friendly"

publicvoidf1(){}

privatevoidf2(){}

protectedvoidf3(){}

voidf4(){}//"Friendly"

publicstaticvoidmain(Stringargs[]){

E06_AccessControltest=

newE06_AccessControl();

//Noproblemaccessingeverythinginside

//ofmain()forthisclass,sincemain()

//isamemberandthereforehasaccess:

test.a=1;

test.b=2;

test.c=3;

test.d=4;

test.f1();

test.f2();

test.f3();

test.f4();

}

}///:

~

Youcanseethatmain()hasaccesstoeverythingbecauseit’samember.Ifyoucreateaseparateclasswithinthesamepackage,thatclasscannotaccesstheprivatemembers:

//:

c05:

E06_Other.java

//Aseparateclassinthesamepackagecannot

//accessprivateelements:

packagec05;

publicclassE06_Other{

publicE06_Other(){

E06_AccessControltest=

newE06_AccessControl();

test.a=1;

//!

test.b=2;//Can'taccess:

private

test.c=3;

test.d=4;

test.f1();

//!

test.f2();//Can'taccess:

private

test.f3();

test.f4();

}

}///:

~

Ifyoucreateaclassinaseparatepackage(whichyoucandoeitherbyexplicitlygivingapackagestatement,orbysimplyputtingitinadifferentdirectory)thenitisunabletoaccessanythingbutpublicmembers:

//:

c05:

other2:

E06_Other2.java

//Aseparateclassinthesamepackagecannot

//accessprivateelements:

packagec05.other2;

publicclassE06_Other2{

publicE06_Other2(){

c05.E06_AccessControltest=

newc05.E06_AccessControl();

test.a=1;

//!

test.b=2;//Can'taccess:

private

//!

test.c=3;//Can'taccess:

protected

//!

test.d=4;//Can'taccess:

friendly

test.f1();

//!

test.f2();//Can'taccess:

private

//!

test.f3();//Can'taccess:

protected

//!

test.f4();//Can'taccess:

friendly

}

}///:

~

Exercise7

//:

c05:

E07_ProtectedManipulation.java

//+MjavaE07_ProtectedManipulation

/******************Exercise7******************

*Createaclasswithprotecteddata.Createa

*secondclassinthesamefilewithamethod

*thatmanipulatestheprotecteddatainthe

*firstclass.

***********************************************/

classWithProtected{

protectedinti;

}

publicclassE07_ProtectedManipulation{

publicstaticvoidmain(Stringargs[]){

WithProtectedwp=newWithProtected();

wp.i=47;

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

}

}///:

~

Thepointofthisexerciseistoshowthatprotectedalsomeans“packageaccess”(aka“friendly”).Thatis,youcanaccessprotectedfieldswithinthesamepackage.Justtobesure,tryaddingaprotectedmethodtoWithProtectedandaccessingitfromwithinE07_ProtectedManipulation.

Exercise8

//:

c05:

E08_LeftToReader.java

//+MjavaE08_LeftToReader

/******************Exercise8******************

*ChangetheclassCookieasspecifiedinthe

*sectionlabeled"protected:

'sortof

*friendly.'"Verifythatbite()isnotpublic.

***********************************************/

publicclassE08_LeftToReader{

publicstaticvoidmain(Stringargs[]){

System.out.println("Exerciselefttoreader");

}

}///:

~

Justfollowtheinstructionsinthebook.

Exercise9

//:

c05:

E09_Widget.java

//+MjavaE09_Widget

/******************Exercise9******************

*Inthesectiontitled"Classaccess"you'll

*findcodefragmentsdescribingmyliband

*Widget.Createthislibrary,thencreatea

*Widgetinaclassthatisnotpartofthe

*mylibpackage.

***********************************************/

importmylib.Widget;

publicclassE09_Widget{

publicstaticvoidmain(Stringargs[]){

newWidget();

}

}///:

~

//:

c05:

mylib:

Widget.java

packagemylib;

publicclassWidget{

publicWidget(){

System.out.println("MakingaWidget");

}

}///:

~

Exercise10

//:

c05:

E10_LeftToReader.java

//+MjavaE10_LeftToReader

/******************Exercise10*****************

*CreateanewdirectoryandedityourCLASSPATH

*toincludethatnewdirectory.Copythe

*P.classfile(producedbycompiling

*com.bruceeckel.tools.P.java)toyournew

*directoryandthenchangethenamesofthe

*file,thePclassinsideandthemethodnames.

*(Youmightalsowanttoaddadditionaloutput

*towatchhowitworks.)Createanotherprogram

*inadifferentdirectorythatusesyournew

*class.

***********************************************/

publicclassE10_LeftToReader{

publicstaticvoidmain(Stringargs[]){

System.out.println("Exerciselefttoreader");

}

}///:

~

Exercise11

//:

c05:

E11_ConnectionManager.java

//+MjavaE11_ConnectionManager

/******************Exercise11*****************

*FollowingtheformoftheexampleLunch.java,

*createaclasscalledConnectionManagerthat

*managesafixedarrayofConnectionobjects.

*Theclientprogrammermustnotbeableto

*explicitlycreateConnectionobjects,butcan

*onlygetthemviaastaticmethodin

*ConnectionManager.WhentheConnectionManager

*runsoutofobjects,itreturnsanull

*reference.Testtheclassesinmain().

***********************************************/

importc05.connection.*;

publicclassE11_ConnectionManager{

publicstaticvoidmain(Stringargs[]){

Connectionc=

ConnectionManager.getConnection();

while(c!

=null){

System.out.println(c);

c.doSomething();

c=ConnectionManager.getConnection();

}

}

}///:

~

//:

c05:

connection:

Connection.java

packagec05.connection;

publicclassConnection{

privatestaticintcounter=0;

privateintid=counter++;

Connection(){}

publicStringtoString(){

return"Connection"+id;

}

publicvoiddoSomething(){}

}///:

~

//:

c05:

connection:

ConnectionManager.java

packagec05.connection;

publicclassConnectionManager{

privatestaticConnection[]pool=

newConnection[10];

privatestaticintcounter=0;

static{

for(inti=0;i

pool[i]=newConnection();

}

//Verysimple--justhandsouteachoneonce:

publicstaticConnectiongetConnection(){

if(counter

returnpool[counter++];

returnnull;

}

}///:

~

TheConnectionclassusesastaticintcalledcountertoautomaticallygiveeachConnectionobjectanidentifier,whichitwillproduceaspartofit’stoString()representation.ConnectionalsohasadoSomething()method,whichispresumablytheusefulworkthatmotivatesyoutousetheConnectionobject.

NotethattheconstructorforConnectionis“friendly,”soitisunavailableoutsideofthispackage.Thus,theclientprogrammerisunabletoaccessthatconstructorandcannotmakeinstancesofConnectiondirectly.TheonlywaytogetConnectionobjectsisthroughtheConnectionManager.

IntheConnectionManager,astaticarrayofobjectsisinitiali

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2