Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 15,16 Java’s Methods.

Similar presentations


Presentation on theme: "Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 15,16 Java’s Methods."— Presentation transcript:

1 Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 15,16 Java’s Methods

2  Program Modules in Java  Declaring Methods  Scope of Declarations  Static Methods  Class Math  Method Overloading  Passing Arrays to Methods  Emank X Mezank 2 Presented & Prepared by: Mahmoud R. Alfarra

3  Experience has shown that the best way to develop and maintain a large program is to construct it from small, simple pieces, or modules.  This technique is called divide and conquer. 3 Presented & Prepared by: Mahmoud R. Alfarra Introduction

4 4 Presented & Prepared by: Mahmoud R. Alfarra Program Modules in Java Class Methodpackage Modules in Java Modules in Java

5  Methods allow the programmer to modularize a program by separating its tasks into self-contained units.  You have declared methods in every program you have written.  These methods are sometimes referred to as programmer-declared methods.  The actual statements in the method bodies are written only once, reused from perhaps several locations in a program and are hidden from other methods. 5 Presented & Prepared by: Mahmoud R. Alfarra What is Methods?

6  One is the divide-and-conquer approach, which makes program development more manageable by constructing programs from small, simple pieces.  Second is software reusability using existing methods as building blocks to create new programs.  Third is to avoid repeating code.  Forth is, Dividing a program into meaningful methods makes the program easier to debug and maintain. 6 Presented & Prepared by: Mahmoud R. Alfarra Motivations for modularizing

7 7 Presented & Prepared by: Mahmoud R. Alfarra Declaring Methods Access modifiers returned_type Method_name (Type par1, Type par2, … ) control access to a class's variables and methods. The value which the method will returned The name of method with the rules of identifiers The arguments which the method needs to perform its task

8  Write an average method which accepts three variables and return the average.  Then write the calling code. 8 Presented & Prepared by: Mahmoud R. Alfarra Example: Average method Write the pseudo code and flowchart of the above example HW 15.1

9 9 Presented & Prepared by: Mahmoud R. Alfarra Example: Average method Calling non-static method from static method is a syntax error.

10  The scope of a declaration is the portion of the program that can refer to the declared entity by its name.  Such an entity is said to be "in scope" for that portion of the program. 10 Presented & Prepared by: Mahmoud R. Alfarra Scope of Declarations

11 1. The scope of a parameter declaration is the body of the method in which the declaration appears. 11 Presented & Prepared by: Mahmoud R. Alfarra The basic scope rules are as follows: { \\ body of the method } { \\ body of the method } int a, int b,int c public float average (int a, int b,int c) Parameter declaration Parameter declaration

12 2. The scope of a local-variable declaration is from the point at which the declaration appears to the end of that block. 12 Presented & Prepared by: Mahmoud R. Alfarra The basic scope rules are as follows: { \\ stat1 int x; } { \\ stat1 int x; } int a, int b,int c public float average (int a, int b,int c) A local declaration Can be used only after it declared, but before is a syntax error A local declaration Can be used only after it declared, but before is a syntax error

13 3. The scope of a local-variable declaration that appears in the initialization section of a for statement's header is the body of the for statement and the other expressions in the header. 13 Presented & Prepared by: Mahmoud R. Alfarra The basic scope rules are as follows: { \\ stat1 } { \\ stat1 } for(int x=0; x<10; x++) A local declaration Can be used only in the block of for loop A local declaration Can be used only in the block of for loop

14 4. The scope of a method or field of a class is the entire body of the class. This enables non-static methods of a class to use the class's fields and other methods. 14 Presented & Prepared by: Mahmoud R. Alfarra The basic scope rules are as follows: { \\ stat1 int x; } { \\ stat1 int x; } public class average () A local declaration Of class Can be used entire of the class A local declaration Of class Can be used entire of the class

15  Most methods execute in response to method calls on specific objects, this is not always the case.  Sometimes a method performs a task that does not depend on the contents of any object.  Such a method applies to the class in which it is declared as a whole and is known as a static method or a class method. 15 Presented & Prepared by: Mahmoud R. Alfarra Static Methods

16  To declare a method as static, place the keyword static before the return type in the method's declaration.  You can call any static method by specifying the name of the class in which the method is declared, followed by a dot (.) and the method name, as in 16 Presented & Prepared by: Mahmoud R. Alfarra Static Methods declaration int a, int b public static float average (int a, int b)

