20172318 2017-2018-2 《程序设计与数据结构》第5周学习总结

20172318 2017-2018-2 《程序设计与数据结构》第5周学习总结

教材学习内容总结

控制流:运行程序时,语句的执行顺序称为控制流。
布尔表达式:条件语句和循环语句基于布尔表达式
判断语句:if
循环语句:while、do、for
迭代器:提供方法用于一次性处理某个集合
ArrayList类:集合类API一部分,管理对象集
switch语句:计算表达式后与表达式相匹配的第一条case子句处执行
条件语句:三元运算符,如(total>MAX)?total+1:total*2;

教材学习中的问题和解决过程

  • 问题1:对于Coin类中flip方法(int)Math.Random()*2;不理解
  • 问题1解决方案:了解了前面的int用于将后面随机数只产生0或1两种结果;乘2是为了让获得0和1的概率相等,我在之后的石头剪刀布软件中运用了这一思想获得三个随机数中其中一个。
  • 问题2:不了解迭代器的定义和方法
  • 问题2解决方案:参考了百科
    https://baike.baidu.com/item/迭代器/3803342?fr=aladdin 再配合课本的描述认识到迭代器是一个访问特定元素的对象,可用于统计等功能

代码调试中的问题和解决过程

  • 问题1:

  • 问题1解决方案:将if语句后的=号改成==号

  • 问题2:

  • 问题2解决方案:开始以为是;号的问题,后来发现是自己将while首字母大写导致出错

  • 问题3:

  • 问题3解决方案:嗯,卡了很久,最后是因为;;的全角和半角符号错误,而在编写和调试过程中没有注意这个问题

  • 问题4:

  • 问题4解决方案:需要在Math前添加(int)的强制转换符

  • 问题5:pp5.7中无法输入(y/n)进行循环

  • 问题5解决方案:定义两个scan分别表示输入的INT类以及STRING类

代码托管

上周考试错题总结

  • 错题1
    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
    选D,方法决定行为
  • 错题2
    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
    选E,new用于实例化变量
  • 错题3
    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
    选D,final用于控制继承,与封装无关
  • 错题4
    Consider a sequence of method invocations as follows: main calls m1, m1 calls m2, m2 calls m3 and then m2 calls m4, m3 calls m5. If m4 has just terminated, what method will resume execution?
    A . m1
    B . m2
    C . m3
    D . m5
    E . main
    选B,一旦方法终止,控制就会使用调用该方法的方法恢复。在这种情况下,m2调用m4,因此当m4终止时,m2恢复。
  • 错题5
    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
    选E,实例数据是组成该类的实体,可能是任何可用的类型,无论是原始的还是对象的。
  • 错题6
    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
    选B,被声明为私有的类的所有条目只能被该类中的实体访问,不管它们是实例数据还是方法。在这种情况下,由于这两种方法只从Rational的其他方法(包括构造函数)调用,所以它们被声明为私有,以促进信息隐藏在更大的程度。注意,答案C不是一个正确的答案,因为reduce方法调用gcd方法,所以其中一个方法是从构造函数以外的方法调用的。
  • 错题7
    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
    选D(参考答案为A,但老师课上讲过A答案错误),如果你声明一个类构造函数有一个void返回类型,会发生什么?类的默认构造函数将被使用,而不是您所声明的。
  • 错题8
    Java methods can return more than one item if they are modified with the reserved word continue, as in public continue int foo( ) { ... }
    A . true
    B . false
    选B,所有的Java方法都返回一个item(注意是一个),无论它是一个原始数据类型还是一个对象,或者是void。保留的单词继续用于退出循环的其余部分,并再次测试该条件
  • 错题9
    Every class definition must include a constructor.
    A . true
    B . false
    选B(其实A也对,毕竟会有个默认的构造函数)Java允许在没有构造函数的情况下定义类,然而,在这种情况下有一个默认的构造函数。

学习进度条

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 5000行 30篇 400小时
第一周 128/128 1/1 12/12
第二周 212/340 1/2 18/30
第三周 206/546 1/3 20/50
第四周 483/1029 2/5 40/90
第五周 633/1662 1/6 30/120

参考资料

posted @ 2018-04-10 22:41  m1sty  阅读(186)  评论(4编辑  收藏  举报