免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Chapter 8 Objects and Classes1.See the section Defining Classes for Objects.2.The syntax to define a class is public class ClassName 3. The syntax to declare a reference variable for an object isClassName v;4. The syntax to create an object is new ClassName();5.Constructors are special kinds of methods that are called when creating an object using 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 from an 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, 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 as public class ShowErrors public static void main(String args) ShowErrors t = new ShowErrors(5); public ShowErrors () 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 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 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 class A does not have a default constructor. See the second NOTE in the Section, “Constructors.”13. false14. Use the Dates no-arg constructor to create a Date for the current time. Use the Dates toString() method to display a string representation for the Date.15. Use the JFrames no-arg 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.println(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 methods dont need reference any instance objects or invoke any instance methods in the Test class.19. You cannot invoke an instance method or reference an instance variable from a static method. You can invoke a static method or reference a static variable from an instance method? c is an instance variable, which cannot be accessed from the static context in method2. 20. Accessor method is for retrieving private data value and mutator method is for changing private data value. The naming convention for accessor method is getDataFieldName() for non-boolean values and isDataFieldName() for boolean values. The naming convention for mutator method is setDataFieldName(value).21. Two benefits: (1) for protecting data and (2) for easy to maintain the class.22. Not a problem. Though radius is private, myCircle.radius is used inside the Circle class. Thus, it is fine.23.Java uses “pass by value” to pass parameters to a method. When passing a variable of a primitive type to a method, the variable remains unchanged after the method finishes. However, when passing a variable of a reference type to a method, any changes to the object referenced by the variable inside the method are permanent changes to the object referenced by the variable outside of the method. Both the actual parameter and the formal parameter variables reference to the same object.The output of the program is as follows:count 101times 024. Remark: The reference value of circle1 is passed to x and the reference value of circle2 is passed 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;bydouble temp = x.radius;x.radius = y.radius;y.radius = temp;as in swap2.25. a. a0 = 1 a1 = 2b. a0 =
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025标准农田租赁合同模板
- 2025年家具买卖合同范本
- 2025房屋买卖合同书范本
- 2025房屋买卖合同范本下载
- 2025装载机租赁合同
- DB12T 733-2017 天津市行政许可事项操作规程 设置医疗机构许可和执业登记、变更-设置医疗机构许可-设置内资医疗机构许可
- DB11T 1053.1-2013 实验用鱼 第1部分:微生物学等级及监测
- 2025年油井作业考试题库及答案
- 2025电解铜国内购销合同
- 2025-2030中国液态化工品仓储物流行业竞争格局与未来预测报告
- 学院“十五五”大学文化建设规划
- 铜排、铝排载流量安及铜排载流计算
- 文学概论 第二版 课件第六章 文学风格、文学流派与文学思潮
- 《创意书籍》教学课件-2024-2025学年冀美版(2024)初中美术七年级上册
- 水利安全风险防控“六项机制”与安全生产培训
- TCPQSXF006-2023消防水带产品维护更换及售后服务
- 架空索道工程技术标准
- 村级财务培训内容课件
- 电力企业应收账款风险管理与控制策略
- 中职高教版(2023)语文职业模块-第七单元语文综合实践-走进传统节日-探寻文化根脉【课件】
- GB/T 45025-2024珊瑚礁生态修复监测和效果评估技术指南
评论
0/150
提交评论