20,182,303 2019-2020-1 "data structures and object-oriented programming," the first four weeks learning summary

Learning content summary

4.8.1 iterators and a for loop

1. iterator

  • Use collection iterator()acquired iterator class object implemented using interfaces to accept Iterator Iterator interface (polymorphism)
  • Iterator using interface method hasNext()is determined there is no further next element
  • Iterator interface using the method next()fetches the next element in the set

2. for use loop
used to iterate through a set of arrays, and

for (集合/数组的数据类型 变量名 : 集合名/数组名){
  System.out.println(变量名);
}

Writing class

1. Classes and Objects

  • Class is a blueprint for an object.
  • Each class includes class defined for the object attributes and operations.
    • State of the object is defined by the attributes associated with the object.
    • Defined by the operating behavior of an object associated with the object.
  • Data and methods of the class are called members of the class.

2. UML class diagram

Static class 3.
The method of overload : two or more methods have the same name.
5. Testing : The test purpose is to discover an error, by finding the error and modify them to improve the quality of the program.

Textbook learning and problem-solving process

  • Question 1 : iterators and for Robin
  • Problem 1 Solution :
    each have their own advantages Efficiency:
  1. ArrayList faster random access, and used for loop get()method is employed, i.e., random access, so in ArrayList for fast cycle.
  2. LinkedList sequential access is relatively fast, the Iterator next()method used is sequential access method, so faster in LinkedList used in Iterator.
  3. Mainly based on different sets of data structure determination.
  • Problem 2 : staticKeyword tweak the permissions of the class members do?
  • Problem 2 Solution : Java is staticthe keyword does not affect the scope of a variable or method. In Java can affect access only private, , public(includingprotected packet access) these keywords.
  • Question 3 : can thisaccess a static member variables do?
  • Question 3 Solution : For Chestnut
public class Main {  
    static int value = 33;
 
    public static void main(String[] args) throws Exception{
        new Main().printValue();
    }
 
    private void printValue(){
        int value = 3;
        System.out.println(this.value);
    }
}

thisIt represents the current object, through new Main()to call printValueit, the current object is through the new Main()generated objects. The staticvariables are to be enjoyed by the object, and therefore printValuethe this.valuevalue is no doubt 33. In the printValueinterior of the method valueis a local variable, and impossible thisassociated, the output is 33.
Although static member variables independent of the object, but does not mean not to access through the object, all the static methods and static variables are accessible (as long as access to sufficient) through the object.

  • Question 4 : How to understand the "interface", the necessary excuse for the meaning of existence?
  • Scheme 4 Answer questions : the interface is a collection of constants and abstract methods of a set of (abstract method refers to a method not implemented, i.e. no code body, the method not implemented in the interface, the method The header is followed by a semicolon only parameter list). Interfaces can not be instantiated, class by implementing this interface to implement the abstract methods defined in each interface, the class implements the interface need to use implements reserved words in the class declaration of the head, then the interface name is given, the class must implement at least one of abstract interface method, while other methods can also be defined. A plurality of classes can implement the same interface, a class can implement multiple interfaces. When a class that implements the interface, the interface can define additional methods. Example of a simple interface:
public interface Nameable
{
     public static setName(String Name);
     public String getName();
}

"Interface is a specification," and this right. "Why do not you write directly in this class implementation it is more convenient," how do you ensure that this interface is a class to implement it? If more than one class to implement the same interface, the program know how they are linked to it? Since it is not a class to realize that there are many places to be useful, we need uniform standards. And even some programming language (Object-C) do not have an interface called interface, direct call protocol. The purpose of uniform standards, we all know what this is doing, but do not know the specifics of how to do specific. For example: I know Comparable This interface is used to compare two objects, how to compare it? Digital methods have more numbers, strings have string comparison method, students (their own definition of class) has its own comparative method. Then, in another object responsible for sorting (not necessarily the numbers oh) code inside, definitely you need to compare two objects. Both objects are what type it? Object a, b? Certainly not, a> b this syntax will not compile. int a, b? It does not work? He said at the beginning, not necessarily the numbers. .... So, Comparable came. He tells the compiler, ab two objects meet Comparable interface, that is, they can be compared. Specifically how to compare, this program does not need to know. So, he needs some specific implementation, Comparable interface has a method called compareTo. Then this method is used to replace <,> such operators. Because the operator is compared with a compiler to retain the built-in type (integer, floating point) instead of a generalized comparison operation. If you can understand their own JDK libraries such as inside Comparable already have such an interface, it is easy to understand why they need to use when developing the interface of the program.

