Methods. Instructor : Behnam Hajian bhajian@scs.carleton.ca. Introduction to Systems Programming - COMP 1005 , 1405 . Objectives. To declare methods, invoke methods, and pass arguments to a method To use method overloading and know ambiguous overloading
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
F I N
Plus de contenu associé
Transcription de la présentation
Methods Instructor : BehnamHajian bhajian@scs.carleton.ca Introduction to Systems Programming - COMP 1005, 1405
Objectives • To declare methods, invoke methods, and pass arguments to a method • To use method overloading and know ambiguous overloading • To determine the scope of local variables • To learn the concept of method abstraction • To know how to use the methods in the Mathclass • To design and implement methods using stepwise refinement • To group classes into packages
Developing Large Programs • Difficult to manage large code • Teams of people may be involved • Easy to forget what the project is all about • People may leave the company Result: Code should be • well organized • well commented and documented • Organized in a modular form (small coherent parts)
Top Down Design • Begin with the main problem/idea • Divide it into smaller problem/ideas • Further divide each small problem/idea into smaller more manageable parts. The process is termed: Divide and Conquer When to stop? • When the sub-problem/idea is understood and manageable • Coding rule of thumb – a module is 5-30 lines (one page)
Functions : Organized Development • Allow organized development • Develop and test small pieces of code • Functions assignable to team members • Permit code reusability
What is a Function ? • A function is a module (piece of code) aimed at accomplishing a single task • Note: the scope of the task or its objective may large (recall breakdown of a problem/ideas)
Functions as Modules of Code Functions are the basic modulesin structural programming languages. • Everyprogram has a main function • The main function uses other functions • Each function may use other functions • Functions help organize and develop code
Method Abstraction You can think of the method body as a black box that contains the detailed implementation for the method.
Benefits of Methods • Write a method once and reuse it anywhere. • Information hiding. Hide the implementation from the user. • Reduce complexity.
Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Introducing Methods, cont. • Method signature is the combination of the method name and the parameter list. • The variables defined in the method header are known as formal parameters. • When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.
Introducing Methods, cont. • A method may return a value. The returnValueType is the data type of the value the method returns. If the method does not return a value, the returnValueType is the keyword void. For example, the returnValueType in the main method is void.
Calling Methods Listing 5.1 Testing the max method This program demonstrates calling a method max to return the largest of the int values
CAUTION A return statement is required for a nonvoid method. The following method is logically correct, but it has a compilation error, because the Java compiler thinks it possible that this method does not return any value. intsign(int n) { if (n > 0) return 1; else if (n == 0) return 0; else if (n < 0) return –1; } To fix this problem, delete if (n<0) in the code.
Scope of Local Variables A local variable: a variable defined inside a method. Scope: the part of the program where the variable can be referenced. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used.
Scope of Local Variables, cont. You can declare a local variable with the same name multiple times in different non-nesting blocks in a method, but you cannot declare a local variable twice in nested blocks.
Scope of Local Variables, cont. A variable declared in the initial action part of a for loop header has its scope in the entire loop. But a variable declared inside a for loop body has its scope limited in the loop body from its declaration and to the end of the block that contains the variable.
Scope of Local Variables, cont. // Fine with no errors void correctMethod() { int x = 1; int y = 1; // i is declared for (inti = 1; i < 10; i++) { x += i; } // i is declared again for (inti = 1; i < 10; i++) { y += i; } }
Ambiguous Invocation Sometimes there may be two or more possible matches for an invocation of a method, but the compiler cannot determine the most specific match. This is referred to as ambiguous invocation. Ambiguous invocation is a compilation error.
Ambiguous Invocation public class AmbiguousOverloading { public static void main(String[] args) { System.out.println(max(1, 2)); } public static double max(int num1, double num2) { if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { if (num1 > num2) return num1; else return num2; } }
Connexion au compte
Connectez-vous pour publier votre contenu sur SlideServe.