Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1101: Programming Methodology Aaron Tan.

Similar presentations


Presentation on theme: "CS1101: Programming Methodology Aaron Tan."— Presentation transcript:

1 CS1101: Programming Methodology http://www.comp.nus.edu.sg/~cs1101x/ http://www.comp.nus.edu.sg/~cs1101x/ Aaron Tan

2 2 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.

3 3 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

4 4 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.

5 5 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.

6 6 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 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]]  Now they work.

7 7 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)

8 8 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(); String colour2 = ball.getColour(); double radius2 = ball.getRadius(); Point centre2 = ball.getCentre(); return (colour1.equals(colour2)) && (radius1 == radius2) && (centre1.equals(centre2)); } equals() method in BallV2.java 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; } equals() method in BallV3.java

9 9 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 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; }

10 10 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

11 11 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

12 12 Examinations (3/3)  Examination Directory  http://www.nus.edu.sg/registrar/event/examdir.html http://www.nus.edu.sg/registrar/event/examdir.html  Preparing for exams  http://www.cdtl.nus.edu.sg/examprep/ http://www.cdtl.nus.edu.sg/examprep/  Tips on managing study and exam stress:  http://www.nus.edu.sg/uhwc/counselling/selfhelp/index.html http://www.nus.edu.sg/uhwc/counselling/selfhelp/index.html

13 13 Announcement/Reminder  Lab and PE marks  Please check (later this week) http://www.comp.nus.edu.sg/~cs1101x/3_ca/ca_marks.html 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)

14 14 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!

15 15 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/ ftp://ftp.aw.com/cseng/authors/carrano/java/  Prepare yourself well!

16 16 ALL THE BEST!

17 17 End of file


Download ppt "CS1101: Programming Methodology Aaron Tan."

Similar presentations


Ads by Google