CS1101: Programming Methodology Aaron Tan

advertisement
CS1101: Programming Methodology
http://www.comp.nus.edu.sg/~cs1101x/
Aaron Tan
This 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.
2
Issues 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
3
Overriding 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 don’t work properly because they
are not customised for that class.
4
Overriding 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 didn’t define them in Ball.java. Here’s a sample run:
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@1d8957f
2nd ball: Ball@3ee284
 But they don’t 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.
5
Overriding 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
 Now they work.
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.Point[x=3,y=8]]
2nd ball: [red, 2.5, java.awt.Point[x=3,y=8]]
6
Overriding 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)
7
Overriding 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
public boolean equals(BallV2 ball) {
String colour1 = this.getColour();
double radius1 = this.getRadius();
Point centre1 = this.getCentre();
public boolean equals(Object o) {
if (o instanceof BallV3) {
String colour1 = this.getColour();
double radius1 = this.getRadius();
Point centre1 = this.getCentre();
String colour2 = ball.getColour();
double radius2 = ball.getRadius();
Point centre2 = ball.getCentre();
BallV3
String
double
Point
return (colour1.equals(colour2))
&& (radius1 == radius2)
&& (centre1.equals(centre2));
ball = (BallV3) o;
colour2 = ball.getColour();
radius2 = ball.getRadius();
centre2 = ball.getCentre();
return (colour1.equals(colour2))
&& (radius1 == radius2)
&& (centre1.equals(centre2));
}
equals() method in BallV2.java
}
else
return false;
}
equals() method in BallV3.java
8
Overriding 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
String
double
Point
ball = (BallV3) o;
colour2 = ball.getColour();
radius2 = ball.getRadius();
centre2 = ball.getCentre();
return (colour1.equals(colour2)) && (radius1 == radius2)
&& (centre1.equals(centre2));
}
else
return false;
}
9
Examinations (1/3)
 CS1101 Exam
 27 November 2008, Thursday
 5 – 7 pm
 Venue to be announced by Registrar’s
Office
 Shall we wear yellow again? 
 Format
 No MCQ
 Some short-answer questions and some
programming questions
10
Examinations (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 didn’t
cover them this time)
 Regular expressions
 Inheritance and polymorphism
 Recursion
11
Examinations (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/index.html
12
Announcement/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)
13
What’s 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/polymorphism, 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
What’s next? (2/2)
 CS1102
 Data Structures and Algorithms
 Textbook: Data Abstraction and Problem
Solving with JAVA: Walls and Mirrors
by Frank M. Carrano and Janet J. Pitchard
 Companion website:
ftp://ftp.aw.com/cseng/authors/carrano/java/
 Prepare yourself well!
15
ALL THE BEST!
16
End of file
17
Download