More About Methods. Hsuan-Tien Lin. Deptartment of CSIE, NTU. OOP Class, March 8-9, 2010

Similar documents
Basic Java OOP 10/12/2015. Department of Computer Science & Information Engineering. National Taiwan University

More on Methods and Encapsulation

Class versus Instance (Section 5.1)

Array 10/26/2015. Department of Computer Science & Information Engineering. National Taiwan University

Instance Methods and Inheritance (1/2)

Encapsulation 10/26/2015. Department of Computer Science & Information Engineering. National Taiwan University

Generics. Hsuan-Tien Lin. OOP Class, May 23, Department of CSIE, NTU. H.-T. Lin (NTU CSIE) Generics OOP 05/23/ / 16

The Midterm Exam. Hsuan-Tien Lin. Department of CSIE, NTU. OOP Class, April 26-27, 2010

Inheritance. Hsuan-Tien Lin. OOP Class, March 29-30, Department of CSIE, NTU. H.-T. Lin (NTU CSIE) Inheritance OOP 03/29-30/ / 41

ITI Introduction to Computing II

Polymorphism 11/09/2015. Department of Computer Science & Information Engineering. National Taiwan University

ITI Introduction to Computing II

Inheritance 11/02/2015. Department of Computer Science & Information Engineering. National Taiwan University

Object Oriented Software Design (NTU, Class Even, Spring 2009) Final Examination Problem Sheet TIME: 06/16/2009, 14:20 17:20

Introduction to functions

1 Java Night Countdown (30%)

Sharing Objects. Pieter van den Hombergh. Fontys Hogeschool voor Techniek en Logistiek. February 15, 2017

Lecture 14: Nov. 11 & 13

1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

Constructors - Cont. must be distinguished by the number or type of their arguments.

Announcements. Project #1 grades were returned on Monday. Midterm #1. Project #2. Requests for re-grades due by Tuesday

Elementary Sorts 1 / 18

Introduction to Programming (Java) 3/12

Voortgezet Programmeren

Python. chrysn

Locksmith: Context-Sensitive Correlation Analysis for Race Detection

Course Announcements. Bacon is due next Monday. Next lab is about drawing UIs. Today s lecture will help thinking about your DB interface.

Programming Language Concepts, CS2104 Lecture 3

Lisp Introduction. Dr. Neil T. Dantam. Spring CSCI-498/598 RPM, Colorado School of Mines. Dantam (Mines CSCI, RPM) Lisp Spring / 88

Computer Science Introductory Course MSc - Introduction to Java

Quicksort. Recurrence analysis Quicksort introduction. November 17, 2017 Hassan Khosravi / Geoffrey Tien 1

Design Patterns and Refactoring

CMSC 132, Object-Oriented Programming II Summer Lecture 10:

Formal Reasoning CSE 331. Lecture 2 Formal Reasoning. Announcements. Formalization and Reasoning. Software Design and Implementation

Comp 11 Lectures. Mike Shah. July 12, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 12, / 33

DNHI Homework 2 Solutions Recursion

CS 6110 Lecture 28 Subtype Polymorphism 3 April 2013 Lecturer: Andrew Myers

Algorithm Design and Analysis

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

CMSC 441: Algorithms. NP Completeness

Math 3A Winter 2016 Midterm

6.001 Recitation 22: Streams

Assignment #2 COMP 3200 Spring 2012 Prof. Stucki

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 07/2014 SESSION 8:00-10:00

ECEN 651: Microprogrammed Control of Digital Systems Department of Electrical and Computer Engineering Texas A&M University

Techniques of Java Programming

Compiling Techniques

CMSC 132, Object-Oriented Programming II Summer Lecture 1:

Programming Languages Fall 2013

CA Compiler Construction

Notes from Yesterday s Discussion. Big Picture. CIS 500 Software Foundations Fall November 1. Some lessons.

2 - Strings and Binomial Coefficients

DARN: A Matrix Manipulation Language

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

Adam Blank Spring 2017 CSE 311. Foundations of Computing I. * All slides are a combined effort between previous instructors of the course

Problem Decomposition: One Professor s Approach to Coding

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

Practice Midterm Exam

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Chapter 4. A simple method for solving first order equations numerically

MiniMat: Matrix Language in OCaml LLVM

Asymptotic Running Time of Algorithms

List reversal: back into the frying pan

Introduction to lambda calculus Part 2

JChartLib ver 1.0. Silvio Schneider, June 7, Introduction 3. 2 Chart and Dataset 4. 3 Adding data to a chart 4

Formal Languages. We ll use the English language as a running example.

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 09/06/2014 SESSION 8:30-10:30

