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 Welcome back!  How was your recess?  How are you feeling after the recess?

3 3 This is Week 7  Week 6:  Chapter 10: Arrays  only sections 10.1 to 10.6  Modular programming  Writing separate methods  This week:  Revision on Modular programming  Chapter 6: Object-Oriented Programming  Chapter 7: OOP Additional Details  Survey

4 4 Modular Programming Revisit  The following topics are not restricted to OOP:  Local variables  Local variable persistence  return statement  void return type  Empty return statement  Argument passing  The above are applicable with or without OOP. I will go through them again with you using the examples we discussed in our previous lecture.

5 5 NRIC Check Code  Download these programs:  CheckNRIC.java, NewCheckNRIC.java and NewV2CheckNRIC.java  Compare NewCheckNRIC.java with NewV2CheckNRIC.java.  What changes did we make in the latter?  Can you redo findCode() using a String? A character array?  I will use the debugger in DrJava  Illustrate “step into” and “step out”.  Set up watches for local variables i, step2, number and num.  Set up watches for digits * as well. And see how cool it is! * To trace an array in watches, you need the latest DrJava version (currently, it is drjava-stable-20080904-r4668.jar) which is available on http://drjava.sourceforge.net/http://drjava.sourceforge.net/

6 6 Local variable persistence  From the debugger session, you observe that  The variable i is only persistent (usable) within the for loop that it is declared.  The value of number in main() method is copied into the parameter num in generateCheckCode() method (this is called passed by value). Subsequent changes to num does not affect number, because they are two separate ‘boxes’.  The parameter num appears in two methods ( generateCheckCode() and findCode() ), and both num are distinct. They are only persistent in the method that they are declared.

7 7 Cohesion: Writing cohesive methods  Suppose we change the findCode() method to the following. What’s wrong with this? public static char findCode(int num) { char code; if (num < 10) code = (char) ('A' + num - 1); else if (num == 10) code = 'Z'; else // if (num == 11) code = 'J'; System.out.println("The code is " + code); return code; }// end findCode  Good programming practice: Do not mix computation with input/output in a method!

8 8 return statement (1/3)  A method may return some value, or it may not return anything.  If it returns a value, it must have a return type, and there must be a return statement in the method to return a value of that type. public static char findCode(int num) { if (num < 10) return (char) ('A' + num - 1); else if (num == 10) return 'Z'; else return 'J'; }// end findCode value returned must be of the same type as return type return type

9 9 return statement (2/3)  If it does not return any value, then its return type is void.  The return statement is optional. public static void displayInstructions() { System.out.println("This is a game."); System.out.println("To play the game,..."); return; }// end displayInstructions optional

10 10 return statement (3/3)  In some programming languages, for example Pascal, they are called function (that returns some value) and procedure (that does not return any value).  Can a method return more than one value?  No, a method can only return one “thing”...  But the “thing” can be a reference, and a reference can be an object reference that contains many data, or an array reference that contains many elements. So...

11 11 Tracing codes  Hand trace the programs in the given handout and write out the outputs.

12 12 OOP (1/2)  How do we see the world?  As a world of “things”.  These things have properties and behaviours.  In OOP, the program models these things as objects.  Objects come from classes.

13 13 OOP (2/2)  So far, the classes we have written do not make use of OOP features.  Why? Because we don’t create objects out of these classes.  But we have seen and used classes where we need to create objects out of them.  Examples: String, Scanner, Random, DecimalFormat (the last 2 are not even covered in the textbook)  Now, we will learn how to write such classes where we can create objects out of them, and manipulate the objects. (Hooray!)  Let’s go on to Chapter 6!

14 14 Class Activity: Ball class  Create a Ball class  What questions must you first ask?  What are its properties?  What are its behaviours?  For the identified properties, we implement them as instance variable.  For the identified behaviours, we implement them as methods.  Write a driver program BallDriver to test it.  Keep your Ball class for future reference. We are going to enhance it as we go along.

15 15 Constructor  We discussed the accessor method and mutator method in Chapter 6.  There is another type of method, called the constructor, which is covered in Chapter 7.  However, I will introduce it to you now, using our Ball class as an example.

16 16 Chapter 7 OOP Additional Details  I will now carry on to Chapter 7 to cover some additional details we have put off in Chapter 6.  Some of these I might have covered informally before.

17 17 Announcement/Reminder  Lab #3  Release: 30 September (Tuesday), 2359hr.  Deadline: 8 October (Wednesday), 2359hr.  Identical codes  Please do not share codes for your lab assignments!  Mid-term test  4 October, Saturday, 12noon, LT15 (for CS1101X students)  Refer to course website for more info: http://www.comp.nus.edu.sg/~cs1101x/3_ca/termtests.html http://www.comp.nus.edu.sg/~cs1101x/3_ca/termtests.html

18 18 This is Week 7  Next week?  Chapter 7 OOP – Additional Details (if we cannot complete it today)  Chapter 8 Software Engineering  Chapter 9 Classes with Class Members (if time permits)

19 19 End of file


Download ppt "CS1101: Programming Methodology Aaron Tan."

Similar presentations


Ads by Google