20172308《程序設計與數據結構》第四周學習總結

20172308 2017-2018-2 《Java程序設計》第三週學習總結

教材學習內容總結

  • 1.類與對象:
    如何編寫一個類,如何設置訪問、修改方式(修飾符:public,private)
    類的編寫:構造方法,方法聲明,參數,return語句,實例數據,以及幫助建模的UML。
  • 2.靜態類,靜態方法
  • 3.類間關係:
    依賴關係(一個類引用另外一個類的方法,需根據方法是否爲靜態)
    聚合關係(一個類引用另外一個類的屬性)
  • 4.接口:特殊的類,Comparable, Iterator
  • 5.枚舉類型,方法重載(方法名可相同,形參不一樣)

教材學習中的問題和解決過程

  • 問題1:書上說:「常將一個類保存爲一個文件」。當時的疑問是:那麼運行程序的時候是要把所在的文件夾都搜一遍的嗎?java

  • 問題2:方法聲明首部的形參名,是建立的局部變量嗎?爲何重複命名的也能夠用?
  • 問題2解決方案:形參是僅當方法調用時纔會存在的,在方法調用結束時消亡。git

  • 問題3:書上說:「對於類的全部對象,只存在一個靜態變量實體。所以,在一個對象中改變靜態變量的值將直接影響其餘對象。」
  • 問題3解決方案:

    如圖:當改變hyt時,gk的值也會跟着改變。編程

  • 問題4:接口的意義在哪裏?既然要把要用的方法都寫出來,那不如不要這個接口嘍
  • 問題4解決方案:百度:沒法保證一個接口就一個類去實現,多個類去實現同一個接口,程序怎麼知道他們是有關聯的?(多重繼承是被禁止的)安全

代碼調試中的問題和解決過程

  • 問題1:寫了一段代碼,編譯的時候出現瞭如圖錯誤:
    app

  • 問題1解決方案:通過一番嘗試和對書上例題的參考,發現了錯誤所在:在編寫類的時候,構造方法中的參數(形參),寫了好幾個(當時還不知道括號裏寫這個是幹嗎用的),而在寫驅動類的程序中實例化的時候沒有按照格式寫好參數致使的錯誤,改正後的代碼如圖:
    less

  • 問題2:在寫一個修改器方法的時候,我像書上同樣設置成了不返回值型void,而我後來寫的驅動類程序的時候,須要把設置的數值用來計算,結果是這樣:
    dom

  • 問題2解決方案:找不到符號,屢次嘗試以後,將void去掉了,設置了返回類型,結果成功編譯。可是輸出的結果並非預期結果,如圖:

    當前直徑是用修改器方法設置好的,也返回出來了,表面積也是計算出來了,只有體積算不出來。想了一下,把程序中要除以的6改爲了6.0,成功運行了。
    (記錄一下這個小失誤)iphone

  • 問題3:跟上面的出現了類似的狀況,找不到符號,如圖:
    tcp

  • 問題3解決方法:跟上面的狀況不一樣,此次是在寫實際參數的時候,字符串沒加引號。ide

  • 問題4:在運行結果的時候,要獲得的車名,型號的結果顯示爲「null」,如圖:

    這兩個的結果是用本身寫的類的獲取方法獲得的,驅動類的代碼如圖:
  • 問題4解決方法:後來試了一下修改方法,是能夠的。驅動程序沒問題,那就是類有問題了,代碼如圖:

    上課的時候恍惚間聽見老師說能夠將形參的名字寫成變量的名字同樣,並且不會報錯,因此一開始我就這樣作了,就出現了上述狀況。後來改爲了如上圖所示,在驅動類的實參寫的字符串就能正常getX到了。
    這裏還要記錄一下,在用布爾值表示這輛車是否爲古董車的時候,我用了startsWith方法。判斷它的出廠年份減去四十五以後,是正值仍是負值,即判斷有沒有負號。可是有一個問題就是,要想進行減法運算,得把年份設成數值型,只有將減過以後的結果變成字符串才能用那個方法判斷結果有沒有「-」。代碼如圖:

代碼託管

