Java语言程序设计第九版第八章答案_第1页
Java语言程序设计第九版第八章答案_第2页
Java语言程序设计第九版第八章答案_第3页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

1、Chapter 8 Objects and Classes1. See the section "Defining Classes for Objects."2. The syntax to define a class ispublic class ClassName 3. The syntax to declare a reference variable for an object isClassName v;4. The syntax to create an object isnew ClassName();5. Constructors are special

2、kinds of methods that are called when creating an objectusing the new operator. Constructors do not have a return typenot even void.6. A class has a default constructor only if the class does not define any constructor.7. The member access operator is used to access a data field or invoke a method f

3、roman object.8. An anonymous object is the one that does not have a reference variable referencing it.9. A NullPointerException occurs when a null reference variable is used to access the members of an object.10. An array is an object. The default value for the elements of an array is 0 for numeric,

4、 false for boolean, u0000' for char, null for object element type.11. (a) There is such constructor ShowErrors(int) in the ShowErrors class.The ShowErrors class in the book has a default constructor. It is actually same aspublic class ShowErrors ShowErrors t = new ShowErrors(5);public ShowErrors

5、 () On Line 3, new ShowErrors(5) attempts to create an instance using a constructor ShowErrors(int), but the ShowErrors class does not have such a constructor. That is an error.(b) x() is not a method in the ShowErrors class.The ShowErrors class in the book has a default constructor. It is actually

6、same as public class ShowErrors public static void main(String args) ShowErrors t = new ShowErrors();t.x();public ShowErrors () On Line 4, t.x() is invoked, but the ShowErrors class does not have the method named x(). That is an error.(c) The program compiles fine, but it has a runtime error because

7、 variable c is null when the println statement is executed.(d) new C(5.0) does not match any constructors in class C. The program has a compilation error because class C does not have a constructor with a double argument.12. The program does not compile because new A() is used in class Test, but cla

8、ss Adoes not have a default constructor. See the second NOTE in the Section, “ Constructors.”13. false14. Use the Date 's n-aorg constructor to create a Date for the current time. Use the Date ' s toString() method to display a string representation for the Date.15. Use the JFrame 's -na

9、org constructor to create JFrame. Use the setTitle(String) method a set a title and use the setVisible(true) method to display the frame.16. Date is in java.util. JFrame and JOptionPane are in javax.swing. System and Math are in java.lang.17. System.out.println(f.i); Answer: CorrectSystem.out.printl

10、n(f.s); Answer: Correctf.imethod(); Answer: Correctf.smethod(); Answer: CorrectSystem.out.println(F.i); Answer: IncorrectSystem.out.println(F.s); Answer: CorrectF.imethod(); Answer: IncorrectF.smethod(); Answer: Correct18. Add static in the main method and in the factorial method because these two m

11、ethods don ' t need reference any instance objects or invoke any instance methods in the Test class.19. You cannot inv oke an in sta nee method or refere nee an in sta nee variable from a static method. You can inv oke a static method or refere nee a static variable from an in sta nee method? c

12、is an in sta nee variable, which cannot be accessed from the static eon text in method2.20. Accessor method is for retrieving private data value and mutator method is for changing private data value. The naming conven ti on for accessor method is getDataFieldName() for non- boolea n values and isDat

13、aFieldName() for boolea n values. The naming conven ti on for mutator method is setDataFieldName(value).21. Two ben efits: (1) for protect ing data and (2) for easy to mai nta in the class.22. Not a problem. Though radius is private, myCircle.radius is used in side the Circle class. Thus, it is fine

14、.23. Java uses “ pass by value ” to pass parameters to a metho© Whe n pass in variable of a primitive type to a method, the variable rema ins un cha nged after the method fini shes. However, whe n pass ing a variable of a refere nee type to a method, any cha nges to the object refere need by th

15、e variable in side the method areperma nent cha nges to the object refere need by the variable outside of the method. Both the actual parameter and the formal parameter variables refere nee to the same object.The output of the program is as follows:cou nt 101times 024.Remark: The referenee value of

16、circle1 is passed to x and the referenee value of circle2 ispassed to y. The contents of the objects are not swapped in the swap1 method. circle1 and circle2 are not swapped. To actually swap the contents of these objects, replace the following three linesCircle temp = x;x =y;y =temp; by double temp = x.radius; x.radius = y.radius;y.radius = temp;as in swap2.25. a. a0 = 1 a1 = 2b. a0 = 2 a1 = 1c. e1 = 2 e2 = 1d.

温馨提示

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

评论

0/150

提交评论