Chapter 6 Methods: A Deeper Look

Slides:



Advertisements
Similar presentations
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Computers and Programming Introduction to Methods in Java.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Introduction to Methods
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
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.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 15,16 Java’s Methods.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
(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,
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Functions + Overloading + Scope
Chapter 7 User-Defined Methods.
Object-Oriented Programming: Classes and Objects
Methods Chapter 6.
JavaScript: Functions
JavaScript: Functions.
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
User-Defined Functions
Chapter 6 Methods: A Deeper Look
Chapter 5 - Functions Outline 5.1 Introduction
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 9 Object-Oriented Programming: Inheritance
Lecture 22 Inheritance Richard Gesick.
MSIS 655 Advanced Business Applications Programming
Group Status Project Status.
Introduction to Classes and Objects
Object Oriented Programming in java
6 Methods: A Deeper Look.
Object Oriented Programming in java
Chapter 5 Methods: A Deeper Look
Classes, Objects and Methods
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
6 Functions.
Presentation transcript:

Chapter 6 Methods: A Deeper Look T.A. Norah Alshareef © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.1  Introduction Best way to develop and maintain a large program is to construct it from small, simple pieces, or modules. Topics in this chapter static methods Declare a method with more than one parameter Method-call stack Method overloading. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.2 Program Modules in Java Java programs combine new methods and classes (that you write) with predefined methods and classes (available in the Java Application Programming Interface and in other class libraries. ) Related classes are typically grouped into packages, so that they can be imported into programs and reused. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

Modularity in Java Packages are composed of one or more classes or interfaces. Classes and interfaces are composed of one more members; either data members (fields) or function members (methods). Methods are composed of statements; either declarations (local variables) or executable statements.

6.2 Program Modules in Java (Cont.) Methods help you modularize a program by separating its tasks into self-contained units. Statements in method bodies Written only once Hidden from other methods Can be reused from several locations in a program Software reusability Use existing methods as building blocks to create new programs. Dividing a program into meaningful methods makes the program easier to debug and maintain. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.2 Program Modules in Java (Cont.) Hierarchical form of management (Fig. 6.1). A boss (the caller) asks a worker (the called method) to perform a task and report back (return) the results after completing the task. The boss method does not know how the worker method performs its designated tasks. The worker may also call other worker methods, unbeknown to the boss. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.3 static Methods, static Fields Sometimes a method performs a task that does not depend on the contents of any object. Applies to the class in which it’s declared as a whole Known as a ” static method” or a class method It’s common for classes to contain convenient static methods to perform common tasks. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.3 static Methods, static Fields and Class Math To declare a method as static, place the keyword static before the return type in the method’s declaration. Calling a static method ClassName.methodName( arguments ) if called outside the class Or only methodName( arguments ) if called within the same class Method arguments generally may be constants, variables or expressions. Class Math provides a collection of static methods that enable you to perform common mathematical calculations. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

Where I can find the class Math ?! The Java.lang package contains also the Class String and the Class System .. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.3 static Methods, static Fields and Class Math (Cont.) Math fields for common mathematical constants Math.PI (3.141592653589793) Math.E (2.718281828459045) Those fields declared in class Math with the modifiers: public, final and static public allows  you to use these fields in your own classes. Final is constant —its value cannot change after the field is initialized and this why PI and E are written in Capitals Static  Can be used before creating any objects( instances) and all objects ( instances) of the class share one copy of those fields. PI and E are declared final because their values never change. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.3 static Methods, static Fields and (Cont.) A field (that represents an attribute) is also known as an instance variable each object (instance) of the class has a separate instance (copy) of the variable in memory. Fields for which each object of a class does not have a separate instance (copy) of them are  declared static and are also known as class variables. All objects of a class containing static fields  share one copy of those fields. Together the class variables (i.e., static variables) and instance variables represent the fields of a class. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.3 static Methods, static Fields and (Cont.) Simple example to see how Static Fields can be declared and used : We've declared a class called 'Stuff' and given it one public static variable of type String. We've initialized the variable to the String value "I'm a static variable". © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.3 static Methods, static Fields and (Cont.) Simple example to see how Static Fields can be declared and used (cont..) Now we can use this class in our main program like this: Application.java: Output : I’m a static variable

6.3 static Methods, static Fields and (Cont.) Why is method main declared static? The JVM attempts to invoke the main method of the class you specify—when no objects of the class have been created. Declaring main as static allows the JVM to invoke main without creating an instance of the class. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.4 Declaring Methods with Multiple Parameters Multiple parameters are specified as a comma-separated list. There must be one argument in the method call for each parameter (sometimes called a formal parameter) in the method declaration. Each argument must be consistent with the type of the corresponding parameter. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

How you can decide if a variable should be declared as a field or a local variable?

© Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.4 Declaring Methods with Multiple Parameters (Cont.) Implementing method maximum by reusing method Math.max : Two calls to Math.max, as follows: return Math.max( x, Math.max( y, z ) ); The first specifies arguments x and Math.max( y, z ). Before any method can be called, its arguments must be evaluated to determine their values. If an argument is a method call, the method call must be performed to determine its return value. The result of the first call is passed as the second argument to the other call, which returns the larger of its two arguments. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.5 Notes on Declaring and Using Methods Three ways to call a method: Using a method name by itself  to call another method of the same class such as maximum (number 1 number 2, number3) Using a variable that contains a reference to an object, Object name. method name  to call a method of the referenced object Using the class name and a dot (.) followed by class name  to call a static method of a class © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.5 Notes on Declaring and Using Methods (Cont.) A non-static method can call any method of the same class directly and can manipulate any of the class’s fields directly. A static method can call only other static methods of the same class directly can manipulate only static fields in the same class directly. To access the class’s non-static members, a static method must use a reference to an object of the class. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.5 Notes on Declaring and Using Methods (Cont.) Three ways to return control to the statement that calls a method: When the program flow reaches the method-ending right brace } When the following statement executes return; When the method returns a result with a statement like return expression; © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