About four o'clock in the sense JAVA interfaces exist (to improve the flexibility of the method steps) :
1, the importance of: abstract class and interface supports two mechanisms abstract class definition. It is the existence of these two mechanisms, it gives a powerful object-oriented Java capability;
2, simple and normative: Interface not only to tell the developer that you need to implement those services, but will also naming locked out (to prevent the development of some naming lead to other programmers who just can not understand);
3, maintenance and scalability;
4, security, rigor: Interface software is an important means to achieve loose coupling, which describes all of the external service system, without involving any specific implementation details.

Code debugging and problem solving in the process

  • Question 1 : git upload problems encountered IDEA

  • Problem 1 Solution : The current directory is not a remote repository local directory, so brutally denied.
  • Question 2 garbled after MyDoc.java run:

  • Problem 2 Solution : Set the bottom right corner for the File Encoding GBK can be solved.
  • Question 3 : int 32768 -32768 is converted to short?
  • Problems Solution 3 : 32768 = (1,000,000,000,000,000) 2 is converted to short, the most significant bit becomes a sign bit, so the difference is negative, is represented by the complement, is converted to decimal inverted +1: 011111111111 = 1111 + 1 = 1,000,000,000,000,000 (32768) 10. Minus sign: -32768

Code hosting

Last week exam wrong question summary

  1. Say you write a program that makes use of the Random class, but you fail to include an import statement for java.util.Random (or java.util.*). What will happen when you attempt to compile and run your program.
    A .The program won't run, but it will compile with a warning about the missing class.
    B .The program won't compile-you'll receive a syntax error about the missing class.
    C .The program will compile, but you'll receive a warning about the missing class.
    D .The program will encounter a runtime error when it attempts to access any member of the Random class
    E .none of the above
    正确答案: B 我的答案: A
    解析: The missing class means that there will be undefined variables and / or methods The compiler will detect these and will issue error messages Your program will not be executable (missing class means there will be an undefined variable or method.... the compiler will detect them and issue an error message. your program will not be executed.)
  2. The following Enumeration the Consider
    enum Speed { FAST, MEDIUM, SLOW };
    A ORDINAL .The value of IS MEDIUM, 2
    B ORDINAL .The value of IS SLOW. 1
    C .The The name of Whose ORDINAL Speed Enumeration value IS IS ZERO the FAST
    D .The name of The Speed Enumeration value IS One Whose ORDINAL SLOW iS
    E .None of above at the
    correct answer: C my answer: a
    parsing :. Enumerations have have ORDINAL values Beginning AT ZERO, in left-to-right Ascending the Order (So), Speed.FAST.ordinal( )iS ZERO, Speed.MEDIUM.ordinal( )iS One, and Speed.SLOW.ordinal( )iS TWO (medals. for ordinal value from zero, are arranged in ascending order from left to right, so speed.fast.ordinal()is 0, speed.medium.ordinal()1, speed.slow.ordinal()is 2.)
  3. You need not to Import IF only at The Top of A-Level Package Penalty for, But All the ITS AS Secondary Levels Well, you Should the Write: import package.*.*;
    A .true
    B .false
    correct answer: B I answer: A
    parsed : import statement and only for use with a single * (wildcard). If you need to import all of two packages, they must be explicitly written:
    import package.A.*;
    import package.B.*;
  4. Example of passing to the Message AN A String the WHERE has the Message at The A String Which of the Parameter Would Occur in at The following messages?
    A .length
    B .substring
    C .equals
    D .toUpperCase
    E at The .none of above, IT IS not Possible to Pass A String as a parameter in a message to a String
    correct answer: C my answer: B
    parsing : length and touppercase message has no parameters, substring has two int parameters. For equals, the string must be passed as a parameter to the message character string is compared with the received character string passed as a parameter.
  5. In a UML diagram for a class
    A .classes are represented as rectangles
    B .there may be a section containing the name of the class
    C .there may be a section containing the attributes (data) of the class
    D .there may be a section containing the methods of the class
    E .all of the above
    正确答案: E 我的答案: A
    解析: Those four attributes correctly describe a UML representation of a class.

Pair peer review and

Learning progress bar

The number of lines of code (add / accumulate) Blog amount (add / accumulate) Learning time (add / accumulate) Important growth
aims 10,000 lines 30 400 hours
the first week 254/254 2/2 21/21 Start simple programming
the second week 132/386 1/3 26/47 Learn to use the Scanner class
The third week 632/1018 2/5 21/68 Learn to use some common class
the fourth week 663/1681 2/7 27/95 junit test and writing classes
  • Plan study time: 20 hours

  • The actual study time: 27 hours

Reference material

In-depth understanding of iterators
static understanding

Guess you like

Origin www.cnblogs.com/zdyyy/p/11614162.html