1. 程式人生 > >20172327 2017-2018-2 《程序設計與數據結構》第四周學習總結

20172327 2017-2018-2 《程序設計與數據結構》第四周學習總結

hose math.sqrt 多看 car abs action runt black private

20172327 2017-2018-2 《程序設計與數據結構》第四周學習總結

教材學習內容總結

第4章

  • 類結構的定義和概念
  • 對象的屬性和操作
  • 實例數據
  • 方法定義的結構
  • 構造方法的結構和用途

第7章

  • 軟件設計的主要問題
  • 程序所需要的類和對象
  • 靜態變量與靜態方法
  • 類間關系中的依賴,聚合
  • this引用
  • 接口:Comparable,Iterator
  • 枚舉類型
  • 方法分解,參數的傳遞方式。

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

問題1:Java接口有什麽作用

分析:

下面是我從網上查到的一個博客,感覺說的還是挺全的。

  • 4點關於JAVA中接口存在的意義:
  • 1、重要性:在Java語言中, abstract class 和interface 是支持抽象類定義的兩種機制。正是由於這兩種機制的存在,才賦予了Java強大的 面向對象能力。
  • 2、簡單、規範性:如果一個項目比較龐大,那麽就需要一個能理清所有業務的架構師來定義一些主要的接口,這些接口不僅告訴開發人員你需要實現那些業務,而且也將命名規範限制住了(防止一些開發人員隨便命名導致別的程序員無法看明白)。
  • 3、維護、拓展性:比如你要做一個畫板程序,其中裏面有一個面板類,主要負責繪畫功能,然後你就這樣定義了這個類。可是在不久將來,你突然發現這個類滿足不了你了,然後你又要重新設計這個類,更糟糕是你可能要放棄這個類,那麽其他地方可能有引用他,這樣修改起來很麻煩。如果你一開始定義一個接口,把繪制功能放在接口裏,然後定義類時實現這個接口,然後你只要用這個接口去引用實現它的類就行了,以後要換的話只不過是引用另一個類而已,這樣就達到維護、拓展的方便性。
  • 4、安全、嚴密性:接口是實現軟件松耦合的重要手段,它描敘了系統對外的所有服務,而不涉及任何具體的實現細節。這樣就比較安全、嚴密一些(一般軟件服務商考慮的比較多)。

代碼學習中的問題和解決過程

問題1:在pp4.7的過程中,出現了程序運行出來全部為null的情況。下面是我當時的情況

技術分享圖片
技術分享圖片

分析:解決過程是通過同學的幫助,發現我將形式參數和變量名輸成相同值了,所以程序裏邊混亂,當我預習到第七章時,我發現我可以用This來處理這樣的問題。

技術分享圖片
技術分享圖片

問題2:在編寫MultiSphere過程中,出現了下面的錯誤;

技術分享圖片

分析:這個問題我後來對比同學的,發現我前面的輸入語法有問題。