Software Engineering

Course Announcements. John Jannotti (cs32) Scope, Collections & Generics Feb 8, / 1

Machine Learning Foundations

Examples of Dynamic Binding in.net

Probability and Samples. Sampling. Point Estimates

16.1 Countability. CS125 Lecture 16 Fall 2014

Analysis of Algorithms

Lectures on Separation Logic. Lecture 2: Foundations

INTRODUCTION. This is not a full c-programming course. It is not even a full 'Java to c' programming course.

CompA - Complex Analyzer

CS173 Running Time and Big-O. Tandy Warnow

A Formal Model and an Algorithm for Generating the Permutations of a Multiset

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

Functional Programming with F# Overview and Basic Concepts

Java Programming. Final Examination on December 13, 2015 Fall 2015

Numerical differentiation

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014

A Practical Comparison of Agile Web Frameworks

Formal Languages. We ll use the English language as a running example.

Auto-Tuning Complex Array Layouts for GPUs - Supplemental Material

Turing Machines Part Three

CS Exam 1 Study Guide and Practice Exam

Mathematical Foundations of Programming. Nicolai Kraus. Draft of February 15, 2018

Softwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany

NP-Completeness I. Lecture Overview Introduction: Reduction and Expressiveness

Softwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany

Liveness based Garbage Collection

Lecture 4: Stacks and Queues

Programsystemkonstruktion med C++: Övning 1. Karl Palmskog Translated by: Siavash Soleimanifard. September 2011

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, 2018

CS 301. Lecture 17 Church Turing thesis. Stephen Checkoway. March 19, 2018

Turing machines and linear bounded automata

Transcription:

More About Methods Hsuan-Tien Lin Deptartment of CSIE, NTU OOP Class, March 8-9, 2010 H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 0 / 24

Methods: the Basic Method (1/2, Callee s View) 1 p u b l i c class OOPStudent { 2 p u b l i c i n t score ; 3 p u b l i c S t r i n g name ; 4 p u b l i c void set_score ( i n t new_score ) { score = new_score ; } 5 p u b l i c S t r i n g get_name ( ) { r e t u r n name ; } 6 p u b l i c double get_adjusted ( ) { r e t u r n (60 + score 0. 4 ) ; } 7 } method: what I (the object) do parameter: what I get from the caller return value: what I want to tell the caller H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 1 / 24

Methods: the Basic Method (2/2, Caller s View) 1 p u b l i c class OOPStudent { 2 p u b l i c i n t score ; 3 p u b l i c S t r i n g name ; 4 p u b l i c void set_score ( i n t new_score ) { score = new_score ; } 5 p u b l i c S t r i n g get_name ( ) { r e t u r n name ; } 6 p u b l i c double get_adjusted ( ) { r e t u r n (60 + score 0. 4 ) ; } 7 } 1 p u b l i c class OOPStudentDemo { 2 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 3 OOPStudent CharlieL = new OOPStudent ( ) ; 4 CharlieL. set_score (80) ; 5 System. out. p r i n t l n ( CharlieL. get_adjusted ( ) ) ; 6 System. out. p r i n t l n ( CharlieL. get_name ( ) ) ; 7 } 8 } method: what I (caller) want the object to do argument: what I tell the callee return value: what I want to hear from the callee H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 2 / 24

Methods: the Basic Method: Key Point method: an abstraction of action, where information is passed through argument/parameter and return values H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 3 / 24

Methods: Value Passing Return Values (1/1) 1 p u b l i c class OOPStudent { 2 p u b l i c i n t score ; 3 p u b l i c S t r i n g name ; 4 p u b l i c void set_score ( i n t new_score ) { score = new_score ; } 5 p u b l i c S t r i n g get_name ( ) { r e t u r n name ; } 6 p u b l i c double get_adjusted ( ) { r e t u r n (60 + score 0. 4 ) ; } 7 } 1 p u b l i c class OOPStudentDemo { 2 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 3 OOPStudent CharlieL = new OOPStudent ( ) ; 4 CharlieL. set_score (80) ; 5 System. out. p r i n t l n ( CharlieL. get_adjusted ( ) ) ; 6 System. out. p r i n t l n ( CharlieL. get_name ( ) ) ; 7 } 8 } void: must-have to mean no return value primitive/extended return types possible H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 4 / 24

Methods: Value Passing Return Values: Key Point void for no return value H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 5 / 24

