JAVA編程思想第四版第二章_第1頁
JAVA編程思想第四版第二章_第2頁
JAVA編程思想第四版第二章_第3頁
JAVA編程思想第四版第二章_第4頁
JAVA編程思想第四版第二章_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、JAVA編程思想第四版第二章習(xí)題與答案 分類: 數(shù)據(jù)結(jié)構(gòu) JAVAJAVA編程思想2011-11-03 09:58276人閱讀評論(0)收藏舉報(bào)java編程microsofttreeclassstring(1) 參照本章的第一個(gè)例子,創(chuàng)建一個(gè)“Hello,World”程序,在屏幕上簡單地顯示這句話。注意在自己的類里只需一個(gè)方法(“main”方法會(huì)在程序啟動(dòng)時(shí)執(zhí)行)。記住要把它設(shè)為static形式,并置入自變量列表即使根本不會(huì)用到這個(gè)列表。用javac編譯這個(gè)程序,再用java運(yùn)行它。 java view plaincopyprint?1. public class HelloWorld 2.

2、3. public static void main(String args) 4. 5. System.out.println(Hello,World); 6. 7. public class HelloWorldpublic static void main(String args)System.out.println(Hello,World);(2) 寫一個(gè)程序,打印出從命令行獲取的三個(gè)自變量。java view plaincopyprint?1. public class GetArgs 2. 3. public static void main(String args) 4. 5.

3、System.out.println(args0); 6. System.out.println(args1); 7. System.out.println(args2); 8. 9. 10. /java GetArgs a 3 12 public class GetArgspublic static void main(String args)System.out.println(args0);System.out.println(args1);System.out.println(args2);/java GetArgs a 3 12(3)java view plaincopyprint?

4、1. class Tree 2.3. int height; 4.5. Tree() 6. System.out.println(Planting a seedling); 7. height=0; 8. 9.10. Tree(int initialHeight) 11. height =initialHeight; 12. System.out.println(Creating new tree that is +height+ feet tall); 13. 14.15. void info() 16. 17. System.out.println(Tree is +height+ fee

5、t tall); 18. 19.20. void info(String s) 21. System.out.println(s+:Tree is +height+ feettall); 22. 23. 24.25. public class OverLoading 26. public static void main(String args) 27. new Tree(); 28. for (int i=0;i5;i+) 29. Tree t=new Tree(i); 30. (); 31. (overloading method); 32. 33.34. 35.

