Title: CS1101: Programming Methodology http://www.comp.nus.edu.sg/~cs1101x/
1CS1101 Programming Methodology
http//www.comp.nus.edu.sg/cs1101x/
Aaron Tan
2This is Week 13
- This week
- Chapter 12 Aggregation, Composition, and
Inheritance - On your own
- Read up Chapter 13 Inheritance and Polymorphism
- Read up on Recursion
- Textbook Appendix 8 and lecture website
- The above are not in the scope of the final
examination.
3Issues not discussed
- There are certain issues we have not discussed,
such as - The four access modifiers private, public,
protected and default. - Abstract classes and interfaces
4Overriding Methods (1/6)
- You are aware that every class has a default
constructor, when you do not write one. - Such a constructor is inherited from the Object
class, which is the superclass of all classes. - (I never understand why they give such a name
Object to this class. This is so confusing!) - What other things are inherited from the Object
class? The toString() method and the equals()
method. - This means that you can use toString() and
equals() in your class, even though you have not
written them. - However, most likely they dont work properly
because they are not customised for that class.
5Overriding Methods (2/6)
- Refer to Ball.java and BallDriver.java
- In BallDriver.java, we make use of equals()
method, and also toString() method (in the last
two println() statements), even though we didnt
define them in Ball.java. Heres a sample run
- But they dont work (to your expectation),
because the inherited equals() method compares
the addresses of the Ball objects, and the
toString() method displays the hashcode of the
address.
Enter 1st ball's colour red Enter 1st ball's
radius 2.5 Enter 1st ball's centre 3 8 Enter
2nd ball's colour red Enter 2nd ball's radius
2.5 Enter 2nd ball's centre 3 8 They are not
equal. 1st ball Ball_at_1d8957f 2nd ball
Ball_at_3ee284
6Overriding Methods (3/6)
- Refer to BallV2.java and BallV2Driver.java
- We write our own toString() method, which
overrides the inherited toString() method. This
is called overriding method. - We also write our own equals() method. But this
is not an overriding method. (To be explained.)
Enter 1st ball's colour red Enter 1st ball's
radius 2.5 Enter 1st ball's centre 3 8 Enter
2nd ball's colour red Enter 2nd ball's radius
2.5 Enter 2nd ball's centre 3 8 They are
equal. 1st ball red, 2.5, java.awt.Pointx3,y8
2nd ball red, 2.5, java.awt.Pointx3,y8
7Overriding Methods (4/6)
- Why is the equals() method in BallV2 not an
overriding method? - Because it has this heading (signature)
public boolean equals(BallV2 ball)
- The inherited equals() method, which comes from
the Object class, has this heading (signature)
public boolean equals(Object o)
8Overriding Methods (5/6)
- If we truly want to provide an overriding method
for equals(), we must stick to the second
heading. - The code needs to be changed. See BallV3.java
9Overriding Methods (6/6)
- The instanceof operator checks whether o is
indeed a reference to a BallV3 object. - You may omit the if statement if you are sure
you always pass a BallV3 reference to o. - Why do we need the (BallV3) cast?
public boolean equals(Object o) if (o
instanceof BallV3) String colour1
this.getColour() double radius1
this.getRadius() Point centre1
this.getCentre() BallV3 ball (BallV3)
o String colour2 ball.getColour()
double radius2 ball.getRadius() Point
centre2 ball.getCentre() return
(colour1.equals(colour2)) (radius1
radius2) (centre1.equals(centre2
)) else return false
10Examinations (1/3)
- CS1101 Exam
- 27 November 2008, Thursday
- 5 7 pm
- Venue to be announced by Registrars Office
- Shall we wear yellow again? ?
- Format
- No MCQ
- Some short-answer questions and some programming
questions
11Examinations (2/3)
- Scope
- Chapters 1 15 (except 12 and 13)
- Everything you learned in labs and discussion
sessions. - For Chapter 15 Files, only on text files.
- Topics that appear quite often in past-years
papers but are not tested (because we didnt
cover them this time) - Regular expressions
- Inheritance and polymorphism
- Recursion
12Examinations (3/3)
- Examination Directory
- http//www.nus.edu.sg/registrar/event/examdir.html
- Preparing for exams
- http//www.cdtl.nus.edu.sg/examprep/
- Tips on managing study and exam stress
- http//www.nus.edu.sg/uhwc/counselling/selfhelp/in
dex.html
13Announcement/Reminder
- Lab and PE marks
- Please check (later this week) http//www.comp.nus
.edu.sg/cs1101x/3_ca/ca_marks.html - Consultation
- 13 November, 2008, Thursday, 2 4 pm
- My office (COM1-03-12)
14Whats next? (1/2)
- We have come to the end of CS1101! ? or ??
- Have you learned everything about programming?
- No. There are issues such as inheritance/polymorph
ism, efficiency, etc. - No. Even for issues we have discussed, you will
need more practice. For example, design aspect. - A correct program may not be a good program!
14
15Whats next? (2/2)
- CS1102
- Data Structures and Algorithms
- Textbook Data Abstraction and Problem Solving
with JAVA Walls and Mirrorsby Frank M. Carrano
and Janet J. Pitchard - Companion website ftp//ftp.aw.com/cseng/authors/
carrano/java/ - Prepare yourself well!
15
1616
17End of file
17