上週考試錯題總結

  • 錯題1: If two variables contain aliases of the same object then
    A .the object may be modified using either alias
    B .the object cannot be modified unless there's but a single reference to it
    C .a third alias is created if/when the object is modified
    D .the object will become an "orphan" if both variables are set to null
    E .answers A and D are correct
    緣由:根據定義,別名提供了能夠修改對象的方法(別名就像指針)。根據定義,別名提供了能夠修改對象的方法(別名就像指針)。若是兩個變量都被設置爲null,那麼這個對象就不會被任何變量引用(經過任何別名),並且它確實會變成一個「孤兒」,而且在未來某個時候會被垃圾收集。

  • 錯題2 Which properties are true of String objects?
    A .Their lengths never change
    B .The shortest string has zero length
    C .Individual characters within a String may be changed using the replace method
    D .The index of the first character in a string is one
    E .Only A and B are true
    緣由:字符串是不可變的。這意味着一旦建立了字符串對象,就不能更改它。所以,字符串的長度在建立後不會改變。最短長度的字符串是「」,引號之間沒有字符,因此長度爲零。replace方法容許您從原始的字符串中建立一個新的字符串,替換一些字符。字符串中第一個位置的索引是0,而不是1。另外,每一個字符串的最後一個字節包含了字符串的末尾字符,它是一個低值或二進制零的字節。

  • 錯題3: What is output with the statement System.out.println(x+y); if x and y are int values where x=10 and y=5? (若是x和y是int類型的數值,x=10,y=5,那麼語句System.out.println(x+y);的輸出是什麼?)
    A .15
    B .105
    C .10 5
    D .x+y
    E .An error since neither x nor y is a String (因x和y都不是字符串而引發的一個錯誤)
    緣由:這裏的x和y是變量,存儲着數值,「+」起的是加號的做用,不是鏈接符。

  • 錯題4: What happens if you attempt to use a variable before it has been initialized?
    A .A syntax error may be generated by the compiler
    B .A runtime error may occur during execution
    C .A "garbage" or "uninitialized" value will be used in the computation
    D .A value of zero is used if a variable has not been initialized
    E .Answers A and B are correct
    緣由:不少時候,編譯器可以檢測到未初始化變量的嘗試使用,在這種狀況下會產生語法錯誤。若是在編譯器中使用脫逃檢測,那麼在使用時就會出現運行時錯誤。Java是一種很是「安全」的語言,所以若是在計算中使用未初始化的變量,它不容許使用「垃圾」或「0」。

  • 錯題5: What is the function of the dot operator?
    A . It serves to separate the integer portion from the fractional portion of a floating point number
    B . It allows one to access the data within an object when given a reference to the object
    C . It allows one to invoke a method within an object when given a reference to the object
    D . It is used to terminate commands (much as a period terminates a sentence in English)
    E . Both B and C are correct
    緣由:沒有任何機制能夠,但有方法能夠執行相似的操做,並返回一個字符在一個給定的位置(charAt)或返回int。

  • 錯題6:
    In the StringMutation program shown in Listing 3.1, if phrase is initialized to "Einstein" what will Mutation #3 yield if Mutation #1: mutation1 = phrase.concat(".")?
    A .Einstein.
    B .EINSTEIN.
    C .XINSTXIN.
    D .einstein.
    E .xinstxin.
    緣由:突變#2將每一個字母更改成大寫。而後突變#3用Xs替換了Es。

  • 錯題7:
    Say you write a program that makes use of the Random class, but you fail to include an import statement for java.util.Random (or java.util.*). What will happen when you attempt to compile and run your program.
    A .The program won't run, but it will compile with a warning about the missing class.
    B .The program won't compile-you'll receive a syntax error about the missing class.
    C .The program will compile, but you'll receive a warning about the missing class.
    D .The program will encounter a runtime error when it attempts to access any member of the Random class
    E .none of the above
    緣由:缺乏的類意味着將有未定義的變量和/或方法。編譯器將檢測這些並將發出錯誤消息。你的程序沒法執行。

  • 錯題8:
    The String class' compareTo method
    A .compares two string in a case-independent manner
    B .yields true or false
    C .yields 0 if the two strings are identical
    D .returns 1 if the first string comes lexically before the second string
    E .none of the above
    緣由:若是兩個字符串是相同的,那麼就會獲得0。若是第一個字符串在第二個字符串以前出現,那麼-1若是第一個字符串在第二個字符串後面出現,則爲+1。

  • 錯題9:
    The advantage(s) of the Random class' pseudo-random number generators, compared to the Math.random method, is that
    A .you may create several random number generators
    B .the generators in Random are more efficient than the one in Math.random
    C .you can generate random ints, floats, and ints within a range
    D .you can initialize and reinitialize Random generators
    E .all but answer B
    緣由:全部隨機數生成器的效率是相同的。隨機生成器優於數學的優勢。隨機包含全部其餘屬性。

感想

  • 1.這一週的內容說實話有點多,也難懂了許多。從代碼行和代碼上就能看出來,有的代碼就須要仔細去理解才能懂。
  • 2.從編程的角度看,一個程序編寫的實現也開始變得多樣化了,不一樣的編寫都能實現想要的結果。這就使得本身要思考一個更好的編寫方式,就須要花費必定的時間去嘗試了。

學習進度條

代碼行數(新增/累積) 博客量(新增/累積) 學習時間(新增/累積) 重要成長
目標 5000行 30篇 400小時
第一週 309/309 1/1 20/20
第二週 269/578 1/2 18/38
第三週 236/776 1/3 22/60
第四周 507/1283 2/5

參考資料

相關文章
相關標籤/搜索