Methods: Value Passing Primitive Argument/Parameter (1/1) 1 p u b l i c class Tool { 2 p u b l i c void swap ( i n t f i r s t, i n t second ) { 3 i n t tmp = f i r s t ; 4 f i r s t = second ; 5 second = tmp ; 6 System. out. p r i n t l n ( f i r s t ) ; 7 System. out. p r i n t l n ( second ) ; 8 } 9 } 10 p u b l i c class Demo{ 11 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 12 Tool t = new Tool ( ) ; 13 i n t i = 3; i n t j = 5; 14 t. swap ( i, j ) ; 15 System. out. p r i n t l n ( i ) ; 16 System. out. p r i n t l n ( j ) ; 17 } 18 } first, second: swapped i, j: didn t H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 6 / 24

Methods: Value Passing Primitive Argument/Parameter: Key Point argument parameter: call by value (copying) change in parameter does not change argument H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 7 / 24

Methods: Value Passing Extended Argument/Parameter to be discussed in Chapter 5 H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 8 / 24

this (1/1) Methods: Value Passing 1 p u b l i c class OOPStudent { 2 p u b l i c S t r i n g name ; 3 p u b l i c S t r i n g get_name ( ) { r e t u r n name ; } 4 p u b l i c S t r i n g get_the_name ( OOPStudent WHO) 5 { r e t u r n WHO. name ; } 6 } CharlieL.get_name() returns CharlieL.name Ptt.get_name() returns Ptt.name how? a hidden parameter WHO can do 1 p u b l i c S t r i n g get_name ( ) { r e t u r n t h i s. name ; } 2 p u b l i c void set_score ( i n t score ) { t h i s. score = score ; } H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 9 / 24

Methods: Value Passing this: Key Point this: a hidden parameter in the method to keep in touch with the instance H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 10 / 24

Local Variables Local Variables (1/7) 1 p u b l i c class FibCompute { 2 p u b l i c i n t f i b ( i n t n ) { 3 i n t res ; 4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ; System. out. p r i n t l n ( s ) ; 5 i f ( n <= 1) res = 1; 6 else { res = f i b ( n 1) ; res = res + f i b ( n 2) ; } 7 r e t u r n res ; 8 } 9 } 1 p u b l i c class FibDemo { 2 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 3 FibCompute f = new FibCompute ( ) ; 4 System. out. p r i n t l n ( " res = " + f. f i b ( 5 ) ) ; 5 } 6 } a so-called recursive method local primitive n: allocated, and assigned by argument parameter H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 11 / 24

Local Variables Local Variables (2/7) 1 p u b l i c class FibCompute { 2 p u b l i c i n t f i b ( i n t n ) { 3 i n t res ; 4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ; System. out. p r i n t l n ( s ) ; 5 i f ( n <= 1) res = 1; 6 else { res = f i b ( n 1) ; res = res + f i b ( n 2) ; } 7 r e t u r n res ; 8 } 9 } 1 p u b l i c class FibDemo { 2 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 3 FibCompute f = new FibCompute ( ) ; 4 System. out. p r i n t l n ( " res = " + f. f i b ( 5 ) ) ; 5 } 6 } local extended this: allocated, and assigned by argument (f) parameter (this) H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 12 / 24

Local Variables Local Variables (3/7) 1 p u b l i c class FibCompute { 2 p u b l i c i n t f i b ( i n t n ) { 3 i n t res ; 4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ; System. out. p r i n t l n ( s ) ; 5 i f ( n <= 1) res = 1; 6 else { res = f i b ( n 1) ; res = res + f i b ( n 2) ; } 7 r e t u r n res ; 8 } 9 } 1 p u b l i c class FibDemo { 2 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 3 FibCompute f = new FibCompute ( ) ; 4 System. out. p r i n t l n ( " res = " + f. f i b ( 5 ) ) ; 5 } 6 } local primitive res: allocated, not initialized assigned by ourselves H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 13 / 24

Local Variables Local Variables (4/7) 1 p u b l i c class FibCompute { 2 p u b l i c i n t f i b ( i n t n ) { 3 i n t res ; 4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ; System. out. p r i n t l n ( s ) ; 5 i f ( n <= 1) res = 1; 6 else { res = f i b ( n 1) ; res = res + f i b ( n 2) ; } 7 r e t u r n res ; 8 } 9 } 1 p u b l i c class FibDemo { 2 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 3 FibCompute f = new FibCompute ( ) ; 4 System. out. p r i n t l n ( " res = " + f. f i b ( 5 ) ) ; 5 } 6 } local extended s: allocated, not initialized, links to a valid instance by ourselves H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 14 / 24