上周考試錯題總結

  • 第一題:The behavior of an object is defined by the object‘s
    A .instance data
    B .constructor
    C .visibility modifiers
    D .methods

    E .all of the above
  • 分析這些方法指示對象在傳遞消息時的反應方式。 每條消息都作為一個方法實現, 該方法是傳遞消息時執行的代碼。 構造函數是這些方法之一, 但所有方法都組合在一起來決定行為。 可見性修飾符會間接影響對象的性能。

  • 第二題:Which of the following reserved words in Java is used to create an instance of a class?
    A .class
    B .public
    C .public or private, either could be used
    D .import
    E .new
  • 分析:保留字 "new " 用於實例化對象, 即創建類的實例。 新語句後面跟著類的名稱。 這將調用類的構造函數。 例子: Car x = new Car ();將創建一個新的汽車實例, 並設置變量 x。

  • 第三題:In order to preserve encapsulation of an object, we would do all of the following except for which one?
    A .Make the instance data private
    B .Define the methods in the class to access and manipulate the instance data
    C .Make the methods of the class public
    D .Make the class final
    E .All of the above preserve encapsulation
  • 分析:封裝意味著該類同時包含操作數據所需的數據和方法。 為了正確保存封裝, 實例數據不應直接從類外部訪問, 因此實例數據是私有的, 並且定義了方法來訪問和操作實例數據。 此外, 訪問和操作實例數據的方法將被公開, 以便其他類可以使用該對象。 保留字 "final " 用於控制繼承, 與封裝無關。

  • 第四題:If a method does not have a return statement, then
    A .it will produce a syntax error when compiled
    B .it must be a void method
    C .it can not be called from outside the class that defined the method
    D .it must be defined to be a public method
    E .it must be an int, double, float or String method
  • 分析:所有的方法都是隱含的返回的東西, 因此必須有一個返回語句。 但是, 如果程序員希望編寫一個不返回任何內容的方法, 因此不需要返回語句, 則它必須是 void 方法 (標頭具有 "void" 作為返回類型的方法)。

  • 第五題:Instance data for a Java class
    A .are limited to primitive types (e.g., int, float, char)
    B .are limited to Strings
    C .are limited to objects(e.g., Strings, classes defined by other programmers)
    D .may be primitive types or objects, but objects must be defined to be private
    E .may be primitive types or objects
  • 分析:實例數據是構成類的實體, 並且可以是任何可用的類型 (無論是基元還是對象), 並且可能是公共的還是私有的。 通過使用對象作為實例數據, 它允許將類構建在其他類上。 類具有其他類的實例數據的這種關系稱為 "已有" 關系。

  • 第六題:Consider a Rational class designed to represent rational numbers as a pair of int‘s, along with methods reduce (to reduce the rational to simplest form), gcd (to find the greatest common divisor of two int‘s), as well as methods for addition, subtraction, multiplication, and division. Why should the reduce and gcd methods be declared to be private.
    A .Because they will never be used
    B .Because they will only be called from methods inside of Rational
    C .Because they will only be called from the constructor of Rational
    D .Because they do not use any of Rational‘s instance data
    E .Because it is a typo and they should be declared as public
  • 分析:聲明為私有的類的所有項只能由該類中的實體訪問, 無論它們是實例數據還是方法。 在這種情況下, 由於這兩種方法只從理性的其他方法 (包括構造函數) 調用, 因此它們被聲明為私有, 以更大程度地促進信息隱藏。 請註意, 答案 C 不是正確的答案, 因為縮減方法調用 gcd 方法, 因此從構造函數以外的方法調用其中的一個方法。

  • 第七題:Visibility modifiers include
    A .public, private
    B .public, private, protected
    C .public, private, protected, final
    D .public, protected, final, static
    E .public, private, protected, static
  • 分析:公共、私有、受保護的控制變量和方法的可見性。 最終控制變量、方法或類是否可以進一步更改或重寫不可見性。 靜態控制變量或方法是否與類或類本身的實例關聯。

  • 第八題:What happens if you declare a class constructor to have a void return type?
    A .You‘ll likely receive a syntax error
    B .The program will compile with a warning, but you‘ll get a runtime error
    C .There‘s nothing wrong with declaring a constructor to be void
    D .The class‘ default constructor will be used instead of the one you‘re declaring
    E .None of the above
  • 分析:聲明具有任何類型甚至無效的構造函數是一種語法沖突, 因此您將收到語法錯誤。

  • 第九題:Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header.
    A .true
    B .false
  • 分析:問題有兩個定義顛倒了。 形式參數是在方法標頭中出現的, 實際參數是方法調用中的參數 (傳遞給方法)。

  • 第十題:Defining formal parameters requires including each parameters type.
    A .true
    B .false
  • 分析:為了讓編譯器檢查方法調用是否正確, 編譯器需要知道正在傳遞的參數的類型。 因此, 所有的正式參數 (在方法頭中定義的) 必須包括它們的類型。 這是使 Java 成為強類型語言的一個元素。

  • 第十一題:Every class definition must include a constructor.
    A .true
    B .false
  • 分析:Java 允許在沒有構造函數的情況下定義類, 但是, 在這種情況下使用的是默認構造函數。

  • 第十二題:During program development, software requirements specify
    A .how the program will accomplish the task
    B .what the task is that the program must perform
    C .how to divide the task into subtasks
    D .how to test the program when it is done
    E .all of the above
  • 分析:規範階段是了解手頭的問題, 以便程序員能夠確定需要做些什麽來解決這個問題。 上面列出的其他工作是設計階段 (A、C) 和測試階段 (D) 的一部分。

  • 第十三題:Static methods cannot
    A .reference instance data
    B .reference non-static instance data
    C .reference other objects
    D .invoke other static methods
    E .invoke non-static methods
  • 分析:靜態方法是類本身的一部分, 而不是實例化對象的方法, 因此靜態方法在類的所有實例化對象之間共享。 由於靜態方法是共享的, 因此它無法訪問非靜態實例數據, 因為所有非靜態實例數據都是特定於實例化對象的。 靜態方法可以訪問靜態實例數據, 因為與方法類似, 實例數據在類的所有對象之間共享。 靜態方法還可以訪問傳遞給它的參數。

  • 第十四題:Inheritance through an extended (derived) class supports which of the following concepts?
    A .interfaces
    B .modulary
    C .information hiding
    D .code reuse
    E .correctness
  • 分析:通過擴展類並從它繼承, 新類不必重新實現任何繼承的方法或實例數據, 從而節省了程序員的精力。 因此, 代碼重用是通過將他人的代碼擴展為您的需要而重用其他人的密碼的能力。

  • 第十五題:In order to implement Comparable in a class, what method(s) must be defined in that class?
    A .equals
    B .compares
    C .both lessThan and greaterThan
    D .compareTo
    E .both compares and equals
  • 分析:可比較類要求 compareTo 方法的定義, 它將比較兩個對象並確定一個是否等於另一個, 或者如果一個小於或大於另一個, 並以負 int、0或正整數響應。 由於 compareTo 的響應與0如果兩個對象相等, 則不需要定義等號方法。

  • 第十六題:Unlike the String class where you must pass a message to an object (instance) of the class, as in x.length( ), in order to use the Math class, you pass messages directly to the class name, as in Math.abs( ) or Math.sqrt( ).
    A .true
    B .false
  • 分析:數學類使用被稱為靜態方法 (或類方法) 的方法, 通過將消息直接傳遞到類名本身而不是類的對象來調用。

  • 第十七題:Interface classes cannot be extended but classes that implement interfaces can be extended.
    A .true
    B .false
  • 分析:任何類都可以擴展, 無論它是接口、實現接口還是兩者都不是。 唯一的例外情況是, 如果該類被顯式修改為 "final ", 在這種情況下, 不能擴展。

  • 第十八題:Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header.
    A .true
    B .false
  • 分析:問題有兩個定義顛倒了。 形式參數是在方法標頭中出現的, 實際參數是方法調用中的參數 (傳遞給方法)。

  • 第十九題:In black-box testing, the tester should already know something about how the program is implemented so that he/she can more carefully identify what portion(s) of the software are leading to errors.
    A .true
    B .false
  • 分析:在黑盒測試中, 測試人員不應該知道軟件是如何實現的。 實質上, 軟件是一個具有輸入和輸出的黑箱, 程序的機制是不透明的。 如果測試人員確實知道程序是如何工作的, 那麽測試人員的測試用例可能會有偏差。 如果測試人員知道程序的工作原理, 那麽測試就被稱為玻璃盒測試。

