thinkinginjava習(xí)題答案1-_第1頁
thinkinginjava習(xí)題答案1-_第2頁
thinkinginjava習(xí)題答案1-_第3頁
thinkinginjava習(xí)題答案1-_第4頁
thinkinginjava習(xí)題答案1-_第5頁
已閱讀5頁,還剩132頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、這是第2章的答案:exercise 1/: c02:e01_helloworld.java/+m java e01_helloworld/* exercise 1 * * following the hellodate.java example in this * chapter, create a "hello, world" program that * simply prints out that statement. you need * only a single method in your class (the "main" * one t

2、hat gets executed when the program * starts). remember to make it static and to * include the argument list, even though you * don't use the argument list. compile the * program with javac and run it using java. if * you are using a different development * environment than the jdk, learn how to

3、compile * and run programs in that environment. */public class e01_helloworld public static void main(string args) system.out.println("hello, world!"); /:the +m directive tells my makefile builder tool to add this command (java helloworld) to the commands that it automatically runs while b

4、uilding the program. this way, if the execution fails for any reason then the make will abort and show that something is wrong for a particular program. this is a very simple form of built-in testing: automatically running the program to ensure no exceptions are thrown. youll see this on most of the

5、 programs in this solution guide, and the makefiles that come with the source code (that is packaged with this guide) will automatically run the programs.exercise 2/: c02:e02_atypename.java/+m java e02_atypename/* exercise 2 * * find the code fragments involving atypename * and turn them into a prog

6、ram that compiles and * runs. */public class e02_atypename public static void main(string args) e02_atypename a = new e02_atypename(); /:exercise 3/: c02:e03_dataonly.java/+m java e03_dataonly/* exercise 3 * * turn the dataonly code fragments into a * program that compiles and runs. */public class e

7、03_dataonly int i; float f; boolean b; public static void main(string args) e03_dataonly d = new e03_dataonly(); d.i = 47; d.f = 1.1f; d.b = false; /:exercise 4/: c02:e04_dataonly2.java/+m java e04_dataonly2/* exercise 4 * * modify exercise 3 so that the values of the * data in dataonly are assigned

8、 to and printed * in main(). */public class e04_dataonly2 int i; float f; boolean b; public static void main(string args) e04_dataonly2 d = new e04_dataonly2(); d.i = 47; system.out.println("d.i = " + d.i); d.f = 1.1f; system.out.println("d.f = " + d.f); d.b = false; system.out.p

