CS1101: Programming Methodology Aaron Tan.

Slides:



Advertisements
Similar presentations
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
Advertisements

Written by: Dr. JJ Shepherd
CS1101: Programming Methodology
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 41 Defining Classes and Methods Chapter 4.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Development Software Life Cycle UML Diagrams.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++
Slide 1. Slide 2 Administrivia Nate's office hours are Wed, 2-4, in 329 Soda! TA Clint will be handing out a paper survey in class sometime this week.
1 Data Structures Topic #1 Welcome !. 2 Today’s Agenda Introduction...what to expect!?! Talk about our Goals and Objectives Textbook is highly recommended.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
CS1101: Programming Methodology
CS1101: Programming Methodology Aaron Tan.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
CS1101: Programming Methodology Aaron Tan.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 10 Thinking in Objects.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
CS 101E – Exam 2 Review Spring 2007 Michele Co. Announcements Review Session Tonight, 7/7:30 p.m., OLS 009 Will be announced via In-class Exam Wednesday.
1 CS Programming Languages Class 15 October 17, 2000.
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology Aaron Tan.
Python – May 11 Briefing Course overview Introduction to the language Lab.
Agenda Object Oriented Programming Reading: Chapter 14.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
CS1101: Programming Methodology
CS1101: Programming Methodology Aaron Tan.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the structure, union, and enumerated types ❏ To use the type definition.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CS1101: Programming Methodology Aaron Tan.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
Written by: Dr. JJ Shepherd
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
CS1101: Programming Methodology
A High Flying Overview CS139 – Fall 2006 How far we have come.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 CS100J Spring Instructor: David Gries CS100M: Matlab No prior programming experience One semester of calculus Math & engineering type problems.
PROBLEM SOLVING AND PROGRAMMING ISMAIL ABUMUHFOUZ | CS 170.
C++ Functions A bit of review (things we’ve covered so far)
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
A High Flying Overview CS139 – Fall 2010 How far we have come.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design II Lecture 7, Thu Jan
CS1101: Programming Methodology Aaron Tan.
Objects as a programming concept
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Lecture 14 Writing Classes part 2 Richard Gesick.
More Object Oriented Programming
CS139 – Fall 2010 How far we have come
Week 4 Lecture-2 Chapter 6 (Methods).
CS1100 Computational Engineering
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Classes and Objects Systems Programming.
Presentation transcript:

CS1101: Programming Methodology Aaron Tan

2 Welcome back!  How was your recess?  How are you feeling after the recess?

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 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 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 r4668.jar) which is available on

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 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 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 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 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 Tracing codes  Hand trace the programs in the given handout and write out the outputs.

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

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 End of file