代碼托管

技術分享圖片
技術分享圖片

其他(感悟、思考等,可選)

進入第二周,有點難,程序的樣子和上一周有所改變,但要打的代碼更多了,發現時間有點不夠用,感覺這一周學的狀態並不好,所以下一周會花更多的時間在Java的學習中。

學習進度條

代碼行數(新增/累積) 博客量(新增/累積) 學習時間(新增/累積) 重要成長
目標 5000行 30篇 400小時
第一周 95/95 1/1 18/18
第二周 515/620 1/2 22/40
第三周 290/910 1/3 20/60
第四周 1741/2651 1/4 30/84

嘗試一下記錄「計劃學習時間」和「實際學習時間」,到期末看看能不能改進自己的計劃能力。這個工作學習中很重要,也很有用。
耗時估計的公式:Y=X+X/N ,Y=X-X/N,訓練次數多了,X、Y就接近了。

參考:軟件工程軟件的估計為什麽這麽難,軟件工程 估計方法

  • 計劃學習時間:20小時

  • 實際學習時間:30小時

  • 改進情況:有點

(有空多看看現代軟件工程 課件
軟件工程師能力自我評價表)

參考資料

  • 《Java程序設計與數據結構教程(第二版)》

  • 《Java程序設計與數據結構教程(第二版)》學習指導
  • ...

20172327 2017-2018-2 《程序設計與數據結構》第四周學習總結