6、class Treeint height;Tree()System.out.println(Planting a seedling);height=0;Tree(int initialHeight)height =initialHeight;System.out.println(Creating new tree that is +height+ feet tall);void info()System.out.println(Tree is +height+ feet tall);void info(String s)System.out.println(s+:Tree is +height

7、+ feettall);public class OverLoadingpublic static void main(String args)new Tree();for (int i=0;ijavac OverLoading.javaD:java OverLoadingPlanting a seedlingCreating new tree that is 0 feet tallTree is 0 feet talloverloading method:Tree is 0 feettallCreating new tree that is 1 feet tallTree is 1 feet

8、 talloverloading method:Tree is 1 feettallCreating new tree that is 2 feet tallTree is 2 feet talloverloading method:Tree is 2 feettallCreating new tree that is 3 feet tallTree is 3 feet talloverloading method:Tree is 3 feettallCreating new tree that is 4 feet tallTree is 4 feet talloverloading meth

9、od:Tree is 4 feettallD:(4)java view plaincopyprint?1. /輸出當(dāng)前文件兩倍長度 2. public class Demo 3. public static void main(String args) 4. class StoreStuff 5. int storage(String s) 6. return s.length() * 2; 7. 8. 9. StoreStuff x = new StoreStuff(); 10. System.out.println(x.storage(hi); 11. 12. 13. /* 14. out

10、put: 15. 4 16. / /輸出當(dāng)前文件兩倍長度public class Demo public static void main(String args) class StoreStuff int storage(String s) return s.length() * 2;StoreStuff x = new StoreStuff();System.out.println(x.storage(hi);/*output:4/(5)java view plaincopyprint?1. public class DataOnlyTestTwo 2. public static voi

11、d main(String args) 3. class DataOnly 4. int a; 5. double b; 6. boolean c; 7. void show() 8. 9. System.out.println(a); 10. System.out.println(b); 11. System.out.println(c); 12. 13. 14.15. DataOnly test=new DataOnly(); 16. test.a=234; 17. test.b=2.; 18. test.c=true; 19. test.show(); 20. 21. 22.23. /*

12、 24. output: 25. 234 26. 2. 27. true 28. / public class DataOnlyTestTwopublic static void main(String args)class DataOnlyint a;double b;boolean c;void show()System.out.println(a);System.out.println(b);System.out.println(c);DataOnly test=new DataOnly();test.a=234;test.b=2.;test.c=true;test.show();/*o

13、utput:2342.true/(6)java view plaincopyprint?1. /精度設(shè)置,封裝調(diào)用 2. public class DataOnlyTest 3. public static void main(String args) 4. class DataOnly 5. int a; 6. double b; 7. boolean c; 8. void show() 9. 10. System.out.println(a); 11. System.out.println(b); 12. System.out.println(c); 13. 14. 15.16. Data

14、Only test=new DataOnly(); 17. test.a=20; 18. test.b=3.; 19. test.c=true; 20. test.show(); 21. 22. 23.24. /* 25. output: 26. 20 27. 3. 28. true 29. */ /精度設(shè)置,封裝調(diào)用public class DataOnlyTestpublic static void main(String args)class DataOnlyint a;double b;boolean c;void show()System.out.println(a);System.

15、out.println(b);System.out.println(c);DataOnly test=new DataOnly();test.a=20;test.b=3.;test.c=true;test.show();/*output:203.true*/(7)java view plaincopyprint?1. public class ATNTest 2. public static void main(String args) 3. class ATypeName 4. int i; 5. double d; 6. boolean b; 7. void show() 8. 9. Sy

16、stem.out.println(i); 10. System.out.println(d); 11. System.out.println(b); 12. 13. 14.15. ATypeName a=new ATypeName(); 16. a.i=3; 17. a.d=2.71828; 18. a.b=false; 19. a.show(); 20. 21. 22.23. /* 24. output: 25. 3 26. 2.71828 27. false 28. */ public class ATNTestpublic static void main(String args)cla

17、ss ATypeName int i;double d;boolean b;void show()System.out.println(i);System.out.println(d);System.out.println(b);ATypeName a=new ATypeName();a.i=3;a.d=2.71828;a.b=false;a.show();/*output:32.71828false*/(8)java view plaincopyprint?1. /數(shù)據(jù)元素測試 2. public class Primitive 3. static int i; 4. static char

18、 c; 5.6. public static void main(String args) 7. System.out.println(int=+i); 8. System.out.println(char=+c); 9. 10. 11.12. /* 13. output: 14. int = 0 15. char = 16.17. */ /數(shù)據(jù)元素測試public class Primitivestatic int i;static char c;public static void main(String args)System.out.println(int=+i);System.out

19、.println(char=+c);/*output:int = 0char =*/(9)java view plaincopyprint?1. / object/Rainbow.java 2. / TIJ4 Chapter Object, Exercise 11, page 90 3. / Turn the AllColorsOfTheRainbow into a program that compiles and runs. 4.5. public class RainBow 6. public static void main(String args) 7. AllTheColorsOf

20、TheRainbow atc = new AllTheColorsOfTheRainbow(); 8. System.out.println(atc.anIntegerRepresentingColors = + atc.anIntegerRepresentingColors); 9. atc.changeColor(7); 10. atc.changeTheHueOfTheColor(77); 11. System.out.println(After color change, atc.anIntegerRepresentingColors = + atc.anIntegerRepresen

21、tingColors); 12. System.out.println(atc.hue = + atc.hue); 13. 14. 15.16. class AllTheColorsOfTheRainbow 17. int anIntegerRepresentingColors = 0; 18. int hue = 0; 19. void changeTheHueOfTheColor(int newHue) 20. hue = newHue; 21. 22. int changeColor(int newColor) 23. return anIntegerRepresentingColors

22、 = newColor; 24. 25. / object/Rainbow.java/ TIJ4 Chapter Object, Exercise 11, page 90/ Turn the AllColorsOfTheRainbow into a program that compiles and runs.public class RainBow public static void main(String args) AllTheColorsOfTheRainbow atc = new AllTheColorsOfTheRainbow();System.out.println(atc.a

23、nIntegerRepresentingColors = + atc.anIntegerRepresentingColors);atc.changeColor(7);atc.changeTheHueOfTheColor(77);System.out.println(After color change, atc.anIntegerRepresentingColors = + atc.anIntegerRepresentingColors);System.out.println(atc.hue = + atc.hue);class AllTheColorsOfTheRainbow int anI

24、ntegerRepresentingColors = 0;int hue = 0;void changeTheHueOfTheColor(int newHue) hue = newHue;int changeColor(int newColor) return anIntegerRepresentingColors = newColor;Microsoft Windows 版本 6.1.7600版權(quán)所有 (c) 2009 Microsoft Corporation。保留所有權(quán)利。D:javac RainBow.javaD:java RainBowatc.anIntegerRepresentin

25、gColors = 0After color change, atc.anIntegerRepresentingColors = 7atc.hue = 77D:(10)java view plaincopyprint?1. class StaticTest 2. static int i=47; 3. 4.5. class Increamentable 6. static void increment()StaticTest.i+; 7. 8.9. public class Test 10. public static void main(String args) 11. System.out

26、.println(StaticTest.i=+StaticTest.i); 12. StaticTest st1=new StaticTest(); 13. StaticTest st2=new StaticTest(); 14. System.out.println(st1.i=+st1.i); 15. System.out.println(st2.i=+st2.i); 16. Increamentable.increment(); 17. System.out.println(After Incrementable increment() called:); 18. System.out.

27、println(st1.i=+st1.i); 19. System.out.println(st2.i=+st2.i); 20. st1.i=3; 21. System.out.println(After st1.i=3,); 22. System.out.println(st1.i=+st1.i); 23. System.out.println(st1.i=+st1.i); 24. System.out.println(Create another StaticTest,st3); 25. StaticTest st3=new StaticTest(); 26. System.out.pri

28、ntln(st3.i=+st3.i); 27.28. 29. class StaticTeststatic int i=47;class Increamentablestatic void increment()StaticTest.i+;public class Testpublic static void main(String args)System.out.println(StaticTest.i=+StaticTest.i);StaticTest st1=new StaticTest();StaticTest st2=new StaticTest();System.out.print

29、ln(st1.i=+st1.i);System.out.println(st2.i=+st2.i);Increamentable.increment();System.out.println(After Incrementable increment() called:);System.out.println(st1.i=+st1.i);System.out.println(st2.i=+st2.i);st1.i=3;System.out.println(After st1.i=3,);System.out.println(st1.i=+st1.i);System.out.println(st

30、1.i=+st1.i);System.out.println(Create another StaticTest,st3);StaticTest st3=new StaticTest();System.out.println(st3.i=+st3.i);Microsoft Windows 版本 6.1.7600版權(quán)所有 (c) 2009 Microsoft Corporation。保留所有權(quán)利。D:javac Test.javaD:java TestStaticTest.i=47st1.i=47st2.i=47After Incrementable increment() called:st1

31、.i=48st2.i=48After st1.i=3,st1.i=3st1.i=3Create another StaticTest,st3st3.i=3D:(11)java view plaincopyprint?1. public class test 2. public static void main(String args) 3. boolean b=false; 4. char c=x; 5. byte t=8; 6. short s=16; 7. int i=32; 8. long l=64; 9. float f=0.32f; 10. double d=0.64; 11. Boolean B=b; 12. System.out.println(boolean b=+b); 13. System.out.println(Boolean B=+B); 14. Character C=c; 15. System.out.println(char c=+c); 16. System.out.println(CharacterC=+C); 17. Byte T=t; 18. System.out.println(byte t=+t); 19. System.out.println(Byte T

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論