20172317 2017-2018-2 "Program Design and Data Structure" Week 7 Learning Summary
Brief conclusion of this chapter
- Understood the concept of "Inheritance",for example:
- creating subclasses
- using the reversed word
extended
- REMEMBER!An instance of a chiild class dose not rely on an instance of the parent class
- Understood the usage of the super reference(maybe)
- by utilizing
super
,a subclass can grant access to a parent's members
- by utilizing
- In Java, a derived class can have only one parent's class
- this trait is known as single inheritence
- A child class can override the parent's defination of an inherited method
- however,a final method can't be overriden
- Understood the concept of object class(maybe)
Problems while studying the textbook and the solutions
- Problem 1:great?
Solutions of Problem 1:The textbook mentioned the
super
reference,stated that"one use of of thesuper
reference is to invoke a parent's constructor"For I can not quite understand what exactly the reserved wordsuper
can bring about, here I do a little test:
The tested object is LISTING 9.6 on the text book, which utilizedsuper(numPages)
.Here I first remove the codesuper(numPages)
,then I tried addpages = numPages
after I removedsuper(numPages)
.Under both circumstances I got this error
Familiar,isn't it?This is the error I often encountered when I was studying chapter 4 & 7.This error means there is supposed to be an integer in the brackets of the constructor
Book2(int numPages)
in Book2.java.Howerver there is no value in it now.
As for whysuper(numPages)
won't lead to such problems, I don't know either,so I just recourse to the internet.After some investigation I found two different ways ofsuper
to reference parent.According to the internet,super()
will invoke the constructor of a parent,whilesuper(parameter)
will invoke the constructor with the same formal parameters in a parent.To distinguishsuper()
fromsuper(parameter)
, I removedsuper(numPages)
withsuper()
in Dictionary2.java, trying to get some useful output.Turns out the result is the same error message like before[pic]Although some question still bother me, fortunately an example explaned everything,the following is the example:public class Person { public static void prt(String s) { System.out.println(s); } Person() { prt("父类·无参数构造方法: "+"A Person."); }//构造方法(1) Person(String name) { prt("父类·含一个参数的构造方法: "+"A person's name is " + name); }//构造方法(2) }
public class Chinese extends Person { Chinese() { super(); // 调用父类构造方法(1) prt("子类·调用父类”无参数构造方法“: "+"A chinese coder."); } Chinese(String name) { super(name);// 调用父类具有相同形参的构造方法(2) prt("子类·调用父类”含一个参数的构造方法“: "+"his name is " + name); } public static void main(String[] args) { Chinese cn = new Chinese(); cn = new Chinese("codersai"); } }
The first one is the code of parent class,the secound is the code of subclass.Actually I can't quite understand why there are statements such as
So with the informations above,here is my speculation:public static void prt(String s)
,or why the subclass include the instantiation statement. Anyway, the following picture is the output of these code.
super()
will invoke the constructor which never used any parameters in the parent class,whilesuper(parameter)
will invoke the constructor which has the same type of parameter in the parent class.
super
is also mentioned in overriding methods, for this I found an explanation on the internet:super
can be understood as a pointer that points towards it's parent's class.Therefore you can invoke members in the parent's class bysuper.xxx
- Problem 2:protected?
Solutions of problem 2:The reserved word
protected
was first encountered when I was searching data ofprivate
in the learning of chapter 4.But I....er.....didn't take it seriously........
So I come back to search those data.For the access modifierprotected
:"It's an access modifier betweenpublic
andprivate
.Any class that is modified withprotected
,can only be accessed by the method of it's own or the methods of it's subclass,even if the subclass is in a different package."Which meansprotected
modifier's encapsulation is not as good asprivate
but better thanpublic
.After replaceprotected
withpriavte
inprotected int pages
in the LISTING 9.5(Book2.java),an error message states that "variable pages has private access in Book2.java",which proved the theory,with the futher explanation of the usage of modifierprivate
,which I concluded while studying chapter 4.
Well....I found this on the internet.It explains the visibility of each modifier.It seems thatdefault
is the modifier yet to be learned.......
Problems while coding and the solutions
- Question 1: Hmm? Not finished the blog yet
- Problem 1 Solution: XXXXXX
- Question 2: XXXXXX
- Problem 2 Solution: XXXXXX
- ...
code hosting
(screenshot of the running result of the statistics.sh script)
Conclusions of mistakes in the last week's test
3.The "off-by-one" error associated with arrays arises because
The correct answer is A,I chose B
A.the first array index is 0 and programmers may start at index 1, or may use a loop that goes one index too far
B.the last array index is at length + 1 and loops may only iterate to length, missing one
C.the last array element ends at length - 1 and loops may go one too far
D.programmers write a loop that goes from 0 to length - 1 whereas the array actually goes from 1 to length
E.none of the above, the "off-by-one" error has nothing to do with arrays
The explanation goes:"The array is initialized as = new type[x] where x is the size of the array. However, the array has legal indices of 0 to x - 1 and so, programmers are often off-by-one because programmers will write code to try to access indices 1 to x."
Well......this is the first time I encounter the concept "off-by-one",and explanation is still ambiguous as ever.So here is an example for better understanding:
Sometimes you intended to perform a loop n times and write something like this:for (int i =0; i < n; ++i) { ... }
.However,you may write something like this.....for (int i = 1; i < n; ++i) { ... }
or this......for (int i = 0; i <= n; ++i) { ... }
.The last two statement is so-called "off-by-one"8.Assume that BankAccount is a predefined class and that the declaration BankAccount[ ] firstEmpireBank; has already been performed. Then the following instruction reserves memory space for
The correct answer is B,I chose A
firstEmpireBank = new BankAccount[1000];
A.a reference variable to the memory that stores all 1000 BankAccount entries
B.1000 reference variables, each of which point to a single BankAccount entry
C.a single BankAccount entry
D.1000 BankAccount entries
E.1000 reference variables and 1000 BankAccount entries
The declaration BankAccount[ ] firstEmpireBank; reserves memory space for firstEmpireBank, which itself is a reference variable that points to the BankAccount[ ] object. The statement firstEmpireBank = new BankAccount[1000]; instantiates the BankAccount[ ] object to be 1000 BankAccount objects. This means that firstEmpireBank[0] and firstEmpireBank[1] and firstEmpireBank[999] are all now legal references, each of which is a reference variable since each references a BankAccount object. So, the statement reserves memory space for 1000 reference variables. Note that none of the 1000 BankAccount objects are yet instantiated, so no memory has been set aside yet for any of the actual BankAccount objects.9.If x is a char, and values is an int array, then values[x]
A.causes a syntax error
B.causes an Exception to be thrown
C.casts x as an int based on x's position in the alphabet (for instance, if x is 'a' then it uses 0 and if x is 'z' then it uses 25)
D.casts x as an int based on x's ASCII value (for instance, if x is 'a' then it uses 97 and if x is 'z' then it uses 122)
E.casts x as an int based on the digit that is stored in x (for instance, if x is '3' it uses 3) but throws an exception if x does not store a digitThe correct answer is D,and due to some unknown reasons, I chose E,even if I know that the correct answer is D.......
Pairing and mutual evaluation
Grading
- Correct use of Markdown syntax (plus 1 point):
- No points for not using Markdown
- No extra points for grammatical errors (links can't be opened, tables are incorrect, lists are incorrect...)
- No points for messy typography
- The elements in the template are complete (plus 1 point)
- No points for lack of "Problems and Solving Processes in Textbook Learning"
- No bonus points for missing "Issues in Code Debugging and Resolution Processes"
- No bonus points for code hosting that cannot be opened
- No points will be added for those that cannot be opened without "pairing and mutual evaluation"
- No extra points for missing "summary of last week's exam mistakes"
- Missing "progress bar" can not add points
- No extra points for missing "references"
Problems and solving process in teaching material learning, one point is added for each problem
Problems and solutions in code debugging, plus 1 point for each problem
- This week's valid code exceeds 300 branches (plus 2 points)
- No extra points for submissions less than 20 times a week
- Other bonus points:
- 1 point for blogging before Friday
- Feelings, experience is not fake plus 1 point
- Nice typography plus a point
- Add 1 point for recording the learning time and improvement in the progress bar
- 1 point for writing new code by hand
- After-class multiple-choice questions with verification plus 1 point
- 1 point for code Commit Message specification
- Add 1 point for in-depth study of wrong questions
- Serious comments, plus 1 point for pointing out problems in the blog and code
- 1 point for true and credible paired learning
- Deductions:
- 0 points for plagiarism
- Code cheating will be deducted to 0 points
- 0 points for late work
Review Template:
- Worth learning or questions from the blog:
- xxx
- xxx
- ...
- Worth learning or problems in the code:
- xxx
- xxx
- ...
Based on the scoring criteria, I give this blog a score: XX points. The scores are as follows: xxx
Reviewed classmates blog and code
- Pair study this week
- Paired student number 1
- pairing photos
- Pair learning content
- XXXX
- XXXX
- ...
- Last week's blog comments
Others (perception, thinking, etc., optional)
xxx
xxx
learning progress bar
Lines of code (added/accumulated) | Blog volume (new/cumulative) | Study time (added/accumulated) | important growth | |
---|---|---|---|---|
Target | 5000 lines | 30 articles | 400 hours | |
the first week | 200/200 | 2/2 | 20/20 | |
the second week | 300/500 | 2/4 | 18/38 | |
The third week | 500/1000 | 3/7 | 22/60 | |
the fourth week | 300/1300 | 2/9 | 30/90 |
Try recording "planned study time" and "actual study time", and see if you can improve your planning ability at the end of the due date. This work study is very important and very useful.
The formula for time-consuming estimation: Y=X+X/N, Y=XX/N, the more training times, the closer X and Y are.
Reference: Why is the estimation of software engineering software so difficult , software engineering estimation methods
Planned study time: XX hours
Actual study time: XX hours
Improvements:
(If you have time, take a look at the Modern Software Engineering Courseware
Software Engineer Ability Self-Assessment Form )