1. 程式人生 > >20172325 2016-2017-2 《Java程序設計》第四周學習總結

20172325 2016-2017-2 《Java程序設計》第四周學習總結

dot 自己 pla 都是 隨機數生成 ges 情況 有用 靜態變量

20172325 2016-2017-2 《Java程序設計》第四周學習總結

教材學習內容總結

1.對類、對象、聲明變量的定義和屬性有了進一步的了解
2.學會如何編寫一個類並運用到需要的程序中
3.學習了形參和實參的概念和區別
4.修飾符的作用和運用,例如public和private
5.學習了靜態類,例如靜態方法和靜態變量
6.關於封裝的作用和運用
7.在面向對象的程序設計時需要做的必要步驟,包括需求、設計、實現和測試
8.對枚舉類的進一步了解
9.學習了調用方法中的方法重載
10.UML類圖包含的內容和作用,有助於對類的理解

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

  • 問題1:區分不了對象和類
  • 問題1解決方案:首先是通過學習教材了解具體定義,然後百度具體區別,然後通過具體操作加深理解。下面是自己的理解,類是一個規則一個標準或者說一個模範,是一個虛擬的存在;對象是一個可變的的個體,有具體的需求和形態。而對象和類的聯系在於對象的實現需要通過類來規範。
    技術分享圖片

  • 問題2:在形參和實參的理解和運用上很頭疼
  • 問題2解決方案:之前是在網上查詢,可以參考這篇博客[(https://www.cnblogs.com/calence/p/5346672.html)]
    後來老師在課上也給出了很詳細的講解。
    技術分享圖片

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

  • 問題1:對於getFaceValue和setFaceValue的作用不是很清楚,在敲例題4.2的時候,中間加了這兩個方法和刪除之後運行沒有差別,不知道為何。
  • 問題1解決方案:通過看書以後了解到,他們是同級的不同調用方法,內容不一樣而已,這就是在設計時輸入get和set先後順序對於結果沒有影響

  • 問題2:如下圖,不知道是什麽情況,自從出現了這個東西,試了之前編譯運行成功的程序也出現這樣的結果。
    技術分享圖片

代碼托管

上周考試錯題總結

  • 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
    在JAVA中變量只能包含一個值或一個引用。所以應該選a的。

  • 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
    對象可以使用別名進行修改,如果兩個變量都設置為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
    他們的長度永遠不會改變,最短的字符串長度為零。字符串是不可變的。這意味著, 一旦創建了字符串對象, 就不能更改它。因此, 字符串的長度在創建後不會更改。最短長度字符串為 "", 引號之間沒有字符, 因此長度為零。

  • 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
    編譯器多次能夠檢測到未初始化變量的嘗試使用, 在這種情況下會產生語法錯誤。如果編譯器使用轉義檢測, 則在使用時會發生運行時錯誤。

  • 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
    點算符的功能為:它允許在給定對象的引用時訪問對象中的數據,當給定對象的引用時,它允許在對象中調用方法。所以這道題應該選e的。

  • 6.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
    數學類使用被稱為靜態方法 (或類方法) 的方法, 通過將消息直接傳遞到類名本身而不是類的對象來調用。

  • 7.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
    gen.nextInt(10) - 5 所得到的結果是-5,-4,-3,-2,-1,0,1,2,3,4,並不是題目中所要求的,它得到的只是一些數而不是一個取值範圍。而nextFloat則是在範圍[0,1)中產生偽隨機數字;乘以10的收益率在0到10之間,再減去5,得到結果。

  • 8.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 objects
    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
    這兩個聲明都是合法的Java。s1是一個字符串引用,它被初始化為字符串“testing123”。s2是一個字符串引用,它被初始化為字符串“測試123”。註意“測試”和“123”之間的空格。所以這兩個字符串不相等。

  • 9.An "alias"(別名) is when
    A . two different reference variables refer to the same physical object
    B . two different numeric variables refer to the same physical object
    C . two different numeric variables contain identical values
    D . two variables have the same names
    E . none of the above
    當有兩個或多個對同一物理對象的引用時,就會出現“別名”,這樣,通過遵循任一引用,就可以讀/寫/修改對象

  • 10.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。

  • 11.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
    偽隨機數生成器相對於Math.random的優勢在於:可以創建幾個隨機數生成器,可以在一個範圍內生成隨機的int,floats和ints。所以應該包括A與B。

  • 12.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
    如果您不僅需要導入包的頂層, 而且還要輸入所有的輔助級別, 則應編寫: 導入包. ;(false);導入包.

  • 13.You may use the String replace( ) method to remove characters from a String.
    A true
    B false
    replace()方法僅用其他單個字符替換單個字符。replace()方法不會將字符添加或刪除字符串;字符串長度將保持不變

  • 14.All the methods in the Math class are declared to be static.
    A true
    B false
    數學類方法被設計成在算術表達式中通常有用,因此不需要任何實例來使用它們。這是通過確保所有的數學方法都是靜態的來實現的。

  • 15.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
    所有隨機數字生成器的效率是一樣的。隨機生成器優於數學的優點。隨機包含所有其他屬性。

  • 16.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
    通過一個或多個數學方法,截斷在程序員的手中“%”符號會出現在顯示器的末端,而不是開始。

20172325 2016-2017-2 《Java程序設計》第四周學習總結