Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.

Slides:



Advertisements
Similar presentations
Chapter 6: User-Defined Functions I
Advertisements

Introduction to C Programming
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 5 Functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
Chapter 6: User-Defined Functions I
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Chapter 6: User-Defined Functions I
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Chapter 7: User-Defined Methods
Chapter 5: METHODS USER-DEFINED METHODS. user-defined  We learned that a Java application program is a collection of classes, and that a class is a collection.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 7 User-Defined Methods.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 8 Arrays and Strings
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Methods. 2 A sequence of statements can be packaged together as a unit and re-used. A method is a named unit of re-usable code. modifier returnType methodName(
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Procedural programming in Java Methods, parameters and return values.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
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.
CiS 260: App Dev I. 2 Introduction to Methods n A method (or ________) is a segment of code that performs a specific task. n Advantages of modular program.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Chapter 7: User-Defined Methods J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Third Edition Third.
Functions + Overloading + Scope
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Java Programming: Guided Learning with Early Objects
Methods Chapter 6.
Object Oriented Systems Lecture 03 Method
User-Defined Functions
Chapter 6 Methods: A Deeper Look
Group Status Project Status.
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
Corresponds with Chapter 5
Presentation transcript:

Chapter 7 User-Defined Methods

Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and discover how to use them in a program  Learn about user-defined methods  Examine value-returning methods, including actual and formal parameters

Chapter Objectives  Explore how to construct and use a value- returning, user-defined method in a program  Learn how to construct and use user-defined void methods in a program  Explore variables as parameters  Learn about the scope of an identifier  Become aware of method overloading

Advantages of using methods  Methods are the building blocks or modules of classes and of systems built from many classes  Predefined methods are supplied within Java  User defined methods originate from developers  Allows the developer to write, debug, and integrate one module at a time in a larger system  Allows many developers to simultaneously develop different components of a larger system  Improves readability and ease of maintenance of the system

Methods  The concept of a method is similar to the mathematical concept of a function  A function has  A result or a value  A series of arguments  Functional form  A method either  Evaluates to a result (value) of a particular type  Modifies or manipulates its arguments (e.g. prints)

Syntax: Value-Returning Method modifier(s) returnType methodName(formal parameter list) { statements } modifier(s) void methodName(formal parameter list) { statements } Syntax: Void Method

Predefined Classes and Methods  Methods already written and provided by Java  Organized as a collection of classes (class libraries)  To use: import package or class  Classname.methodname(argument list)

class Math (Package: java.lang)

class Character (Package: java.lang)

Value-Returning Method modifier(s) returnType methodName(formal parameter list) { statements; return expr; }  The definition of the method has two components  The body of the method, the statements between {}, the code required to implement the method and return the value  The heading of the method, the line defining modifiers, returnType, methodName and formal parameters or arguments

User-Defined Methods  Value-returning methods  Used in expressions  Calculate and return a value  Can be used in the expression in an assignment statement to save value for later use  Modifiers: public, private, protected, static, abstract, final  returnType:  type of value that the method calculates and returns (using return statement)  Type of expression formed by call of method  methodName: Java identifier; name of method

Syntax Syntax: Formal Parameter List The syntax of the formal parameter list is: dataType identifier, dataType identifier,... Method Call The syntax to call a value-returning method is: methodName(actual parameter list) Syntax: Actual Parameter List The syntax of the actual parameter list is: expression or variable, expression or variable,...

Equivalent Method Definitions public static double larger(double x, double y) { double max; if(x >= y) max = x; else max = y; return max; }

Equivalent Method Definitions public static double larger(double x, double y) { if(x >= y) if(x >= y) return x; return x; else else return y; return y;}

Programming Example: Palindrome Number  Palindrome: integer or string that reads the same forwards and backwards  Input: integer or string  Output: Boolean message indicating whether integer string is a palindrome

Solution: isPalindrome Method

Flow of Execution  Execution always begins with the first statement in the method main  User-defined methods execute only when called  Call to method transfers control from caller to called method  In method call statement, specify only actual parameters, not data type or method type  Control goes back to caller when method exits

Programming Example: Largest Number  Input: Set of 10 numbers  Output: Largest of 10 numbers  Solution:  Get numbers one at a time  Method largest number: returns the larger of 2 numbers  For loop: calls method largest number on each number received and compares to current largest number

Void Methods  Similar in structure to value-returning methods  No method type  Call to method is always stand-alone statement  Can use return statement to exit method early  Return statement does not return the value of an expression

Void Methods with no parameters: Syntax  Method Definition modifier(s) void methodName( formal parameter list) { statements }  Method Call (Within the Class) methodName();

Void Methods with Parameters: Syntax Method Definition Definition of a void method with parameters modifier(s) void methodName(formal parameter list) { statements statements} Formal Parameter List The formal parameter list has the following syntax: dataType variable, dataType variable,...

Void Methods with Parameters: Syntax Method Call The method call has the following syntax: methodName(actual parameter list); Actual Parameter List The actual parameter list has the following syntax: expression or variable, expression or variable,...

Primitive Data Type Variables as Parameters  A formal parameter receives a copy of its corresponding actual parameter  If a formal parameter is a variable of a primitive data type  Value of actual parameter is directly stored  Cannot pass information outside the method  Provides only a one-way link between actual parameters and formal parameters

Reference Variables as Parameters  If a formal parameter is a reference variable  Copies value of corresponding actual parameter  Value of actual parameter is address of object where actual data is stored  Both formal and actual parameter refer to same object

Uses of Reference Variables as Parameters  Can return more than one value from a method  Can change the value of the actual object  When passing address, would save memory space and time, relative to copying large amount of data

Reference Variables as Parameters: type String

Scope of an Identifier Within a Class  Scope (of an identifier): refers to those parts of a program where the identifier is accessible  Local variables: variables declared within a method (or block)  Within a class  Any method can call any other method  Exception: static method cannot call a nonstatic method

Scope Rules  Identifier declared within a block is accessible  Only within the block from the point at which it is declared until the end of the block  By those blocks nested within that block if the nested block does not have an identifier with the same name as the identifier in the outside block *Outside block: block that encloses nested block *Outside block: block that encloses nested block

Scope Rules: Demonstrated public class ScopeRules { static final double rate = 10.50; static final double rate = 10.50; static int z; static int z; static double t; static double t; public static void main(String[] args) public static void main(String[] args) { int num; int num; double x, z; double x, z; char ch; char ch; //... //... } public static void one(int x, char y) public static void one(int x, char y) { //... //... } public static int w; public static int w; public static void two(int one, int z) public static void two(int one, int z) { char ch; char ch; int a; int a; //block three //block three { int x = 12; int x = 12; //... //... }//end block three }//end block three //... //... }

Scope Rules: Demonstrated

Method Overloading: An Introduction  Method overloading: more than one method can have the same name  Overloading Rules  Every method must have a different number of formal parameters OR  If the number of formal parameters is the same, then the data type of the formal parameter (in the order listed), must differ in at least one position  Types of parameters determine which method executes

Programming Example: Data Comparison  Input: Data from 2 different files  Data format: Course Number followed by scores  Output: Course Number, Group Number, Course Average  Solution:  Read from more than one file, write output to file  Generate bar graphs  User-defined methods and re-use (calculateAverage and printResult)  Parameter passing

Chapter Summary  Predefined methods  User-defined methods  Value-returning methods  Void methods  Formal parameters  Actual parameters  Flow of Execution

Chapter Summary  Primitive data type variables as parameters  One-way link between actual parameters and formal parameters (limitations caused)  Reference variables as parameters  Can pass one or more variables from a method  Can change value of actual parameter  Scope of an identifier within a class  Method overloading