More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 6 Functions.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Object-Oriented Programming (OOP) and C++
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods Dr. Musab Zghoul.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 5 Functions Lecturer: Mrs Rohani Hassan.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
User-Written Functions
Reference: COS240 Syllabus
Java Programming: Guided Learning with Early Objects
Chapter 5 Methods.
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 6 Functions.
About the Presentations
Chapter 5 Functions DDC 2133 Programming II.
Chapter 5 Function Basics
Chapter 6 Methods 1.
Methods.
Chapter 5 Methods.
Chapter 6 Methods: A Deeper Look
Corresponds with Chapter 7
Chapter 5 Function Basics
Chapter 6 Methods.
Week 4 Lecture-2 Chapter 6 (Methods).
Object Oriented Programming in java
Unit-1 Introduction to Java
Corresponds with Chapter 5
Introduction to Methods and Interfaces
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6

Pass by Value Parameter passing can be of two types: 1.Pass by Value A COPY of the actual parameter’s contents is sent to the method and inserted into the formal parameter. Changing the contents of the formal parameter does not affect the actual parameter. 2.Pass by Reference The formal parameter IS THE SAME AS the actual parameter. Changing the contents of the formal parameter also changes the actual parameter. Java is strictly a pass-by-value language. The contents of variable passed to a method will not change based on actions done within the method. HOWEVER, some variables are themselves REFERENCES. The objects that they reference can be changed if a called method accesses these objects through the passed in parameters. (More about this later).

NOTE: data of formal parameters is being changed…but this does not affect actual parameters. Call to swap method, passing two variables Listing 5.3 p 136

Anatomy of Method Declaration and Call return type identifier formal parameters method body No return statement needed for void method specify identifier Pass actual parameters (arguments) No use of return value because return type is void method declaration method call modifiers

Processing Sequence of a Method Call method call method declaration 2) Pass by value: n1 is a copy of num1, n2 is a copy of num2. 3) Method body executes. 4) After method terminates, control returns to right after the calling statement. 1) invoke the method

Review of Variables and Memory Usage Two categories of variables Two categories of variables Value – contains actual data (integer, float, double, boolean, or other primitive data type) Value – contains actual data (integer, float, double, boolean, or other primitive data type) Reference – contains a reference (pointer) to an object or array that is located on the heap. Reference – contains a reference (pointer) to an object or array that is located on the heap. Two different locations of memory (more to come later) Two different locations of memory (more to come later) Frame Stack – contains local variables and parameters of methods Frame Stack – contains local variables and parameters of methods Heap – contains objects and arrays Heap – contains objects and arrays The new keyword always creates an object or array and places it in the heap. The new keyword always creates an object or array and places it in the heap.

Frame Stack args num1 1 num2 main’s frame 2 In main(), before calling swap()

Frame Stack args num1 1 num2 main’s frame 2 swap’s frame n1 1 n2 2 temp In swap(), before assigning n1 to temp

Frame Stack args num1 1 num2 main’s frame 2 swap’s frame n1 1 n2 2 temp 1 In swap(), before assigning n2 to n1

Frame Stack args num1 1 num2 main’s frame 2 swap’s frame n1 2 n2 2 temp 1 In swap(), before assigning temp to n2

Frame Stack args num1 1 num2 main’s frame 2 swap’s frame n1 2 n2 1 temp 1 In swap(), after assigning temp to n2 Note: the formal parameter values changed, but not the actual parameters

Frame Stack args num1 1 num2 main’s frame 2 Back in main(), after swap() returns

Passing Reference Variables as Parameters As the previous example shows, Java is strictly a pass-by-value language. The contents of variable passed to a method will not change based on actions done within the method. HOWEVER, some variables are themselves REFERENCES. The objects that they reference can be changed if a called method accesses these objects through the passed in parameters. Any variable that is not a primitive data type (int, boolean, float, double, etc.) is a REFERENCE variable. That is, the variable is a pointer, pointing to the object. This could be: 1)An array 2)An instance of a class The next example shows the effects of passing an array as a parameter to a method.

Example 6.3 p180 – TestPassArray Class