17  You can call any static method by specifying the name of the class in which the method is declared, followed by a dot (.) and the method name, as in 17 Presented & Prepared by: Mahmoud R. Alfarra Calling Static Methods ClassName. methodName( arguments )

18  Class Math provides a collection of methods that enable you to perform common mathematical calculations. 18 Presented & Prepared by: Mahmoud R. Alfarra Math class (Predefined)

19 19 Presented & Prepared by: Mahmoud R. Alfarra Math class methods

20  Using Math class, Write program to compare the max, min, Power and square of numbers. 20 Presented & Prepared by: Mahmoud R. Alfarra Example: Math Class Write the pseudo code and flowchart of the above example HW 15.1

21 21 Presented & Prepared by: Mahmoud R. Alfarra Example: Math Class

22  Another important feature of method calls is argument promotion converting an argument's value to the type that the method expects to receive in its corresponding parameter.  For example: 22 Presented & Prepared by: Mahmoud R. Alfarra Argument Promotion and Casting System.out.println( Math.sqrt (4) )  Correctly evaluates Math.sqrt( 4 ( and prints the value 2.0  Just conversions can be performed without losing data.

23  The following figure lists the primitive types and the types to which each can be promoted. 23 Presented & Prepared by: Mahmoud R. Alfarra Argument Promotion and Casting

24  Methods of the same name can be declared in the same class, as long as they have different sets of parameters this is called method overloading.  Difference of parameters is determined by the number, types and order of the parameters which is called( ). 24 Presented & Prepared by: Mahmoud R. Alfarra Method Overloading

25  Method overloading is commonly 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. 25 Presented & Prepared by: Mahmoud R. Alfarra Method Overloading

26  For example, Math methods abs, min and max are overloaded with four versions each: 1.One with two double parameters. 2.One with two float parameters. 3.One with two int parameters. 4.One with two long parameters. 26 Presented & Prepared by: Mahmoud R. Alfarra Method Overloading

27 27 Presented & Prepared by: Mahmoud R. Alfarra Declaring Overloaded Methods

28  To pass an array argument to a method, specify the name of the array without any brackets. For example, 28 Presented & Prepared by: Mahmoud R. Alfarra Passing Arrays to Methods if array hourlyTemperatures is declared as double hourlyTemperatures[] = new double [24] ; then the method call modifyArray( hourlyTemperatures ( ;

29  For example, Math methods abs, min and max are overloaded with four versions each: 1.One with two double parameters. 2.One with two float parameters. 3.One with two int parameters. 4.One with two long parameters. 29 Presented & Prepared by: Mahmoud R. Alfarra Method Overloading

30  For example, Math methods abs, min and max are overloaded with four versions each: 1.One with two double parameters. 2.One with two float parameters. 3.One with two int parameters. 4.One with two long parameters. 30 Presented & Prepared by: Mahmoud R. Alfarra Method Overloading

31 قال النبي صلى الله عليه وسلم : ( مَا بُعِثَ نَبِيٌّ إِلا أَنْذَرَ أُمَّتَهُ الأَعْوَرَ الْكَذَّابَ أَلا إِنَّهُ أَعْوَرُ وَإِنَّ رَبَّكُمْ لَيْسَ بِأَعْوَرَ وَإِنَّ بَيْنَ عَيْنَيْهِ مَكْتُوبٌ كَافِرٌ قال النبي صلى الله عليه وسلم : ( مَا بُعِثَ نَبِيٌّ إِلا أَنْذَرَ أُمَّتَهُ الأَعْوَرَ الْكَذَّابَ أَلا إِنَّهُ أَعْوَرُ وَإِنَّ رَبَّكُمْ لَيْسَ بِأَعْوَرَ وَإِنَّ بَيْنَ عَيْنَيْهِ مَكْتُوبٌ كَافِرٌ يَقْرَؤُهُ كُلُّ مُؤْمِنٍ كَاتِبٍ وَغَيْرِ كَاتِبٍ ) رواه مسلم 31 Presented & Prepared by: Mahmoud R. Alfarra

32 Practices 32 Presented & Prepared by: Mahmoud R. Alfarra


Download ppt "Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 15,16 Java’s Methods."

Similar presentations


Ads by Google