



全文預(yù)覽已結(jié)束
下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
Chapter 8 Objects and Classes1.See the section Declaring and Creating Objects.2.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.3. 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.4. (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.5. 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.”6. false7. 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.8. 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.9. Date is in java.util. JFrame and JOptionPane are in javax.swing. System and Math are in java.lang.10.System.out.println(f.i);Answer: CorrectSystem.out.println(f.s);Answer: Correctf.imethod();Answer: Correctf.smethod();Answer: CorrectSystem.out.println(Foo.i);Answer: IncorrectSystem.out.println(Foo.s);Answer: CorrectFoo.imethod();Answer: IncorrectFoo.smethod();Answer: Correct11.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.12. 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. 13. 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).14. Two benefits: (1) for protecting data and (2) for easy to maintain the class.15. Yes. Though radius is private, myCircle.radius is used inside the Circle class. Thus, it is fine.16.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 017. 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.18. a. a0 = 1 a1 = 2b. a0 = 2 a1 = 1c. e1 = 2 e2 = 1d. t1
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 丙舒硫胺行業(yè)深度研究分析報(bào)告(2024-2030版)
- 2022-2027年中國(guó)車載發(fā)射器電商行業(yè)發(fā)展監(jiān)測(cè)及投資戰(zhàn)略咨詢報(bào)告
- 2024-2030全球心臟分流裝置行業(yè)調(diào)研及趨勢(shì)分析報(bào)告
- 農(nóng)機(jī)安全工作總結(jié)
- 2020-2025年中國(guó)AFC檢票機(jī)行業(yè)發(fā)展趨勢(shì)及投資前景預(yù)測(cè)報(bào)告
- 企業(yè)消防安全管理制度完整版
- 駐地安全檢查表
- 通遼市建設(shè)工程質(zhì)量安全監(jiān)督造價(jià)管理站
- 防溺水交通安全心得體會(huì)
- 2022-2027年中國(guó)花草茶行業(yè)發(fā)展監(jiān)測(cè)及投資戰(zhàn)略咨詢報(bào)告
- 中國(guó)IBD藍(lán)皮書(shū)-中國(guó)炎癥性腸病醫(yī)患認(rèn)知暨生存質(zhì)量報(bào)告:克羅恩病部分
- 2025至2030中國(guó)電動(dòng)踏板車行業(yè)項(xiàng)目調(diào)研及市場(chǎng)前景預(yù)測(cè)評(píng)估報(bào)告
- 中國(guó)公安信息化市場(chǎng)前景預(yù)測(cè)及未來(lái)發(fā)展趨勢(shì)報(bào)告
- 專精特新企業(yè)課件
- 四川省南充市四校聯(lián)考2025年英語(yǔ)八下期末質(zhì)量檢測(cè)試題含答案
- JJG 621-2012 液壓千斤頂行業(yè)標(biāo)準(zhǔn)
- 上期開(kāi)特下期出特公式
- 杭州市普通住宅物業(yè)菜單式服務(wù)參考收費(fèi)標(biāo)準(zhǔn)(試行)
- 鋼材標(biāo)準(zhǔn)-SAEAMS5662M-2004鎳基高溫合金[1]._pdf
- 建筑工程內(nèi)外墻面噴漿、粘釘、掛網(wǎng)施工合同(共2頁(yè))
- DP備份軟件日常維護(hù)手冊(cè)
評(píng)論
0/150
提交評(píng)論