Heap args Frame Stack main’s frame a 1)Declaring the reference variable ) Creating the array on the heap 3) Assigning the array’s memory location to the reference variable

Heap args a Frame Stack main’s frame swap’s frame n1 1 n2 2 temp In this case, individual array elements are passed. The array elements are primitive data types, so copies of the data are passed.

Heap args a Frame Stack main’s frame swap’s frame n1 1 n2 2 temp 1 In this case, individual array elements are passed. The array elements are primitive data types, so copies of the data are passed.

Heap args a Frame Stack main’s frame swap’s frame n1 2 n2 2 temp 1 In this case, individual array elements are passed. The array elements are primitive data types, so copies of the data are passed.

Heap args a Frame Stack main’s frame swap’s frame n1 2 n2 1 temp 1 In this case, individual array elements are passed. The array elements are primitive data types, so copies of the data are passed. Original data does not change!!

In this case, the array variable itself is passed. The array variable is a reference type of variable, so a copy of the reference is passed. Heap args a Frame Stack main’s frame Swap First Two In Array’s frame arraytemp

Heap args a Frame Stack main’s frame Swap First Two In Array’s frame arraytemp 1

Note: data in the array itself is changed Heap args a Frame Stack main’s frame Swap First Two In Array’s frame arraytemp 1

Heap args a Frame Stack main’s frame Swap First Two In Array’s frame arraytemp 1 Original data did change!!! Note: data in the array itself is changed

Searching for a Value in an Array It is very common to need to find a particular value in an array It is very common to need to find a particular value in an array The following example shows a simple approach for doing this…it is called a Linear Search. The following example shows a simple approach for doing this…it is called a Linear Search.

Linear Search of an Array This class includes a method that receives two parameters: 1)An integer of the value you want to search for. 2)An integer array that you want to search in. The method will search the array, looking for the desired value. If it finds the value, the method returns the index of the element containing the value. If the value is not found, the method returns -1.

Method Abstraction Separating method use from its implementation The signature is the only thing the caller of a method needs to know. The details of the implementation should not affect the caller. Remember: Signature = modifier + return type + name + formal parameter list

To Illustrate Method Abstraction… Consider the Java Online Documentation: Consider the Java Online Documentation:

Consider the description of the Math.round() method…all you see is the signature. This is an abstraction. You don’t need to know how it implements the code; these details are hidden from the user of the method.

The documentation will provide information about what is returned, the formal parameters expected, and any modifiers. This is all the caller of a method needs.

Top-Down Design and Stepwise Refinement Structure Chart: top-down breakdown of tasks (methods) with data couples (parameters and return values) Structure Chart: top-down breakdown of tasks (methods) with data couples (parameters and return values) Some Elements of a Structure chart: Some Elements of a Structure chart: Boxes (for methods) Boxes (for methods) Connecting arrows (for invocations) Connecting arrows (for invocations) See Liang pp

33 Stepwise Refinement (Optional) The concept of method abstraction can be applied to the process of developing programs. When writing a large program, you can use the “divide and conquer” strategy, also known as stepwise refinement, to decompose it into subproblems. The subproblems can be further decomposed into smaller, more manageable problems.

34 PrintCalendar Case Study Let us use the PrintCalendar example to demonstrate the stepwise refinement approach.

35 Design Diagram

36 Design Diagram

37 Design Diagram

38 Design Diagram

39 Design Diagram

41 Implementation: Top-Down Top-down approach is to implement one method in the structure chart at a time from the top to the bottom. Stubs can be used for the methods waiting to be implemented. A stub is a simple but incomplete version of a method. The use of stubs enables you to test invoking the method from a caller. Implement the main method first and then use a stub for the printMonth method. For example, let printMonth display the year and the month in the stub. Thus, your program may begin like this:

44 Implementation: Bottom-Up Bottom-up approach is to implement one method in the structure chart at a time from the bottom to the top. For each method implemented, write a test program to test it. Both top-down and bottom-up methods are fine. Both approaches implement the methods incrementally and help to isolate programming errors and makes debugging easy. Sometimes, they can be used together.