




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
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 meth
2、ods 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.
3、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 bo
4、olean, 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、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 constr
6、uctor. 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
7、 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 use
8、d 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 JFrame
9、s 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.pri
10、ntln(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 method
11、s 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 variabl
12、e, 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 val
13、ues. 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 pas
14、s 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
15、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 circl
16、e2 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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 預(yù)防擁擠踩踏班會(huì)課件
- 攜手抗疫守護(hù)健康
- 我為健康而來主題演講大綱
- 健康飲食產(chǎn)業(yè)園項(xiàng)目風(fēng)險(xiǎn)管理方案
- 電網(wǎng)側(cè)獨(dú)立儲(chǔ)能示范項(xiàng)目資金申請報(bào)告(參考)
- 2025年高效的鍋爐鼓、引風(fēng)機(jī)項(xiàng)目發(fā)展計(jì)劃
- 系統(tǒng)解剖學(xué)試題(附參考答案)
- 2025年環(huán)保節(jié)能型冷卻塔項(xiàng)目合作計(jì)劃書
- 物業(yè)管理企業(yè)財(cái)務(wù)管理規(guī)定
- 武漢體育學(xué)院附屬體育運(yùn)動(dòng)學(xué)校招聘真題
- 動(dòng)物園動(dòng)物飼養(yǎng)區(qū)的安全管理措施
- ICU醫(yī)院感染管理制度
- 小學(xué)生心理健康與輔導(dǎo)(第4版) 課件 第七章 小學(xué)生常見心理行為問題與輔導(dǎo)
- Unit 8 Section A (1a-1c) 教學(xué)設(shè)計(jì)2024-2025學(xué)年人教版八年級英語上冊
- 2025年3月版安全環(huán)境職業(yè)健康法律法規(guī)標(biāo)準(zhǔn)文件清單
- 《心律失?;颊叩淖o(hù)理指南》課件
- 《班組管理培訓(xùn)》課件
- 2025年個(gè)人資產(chǎn)出售與收購協(xié)議書
- 2025年度人力資源居間費(fèi)合同范本:人才招聘中介服務(wù)協(xié)議
- 污水處理廠設(shè)計(jì)中的安全措施與管理
- 血小板制備純度提升策略-洞察分析
評論
0/150
提交評論