Local Variables Local Variables (5/7) 1 p u b l i c class FibCompute { 2 p u b l i c i n t f i b ( i n t n ) { 3 i n t res ; 4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ; System. out. p r i n t l n ( s ) ; 5 i f ( n <= 1) res = 1; 6 else { res = f i b ( n 1) ; res = res + f i b ( n 2) ; } 7 r e t u r n res ; 8 } 9 } 1 p u b l i c class FibDemo { 2 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 3 FibCompute f = new FibCompute ( ) ; 4 System. out. p r i n t l n ( " res = " + f. f i b ( 5 ) ) ; 5 } 6 } some other local variables generated by compiler: allocated, not initialized, used internally H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 15 / 24

Local Variables Local Variables (6/7) 1 p u b l i c class FibCompute { 2 p u b l i c i n t f i b ( i n t n ) { 3 i n t res ; 4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ; System. out. p r i n t l n ( s ) ; 5 i f ( n <= 1) res = 1; 6 else { res = f i b ( n 1) ; res = res + f i b ( n 2) ; } 7 r e t u r n res ; 8 } 9 } 1 p u b l i c class FibDemo { 2 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 3 FibCompute f = new FibCompute ( ) ; 4 System. out. p r i n t l n ( " res = " + f. f i b ( 5 ) ) ; 5 } 6 } when call returns: the result is copied to the caller s space-of-interest somewhere local variables: discarded H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 16 / 24

Local Variables Local Variables (7/7, courtesy of Prof. Chuen-Liang Chen) b l belong to t declaration modifier difi static i allocation (when) allocation (where) initial to 0 de-allocation scope local variable method invocation within method NO O method invocation stack memory NO method return usage range from declaration to end of block instance variable class (static) variable i t instance class l within class within class NO O YES S instance class g creation loading heap memory heap memory YES YES automatic garbage NO collection direct access range whole class OOP H.-T. Lin (NTU CSIE) whole class Chuen-Liang Chen, NTU CS&IE / 5 More About Methods OOP 03/08-09/2010 17 / 24

Local Variables Local Variables: Key Point local variables: the status of the current method frame by spec not necessarily initialized H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 18 / 24

Method Overloading Method Overloading (1/2) 1 class P r i n t e r { 2 void p r i n t I n t e g e r ( i n t a ) ; 3 void printdouble ( double a ) ; 4 void p r i n t S t r i n g ( S t r i n g a ) ; 5 } Integer and int are basically saying the same thing lazy people don t want to type so many words H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 19 / 24

Method Overloading Method Overloading (2/2) 1 class P r i n t e r { 2 void p r i n t ( i n t a ) ; 3 void p r i n t ( double a ) ; 4 i n t p r i n t ( S t r i n g a ) ; 5 void p r i n t ( i n t a, i n t b ) ; 6 } 7 class PrinterThatCompilerSees { 8 void p r i n t _ i n t ( i n t a ) ; 9 void p r i n t _ d o u b l e ( double a ) ; 10 i n t p r i n t _ S t r i n g ( S t r i n g a ) ; 11 void p r i n t _ i n t _ i n t ( i n t a, i n t b ) ; 12 } Java s (and many modern language s) solution: one method name, many possible argument types called method overloading Java signature of a method: include name and parameters (but NO return types) H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 20 / 24

Method Overloading Method Overloading (2/2) 1 class P r i n t e r { 2 s t a t i c void p r i n t ( i n t a ) ; 3 s t a t i c i n t p r i n t ( i n t a ) ; 4 s t a t i c double p r i n t ( i n t a ) ; 5 6 s t a t i c void main ( S t r i n g [ ] argv ) { 7 P r i n t e r. p r i n t ( P r i n t e r. p r i n t ( 3 ) + 5) ; 8 / / which one do you want to c a l l? 9 } 10 } determine programmer s intention from arguments: easy determine programmer s intention from return value: hard can cast, can discard, etc. Java signature of a method: include name and parameters only compiler s job: from arguments (type), determine which method (name+parameters) to call cannot have two methods with the same signature H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 21 / 24

Method Overloading Method Overloading: Key Point method overloading: a compiler s help by looking at signature rather than name in calling H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 22 / 24

Method Overloading Operator Overloading 1 class Demo{ 2 s t a t i c void main ( S t r i n g [ ] argv ) { 3 i n t a = 5; 4 System. out. p r i n t l n (3 + a ) ; / / plus ( 3, a ) 5 System. out. p r i n t l n ( " " + a ) ; / / plus ( " ", a ) 6 } 7 } Java: a limited special case for String (actually, StringBuffer); the usual cases for primitive types; but not for other extended types C++: can overload almost any operator for any class double-sided sword: powerful, but easily misused H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 23 / 24

Method Overloading Operator Overloading: Key Point operator overloading: very limited support in Java (up to now, and possibly will be) H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 24 / 24