9、rintln("d.b = " + d.b); /:exercise 5/: c02:e05_storage.java/+m java e05_storage/* exercise 5 * * write a program that includes and calls the * storage() method defined as a code fragment in * this chapter. */public class e05_storage string s = "hello, world!" int storage(string s

10、) return s.length() * 2; void print() system.out.println( "storage(s) = " + storage(s); public static void main(string args) e05_storage st = new e05_storage(); st.print(); /:you could have also made storage( ) a static method and just called it from main( ).exercise 6/: c02:e06_staticfun.

11、java/+m java e06_staticfun/* exercise 6 * * turn the staticfun code fragments into a * working program. */class statictest static int i = 47;public class e06_staticfun static void incr() statictest.i+; public static void main(string args) e06_staticfun sf = new e06_staticfun(); sf.incr(); e06_static

12、fun.incr(); /:exercise 7/: c02:e07_showargs.java/+m java e07_showargs a b c/* exercise 7 * * write a program that prints three arguments * taken from the command line. to do this, * you'll need to index into the command-line * array of strings. */public class e07_showargs public static void main

13、(string args) system.out.println(args0); system.out.println(args1); system.out.println(args2); /:note that youll get a run-time exception if you run the program without enough arguments. you should actually test for the length of the array first, like this:/: c02:e07_showargs2.java/+m java e07_showa

14、rgs2 a b c/* exercise 7 * * testing for the length of the array first. */public class e07_showargs2 public static void main(string args) if(args.length < 3) system.out.println("need 3 arguments"); system.exit(1); system.out.println(args0); system.out.println(args1); system.out.println(a

15、rgs2); /:the system.exit( ) call terminates the program and passes its argument back to the operating system as a result (with most operating systems, a non-zero result indicates that the program execution failed).exercise 8/: c02:e08_allthecolorsoftherainbow.java/+m java e08_allthecolorsoftherainbo

16、w/* exercise 8 * * turn the allthecolorsoftherainbow example into * a program that compiles and runs. */public class e08_allthecolorsoftherainbow int anintegerrepresentingcolors; void changethehueofthecolor(int newhue) anintegerrepresentingcolors = newhue; public static void main(string args) e08_al

17、lthecolorsoftherainbow all = new e08_allthecolorsoftherainbow(); all.changethehueofthecolor(27); /:exercise 9/: c02:e09_hellodate.java/+m java e09_hellodate/+m mkdir hellodate/+m javadoc e09_hellodate.java d hellodate/* exercise 9 * * find the code for the second version of * hellodate.java, which i

18、s the simple comment * documentation example. execute javadoc on the * file and view the results with your web * browser. */import java.util.*;/* the first thinking in java example program. * displays a string and today's date. * author bruce eckel * author www.brucee * version 2.0 */public clas

19、s e09_hellodate /* sole entry point to class & application * param args array of string arguments * return no return value * exception exceptions no exceptions thrown */ public static void main(string args) system.out.println("hello, it's: "); system.out.println(new date(); /:the c

20、ommand to run javadoc on the file and to place the results in a separate subdirectory called hellodate is embedded as the third +m comment directive above. note that javadoc doesnt automatically create the destination directory.exercise 10/: c02:e10_doctest.java/+m mkdir doctest/+m javadoc e10_docte

21、st.java d doctest/* exercise 10 * * turn doctest into a file that compiles and * then run it through javadoc. verify the * resulting documentation with your web browser. */* a class comment */public class e10_doctest /* a variable comment */ public int i; /* a method comment */ public void f() /:you

22、 must still execute javadoc and verify the results, but you can see the commands that are added to the makefile to do this, as +m comment directives. note that were only concerned that the file compiles, so theres no makefile directive to run it.exercise 11/: c02:e11_doctest2.java/+m mkdir doctest2/

23、+m javadoc e11_doctest2.java d doctest2/* exercise 11 * * add an html list of items to the documentation * in exercise 10. */* a class comment* <pre>* system.out.println(new date();* </pre>*/public class e11_doctest2 /* a variable comment */ public int i; /* a method comment * you can &l

24、t;em>even</em> insert a list: * <ol> * <li> item one * <li> item two * <li> item three * </ol> */ public void f() /:i simply added the html code fragments from the chapter examples.you must still execute javadoc and verify the results, but you can see the comma

25、nds that are added to the makefile to do this, as +m comment directives.exercise 12/: c02:e12_helloworlddoc.java/+m java e12_helloworlddoc/+m mkdir helloworlddoc/+m javadoc e12_helloworlddoc.java d helloworlddoc/* exercise 12 * * take the program in exercise 1 and add comment * documentation to it.

26、extract this comment * documentation into an html file using javadoc * and view it with your web browser. */* a first example from <i>thinking in java,* 2nd edition</i>. demonstrates the basic class* structure and the creation of a * <code>main()</code> function.*/public clas

27、s e12_helloworlddoc /* the <code>main()</code> function which is called when the program is executed by saying <code>java e12_helloworlddoc</code>. param args array passed from the command-line */ public static void main(string args) system.out.println("hello, world!&quo

28、t;); /:you must still execute javadoc and verify the results, but you can see the commands that are added to the makefile to do this, as +m comment directives.這是第3章的答案:chapter 3exercise 1/: c03:e01_precedence.java/+m java e01_precedence/* exercise 1 * * there are two expressions in the section * lab

29、eled "precedence" early in this chapter. * put these expressions into a program and * demonstrate that they produce different * results. */public class e01_precedence static int a, x = 40, y = 60, z = 10; public static void main(string args) a = x + y - 2/2 + z; system.out.println(a); a =

30、x + (y - 2)/(2 + z); system.out.println(a); /:results are 109 & 44. the difference is because the default order of evaluation is changed by the use of the parentheses.exercise 2/: c03:e02_ternary.java/+m java e02_ternary/* exercise 2 * * put the methods ternary() and alternative() * into a worki

31、ng program. */public class e02_ternary static int ternary(int i) return i < 10 ? i * 100 : i * 10; static int alternative(int i) if (i < 10) return i * 100; else return i * 10; public static void main(string args) system.out.println(ternary(9); system.out.println(ternary(11); system.out.printl

32、n(alternative(9); system.out.println(alternative(11); /:exercise 3/: c03:e03_ifelse3.java/+m java e03_ifelse3/* exercise 3 * * from the sections labeled "if-else" and * "return", modify the two test() methods so * that testval is tested to see if it is within * the range between

33、(and including) the * arguments begin and end. (this is a change to * the exercise in the printed version of the * book, which was in error). */public class e03_ifelse3 static boolean test(int testval, int begin, int end) boolean result = false; if(testval >= begin && testval <= end) r

34、esult = true; return result; public static void main(string args) system.out.println(test(10, 5, 15); system.out.println(test(5, 10, 15); system.out.println(test(5, 5, 5); /:since the test( ) methods are now only testing for two conditions, i took the liberty of changing the return value to boolean.

35、 by using return in the following program, notice that no intermediate result variable is necessary:/: c03:e03_ifelse4.java/+m java e03_ifelse4/ no intermediate 'result' value necessary:public class e03_ifelse4 static boolean test(int testval, int begin, int end) if(testval >= begin &

36、& testval <= end) return true; return false; public static void main(string args) system.out.println(test(10, 5, 15); system.out.println(test(5, 10, 15); system.out.println(test(5, 5, 5); /:exercise 4/: c03:e04_to100.java/+m java e04_to100/* exercise 4 * * write a program that prints values f

37、rom one to * 100. */public class e04_to100 public static void main(string args) for(int i = 1; i <= 100; i+) system.out.println(i); /:this is the most trivial use of a for loop.exercise 5/: c03:e05_break47.java/+m java e05_break47/* exercise 5 * * modify exercise 4 so that the program exits by *

38、using the break keyword at value 47. try using * return instead. */public class e05_break47 public static void main(string args) for(int i = 1; i <= 100; i+) system.out.println(i); / if(i = 47) break; if(i = 47) return; /:the above example includes a commented line of code to use break, as well.

39、the break will exit the for loop, which puts it at the end of main( ), whereas return exits directly from the current method, which happens to be main( ).exercise 6/: c03:e06_comparestrings.java/+m java e06_comparestrings/* exercise 6 * * write a function that takes two string * arguments, and uses

40、all the boolean * comparisons to compare the two strings and * print the results. for the = and !=, also * perform the equals() test. in main(), call * your function with some different string * objects. */public class e06_comparestrings public static void p(string s, boolean b) system.out.println(s

41、 + ": " + b); public static void compare(string lval, string rval) /! p("lval < rval: " + lval < rval); /! p("lval > rval: " + lval > rval); /! p("lval <= rval: " + lval <= rval); /! p("lval >= rval: " + lval >= rval); p("

42、;lval = rval", lval = rval); p("lval != rval", lval != rval); p("lval.equals(rval)", lval.equals(rval); public static void main(string args) compare("hello", "hello"); string s = new string("hello"); compare("hello", s); compare("

43、hello", "goodbye"); /:this is a bit of a trick question, because the only comparisons that actually compile are = and !=. but it also points out the important difference between the = and != operators, which compare references, and equals( ), which actually compares content.the output

44、 of this program is:lval = rval: truelval != rval: falselval.equals(rval): truelval = rval: falselval != rval: truelval.equals(rval): truelval = rval: falselval != rval: truelval.equals(rval): falseremember that quoted character arrays also produce references to string objects. in the first case, th

45、e compiler is smart enough to recognize that the two strings actually contain the same values. because string objects are immutable you cannot change their contents the compile can merge the two string objects into one. this is why = returns true in that case. however, when a separate string s is cr

46、eated, even though it has the same contents, a distinct object is created and therefor the = returns false. the only reliable way to compare objects for equality is with equals( ); be wary if you see a comparison using =, which always compares to see if two references are identical (that is, whether

47、 they point to the same object).exercise 7/: c03:e07_randomints.java/+m java e07_randomints/* exercise 7 * * write a program that generates 25 random int * values. for each value, use an if-else * statement to classify it as greater than, less * than or equal to a second randomly-generated * value.

48、*/import java.util.*;public class e07_randomints static random r = new random(); public static void comparerand() int a = r.nextint(); int b = r.nextint(); system.out.println("a = " + a + ", b = " + b); if(a < b) system.out.println("a < b"); else if (a > b) sys

49、tem.out.println("a > b"); else system.out.println("a = b"); public static void main(string args) for(int i = 0; i < 25; i+) comparerand(); /:part of this is an exercise in standard java library usage. if you havent become familiar with the html documentation for the jdk, do

50、wnloadable from , do so now. if you go to the index, and select “r” you will quickly come across the various choices for generating random numbers. above you see one solution to the exercise. here, i created a method that generates the random numbers and compares them, and then i just call that method 25 times. in your solution, you may have created all the code in line, inside mai

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論