1. 程式人生 > >20172311 《程序設計與數據結構》第四周學習總結

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

服務 指導 等於 ger 簡單方法 thead 進度條 約數 HR

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

教材學習內容總結

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

  • 問題1:不理解compareTo()方法
  • 問題1解決方案:

    網上查找的較易理解的實例

    public class Test{ 
       public static void main(String args[]){
      Integer x = 5;
      System.out.println(x.compareTo(3));
      System.out.println(x.compareTo(5));
      System.out.println(x.compareTo(8));            
     }
    }

    編譯以上程序,輸出結果為:

    1
    0
    -1

  • 總結:compareTo()方法可用於比較Byte, Double, Integer, Float, Long,Short或String等類型的參數該方法用於兩個相同數據類型的比較,兩個不同類型的數據不能用此方法來比較。在java編程中,我們會偶爾遇到字符串大小比較的問題,compareTo()方法很簡單就實現這種功能。該方法用於判斷一個字符串是大於、等於還是小於另一個字符串。判斷字符串大小的依據是根據它們在字典中的順序決定的。

    int compareTo(String str)

    返回一個整形值,正值、0、負值分別表示本對象的字符串按字典順序位置後於、等於或先於str對象的字符串。


  • 問題2:不理解課本上例7.4中以下兩個方法的作用:

    private void reducde()
private int gcd(int num1,int num2)
  • 問題2解決方案:
    通過查閱資料了解到兩個方法分別是有關公約數和公倍數的方法,保證輸出的分數是最簡形式,即沒有公約數。這兩個方法都被聲明為具有private可見性,因為不允許直接從RationalNumber類的外部執行這些方法,這些方法的存在不是為了提供對象的服務,而僅僅是為了支持對象的其他服務。

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

  • 問題1:做pp7_4項目時,以下代碼輸出為0,不符合項目要求
System.out.println(r1.compareTo(r2));

相關代碼截圖如下:
解決前:
技術分享圖片

解決後:
技術分享圖片

  • 問題1解決方案:
    通過查閱資料以及大量的嘗試最終發現自己犯了個低級錯誤,(double)(int a / int b);是先取整再轉換為double型。

  • 問題2:有關 git commit 使用理解不到位。
  • 問題2解決方案:
    通過詢問助教以及自身實踐總結出關於git commit的以下幾點:
    (1)只要改動了.java文件,最好接著就git add . ;git commit-m"" ; git push 。
    (2)使用git add . 時要註意此命令是對所有新建的或者改動的.java文件進行操作,所以盡量做一個項目就接著git commit 。
    (3)提供一個修改commit的較簡單方法:
    vi 想重新commit的.java文件,進行任意一處改動,比如可在註釋中加個空格,然後git add . ;git commit -m""
    ;git push即可。

代碼托管

技術分享圖片

上周考試錯題總結


  • 錯題1:
    In Java a variable may contain

A.a value or a reference

B.a package

C.a method

D.a class

E.any of the above

答案:A 我的選擇:E

理解:在JAVA中變量只能包含一個值或一個引用。


  • 錯題2:
    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

答案:E 我的選擇:B

理解:對象可以使用別名進行修改,如果兩個變量都設置為null,那麽對象將變成一個“孤兒”。


  • 錯題3:
    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

答案:E 我的選擇:B

理解:字符串的長度永遠不會改變,字符串最短的長度為0。


  • 錯題4:
    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

答案:E 我的答案:A

理解:點算符的功能為:它允許在給定對象的引用時訪問對象中的數據,當給定對象的引用時,它允許在對象中調用方法。


  • 錯題5:
    Which of the following will yield a pseudorandom number in the range [ -5, +5 ) given the following:
    Random gen = new Random( );

A.gen.nextFloat( ) * 5

B.gen.nextFloat( ) * 10 - 5

C.gen.nextFloat( ) * 5 - 10

D.gen.nextInt( ) * 10 - 5

E.gen.nextInt(10) - 5

答案:B 我的選擇:D

錯題原因:沒有註意到Float和Int。


  • 錯題6:
    Consider the following two lines of code. What can you say about s1 and s2?
    String s1 = "testing" + "123";
    String s2 = new String("testing 123");

A.s1 and s2 are both references to the same String object

B.the line declaring s2 is legal Java; the line declaring s1 will produce a syntax error

C.s1 and s2 are both references to different String objects

D.s1 and s2 will compare "equal"

E.none of the above

答案:C 我的選擇:A

理解:s1和s2是對不同對象的引用,無論s1和s2對應的變量是否相等。


  • 錯題7:
    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

答案:C 我的選擇:A

理解:string類的compareTo方法中如果兩個字符串是相同的,則得到0。


  • 錯題8:
    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

答案:E 我的選擇:C

理解:偽隨機數生成器相對於Math.random的優勢在於:可以創建幾個隨機數生成器,可以在一個範圍內生成隨機的int,floats和ints。


  • 錯題9:
    The advantages of the DecimalFormat class compared with the NumberFormat class include

A.precise control over the number of digits to be displayed

B.control over the presence of a leading zero

C.the ability to truncate values rather than to round them

D.the ability to display a % automatically at the beginning of the display

E.only A and B

答案:E 我的選擇:A

理解:與NumberFormat類相比,DecimalFormat類的優勢在於可以精確控制顯示的數字的數目,可以控制一個前導0的存在。


  • 錯題10:
    When comparing any primitive type of variable, == should always be used to test to see if two values are equal.

A.true

B.false

答案:B 我的選擇:A

理解:==號可以用來測試兩個值是否相等,但是它的作用不止如此。


  • 錯題11:
    If you need to import not only the top-level of a package, but all its secondary levels as well, you should write: import package..;

A.true

B.false

答案:B 我的選擇:A

理解:如果不僅需要導入一個包的頂層,還需要導入所有的二級級別,那麽應該編寫:import package.*;


  • 錯題12:
    The printf method within System.out is designed to ease the conversion of legacy C code into Java.

A.true

B.false

答案:A 我的選擇:B

理解:系統內的print方法,其目的是為了簡化遺留的c代碼到JAVA的轉換。


上周課堂實踐補充截圖:

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

  • 教訓:在藍墨雲提交時一定要點提交作業再提交,而且盡量用自己的數據網,以防止上傳不上去。

學習進度條

代碼行數(新增/累積) 博客量(新增/累積) 學習時間(新增/累積) 重要成長
目標 5000行 30篇 400小時
第一周 28/28 1/1 16/16
第二周 710/738 1/2 20/36
第三周 426/1164 1/3 16/52
第四周 1068/2232 2/5 20/72
  • 計劃學習時間:16小時

  • 實際學習時間:20小時

  • 改進情況:繼續加強對JAVA的學習。

參考資料

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

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

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