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的学习。

参考资料

posted on 2018-04-04 09:42  socialsea  阅读(308)  评论(4编辑  收藏  举报