JAVA API packages examples ..

JAVA API packages examples ..

JAVA API packages examples ..

© Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

Method’s Signature Method-name (formal-parameter-types) Does not include the return-type Within any class definition, the signature must be unique The same method-name with different formal parameters types can be used to implement overloading of methods.

6.12 Method Overloading Method overloading Methods of the same name declared in the same class Must have different sets of parameters Compiler selects the appropriate method to call by examining : the number, types and order of the arguments in the call. Used to  create several methods with the same name that perform the same or similar tasks, but on different types or different numbers of arguments. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6.12 Method Overloading (cont.) Distinguishing Between Overloaded Methods: The compiler distinguishes overloaded methods by their signatures: (the methods‘names and the number, types and order of their parameters. ) Return types of overloaded methods Method calls cannot be distinguished by return type. Overloaded methods can have different return types if the methods have different parameter lists. Overloaded methods need not have the same number of parameters. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

Examples Related to this Lecture © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

Example 1: Overloading – Different Number of parameters in argument list When methods name are same but number of arguments are different.

Example 1: Overloading – Different Number of parameters in argument list

Example 2: Overloading – Difference in data type of arguments

Example 2: Overloading – Difference in data type of arguments In the above example, method disp() is overloaded based on the data type of arguments – Like example 1 here also, we have two definition of method disp(), one with char argument and another with int argument.

Example3: Overloading – Sequence of data type of arguments

Example3: Overloading – Sequence of data type of arguments Here method disp() is overloaded based on sequence of data type of arguments – Both the methods have different sequence of data type in argument list. First method is having argument list as (char, int) and second is having (int, char). Since the sequence is different, the method can be overloaded without any issues.

few Valid / invalid cases of method overloading

Questions

Question 1 – Find out if the following program will throw compilation error or not , if there is a error explain why?

Question 1 – Find out if the following program will throw compilation error or not , if there is a error explain why? Answer:

Question 2 – Find out if the following program will throw compilation error or not , if there is a error explain why?

Question 2 – Find out if the following program will throw compilation error or not , if there is a error explain why? Answer:

End © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.