資源描述:
《thinkinginjava習(xí)題答案1-8》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、這是第2章的答案:Exercise1//:c02:E01_HelloWorld.java//+MjavaE01_HelloWorld/******************Exercise1*******************FollowingtheHelloDate.javaexampleinthis*chapter,createa"hello,world"programthat*simplyprintsoutthatstatement.Youneed*onlyasinglemethodinyourclass(the"main
2、"*onethatgetsexecutedwhentheprogram*starts).Remembertomakeitstaticandto*includetheargumentlist,eventhoughyou*don'tusetheargumentlist.Compilethe*programwithjavacandrunitusingjava.If*youareusingadifferentdevelopment*environmentthantheJDK,learnhowtocompile*andrunprogram
3、sinthatenvironment.***********************************************/publicclassE01_HelloWorld{publicstaticvoidmain(Stringargs[]){System.out.println("Hello,world!");}}///:~The+Mdirectivetellsmymakefilebuildertooltoaddthiscommand(javaHelloWorld)tothecommandsthatitautoma
4、ticallyrunswhilebuildingtheprogram.Thisway,iftheexecutionfailsforanyreasonthenthemakewillabortandshowthatsomethingiswrongforaparticularprogram.Thisisaverysimpleformofbuilt-intesting:automaticallyrunningtheprogramtoensurenoexceptionsarethrown.You’llseethisonmostofthep
5、rogramsinthissolutionguide,andthemakefilesthatcomewiththesourcecode(thatispackagedwiththisguide)willautomaticallyruntheprograms.Exercise2//:c02:E02_ATypeName.java//+MjavaE02_ATypeName/******************Exercise2*******************FindthecodefragmentsinvolvingATypeNam
6、e*andturnthemintoaprogramthatcompilesand*runs.***********************************************/publicclassE02_ATypeName{publicstaticvoidmain(Stringargs[]){E02_ATypeNamea=newE02_ATypeName();}}///:~Exercise3//:c02:E03_DataOnly.java//+MjavaE03_DataOnly/******************
7、Exercise3*******************TurntheDataOnlycodefragmentsintoa*programthatcompilesandruns.***********************************************/publicclassE03_DataOnly{inti;floatf;booleanb;publicstaticvoidmain(String[]args){E03_DataOnlyd=newE03_DataOnly();d.i=47;d.f=1.1f;d.
8、b=false;}}///:~Exercise4//:c02:E04_DataOnly2.java//+MjavaE04_DataOnly2/******************Exercise4*******************ModifyExercise