JAVA编程思想第四版第二章_第1页
JAVA编程思想第四版第二章_第2页
JAVA编程思想第四版第二章_第3页
JAVA编程思想第四版第二章_第4页
JAVA编程思想第四版第二章_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、JAVA编程思想第四版第二章习题与答案 分类: 数据结构 JAVAJAVA编程思想2011-11-03 09:58276人阅读评论(0)收藏举报java编程microsofttreeclassstring(1) 参照本章的第一个例子,创建一个“Hello,World”程序,在屏幕上简单地显示这句话。注意在自己的类里只需一个方法(“main”方法会在程序启动时执行)。记住要把它设为static形式,并置入自变量列表即使根本不会用到这个列表。用javac编译这个程序,再用java运行它。 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) 写一个程序,打印出从命令行获取的三个自变量。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. /输出当前文件两倍长度 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. / /输出当前文件两倍长度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.1234545; 18. test.c=true; 19. test.show(); 20. 21. 22

12、.23. /* 24. output: 25. 234 26. 2.1234545 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.1234545;test.c

13、=true;test.show();/*output:2342.1234545true/(6)java view plaincopyprint?1. /精度设置,封装调用 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.pri

14、ntln(c); 13. 14. 15.16. DataOnly test=new DataOnly(); 17. test.a=20; 18. test.b=3.141592653; 19. test.c=true; 20. test.show(); 21. 22. 23.24. /* 25. output: 26. 20 27. 3.141592653 28. true 29. */ /精度设置,封装调用public class DataOnlyTestpublic static void main(String args)class DataOnlyint a;double b;bool

15、ean c;void show()System.out.println(a);System.out.println(b);System.out.println(c);DataOnly test=new DataOnly();test.a=20;test.b=3.141592653;test.c=true;test.show();/*output:203.141592653true*/(7)java view plaincopyprint?1. public class ATNTest 2. public static void main(String args) 3. class ATypeN

16、ame 4. int i; 5. double d; 6. boolean b; 7. void show() 8. 9. System.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

17、. */ public class ATNTestpublic static void main(String args)class 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. /

18、数据元素测试 2. public class Primitive 3. static int i; 4. static char 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. */ /数据元素测试public class Primitivestatic int i;static char c;public st

19、atic void main(String args)System.out.println(int=+i);System.out.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 R

20、ainBow 6. public static void main(String args) 7. AllTheColorsOfTheRainbow 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 c

21、hange, atc.anIntegerRepresentingColors = + atc.anIntegerRepresentingColors); 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

22、changeColor(int newColor) 23. return anIntegerRepresentingColors = 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) AllTheColorsOfTheRainb

23、ow atc = new AllTheColorsOfTheRainbow();System.out.println(atc.anIntegerRepresentingColors = + atc.anIntegerRepresentingColors);atc.changeColor(7);atc.changeTheHueOfTheColor(77);System.out.println(After color change, atc.anIntegerRepresentingColors = + atc.anIntegerRepresentingColors);System.out.pri

24、ntln(atc.hue = + atc.hue);class AllTheColorsOfTheRainbow int anIntegerRepresentingColors = 0;int hue = 0;void changeTheHueOfTheColor(int newHue) hue = newHue;int changeColor(int newColor) return anIntegerRepresentingColors = newColor;Microsoft Windows 版本 6.1.7600版权所有 (c) 2009 Microsoft Corporation。保

25、留所有权利。D:javac RainBow.javaD:java RainBowatc.anIntegerRepresentingColors = 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 cl

26、ass Test 10. public static void main(String args) 11. System.out.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.p

27、rintln(After Incrementable increment() called:); 18. System.out.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 StaticTe

28、st,st3); 25. StaticTest st3=new StaticTest(); 26. System.out.println(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=n

29、ew StaticTest();StaticTest st2=new StaticTest();System.out.println(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

30、st1.i=3,);System.out.println(st1.i=+st1.i);System.out.println(st1.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版权所有 (c) 2009 Microsoft Corporation。保留所有权利。D:javac Test.javaD:java TestStaticTes

31、t.i=47st1.i=47st2.i=47After Incrementable increment() called:st1.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

32、 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(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论