CS2011 Introduction to Programming I Methods (II)

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Procedures and Control Flow CS351 – Programming Paradigms.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Chapter 5 Functions.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Introduction to Java Programming, 4E Y. Daniel Liang.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Introduction to Java Programming Lecture 12 Method Benefits, Declaring, and Calling Methods.
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
CIS 270—Application Development II Chapter 18-Generics.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
TOPIC 6 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Functions Functions, locals, parameters, and separate compilation.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Classes - Intermediate
Methods.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Reference: COS240 Syllabus
AKA the birth, life, and death of variables.
Chapter 5 Function Basics
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 5 Functions DDC 2133 Programming II.
Chapter 5 Functions.
Chapter 6 Methods 1.
Programming Fundamentals Lecture #7 Functions
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Methods.
Templates.
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 5 Function Basics
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Group Status Project Status.
CS2011 Introduction to Programming I Objects and Classes
CS2011 Introduction to Programming I Arrays (II)
CS2011 Introduction to Programming I Loop Statements (II)
CS2011 Introduction to Programming I Arrays (I)
Chapter 6 Methods.
AKA the birth, life, and death of variables.
Chapter 5 Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Week 4 Lecture-2 Chapter 6 (Methods).
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
A Methodical Approach to Methods
Unit-1 Introduction to Java
Methods (a.k.a functions)
Corresponds with Chapter 5
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

CS2011 Introduction to Programming I Methods (II) Chengyu Sun California State University, Los Angeles

A Few More Things About Methods Variable scope Pass by value Method overloading Top-Down Design

Scope of Variables The part of the program where a variable can be used public static void method1() { … … for (int i = 1; i < 10; i++) { int j=0; } scope of i scope of j

Local Variable … A variable defined inside a method is called a local variable What are non-local variables?? The scope of a local variable starts from its declaration to the end of the block (i.e. {}) containing the variable Method parameters are actually local variables

… Local Variable Two local variables with the same name cannot have overlapping scopes for (int i = 1; i < 10; i++) { … … for (int i = 1; i < 10; i++) } for (int i = 1; i < 10; i++) { … … }

Example: Swap public static void swap(int a, int b) { int temp = a; a = b; b = temp; } int a = 10, b = 20; swap( a, b ); System.out.println (a + ", " + b ); // ??

Pass By Value When a method is invoked with a argument, the value of the argument is passed to the parameter public static void inc( int n ) { ++n; } public static void main( String args[] ) { int n = 10; inc(n); System.out.println(n); (c) (a) (b) (d)

Activation Record and Call Stack An activation record contains the parameters and local variables of an invoked method A call stack keeps the activation records of the active methods (i.e. methods that are not finished)

Method Invocation Example inc() inc() n = 10 n = 11 main() main() main() main() args n = 10 args n = 10 args n = 10 args n = 10 (a) (b) (c) (d)

Same Method for Different Types public static int max( int n1, int n2 ) { return n1 > n2 ? n1 : n2; } What if we want to create a similar method for double values? One possible solution: int maxInt( int n1, int n2 ) double maxDouble( double n1, double n2)

Method Overloading Method overloading allows us to create methods with the same name but different parameters public static int max( int n1, int n2 ) { return n1 > n2 ? n1 : n2; } public static double max( double n1, double n2 ) { return n1 > n2 ? n1 : n2; }

How Does Java Compiler Choose Which Method to Use? Suppose we have three methods: int max( int n1, int n2 ) double max( double n1, double n2 ) double max( double n1, double n2, double n3 ) max( 10, 20 ); // ?? max( 1.2, 2.3 ); // ?? max( 1, 2, 3 ); // ?? max( 1, 2.3 ); // ??

Ambiguous Invocation Suppose we have two methods: double max( int n1, double n2 ) double max( double n1, int n2 ) max( 10, 20 ); // ?? max( 10.5, 20.5); // ??

Example: Print Calendar

Top-Down Design Bottom-Up Implementation

Readings Chapter 6 of the textbook (there will be a quiz next week)