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

Slides:



Advertisements
Similar presentations
Procedural programming in Java
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
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.
Chapter 6: User-Defined Functions I
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
Chapter 6: User-Defined Functions I
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
Introduction to Methods
Computer Programming 2 Lecture 2: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER.
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
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
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.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Part II © 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.
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Introduction To Programming Information Technology , 1’st Semester
Procedural programming in Java Methods, parameters and return values.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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 6 Methods Chapter 6 - Methods.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 18 Recursion & Pointers in Java.
(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.
Chapter 6: User-Defined Functions I
Presented By: Mahmoud Rafeek Alfarra
Methods Chapter 6.
Introduction to Methods
User-Defined Functions
Chapter 6 Methods: A Deeper Look
Introduction To Programming Information Technology , 1’st Semester
Chapter 6 Methods: A Deeper Look
MSIS 655 Advanced Business Applications Programming
Introduction To Programming Information Technology , 1’st Semester
Chapter 6: User-Defined Functions I
6 Methods: A Deeper Look.
Methods.
Presented By: Mahmoud Rafeek Alfarra
Introduction To Programming Information Technology , 1’st Semester
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.
Presentation transcript:

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

 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

 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 Presented & Prepared by: Mahmoud R. Alfarra Program Modules in Java Class Methodpackage Modules in Java Modules in Java

 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?

 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 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

 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 Presented & Prepared by: Mahmoud R. Alfarra Example: Average method Calling non-static method from static method is a syntax error.

 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

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

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

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

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

 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

 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)

 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 )

 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 Presented & Prepared by: Mahmoud R. Alfarra Math class methods

 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 Presented & Prepared by: Mahmoud R. Alfarra Example: Math Class

 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.

 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

 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

 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

 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 Presented & Prepared by: Mahmoud R. Alfarra Declaring Overloaded Methods

 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 ( ;

 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

 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 Presented & Prepared by: Mahmoud R. Alfarra

Practices 32 Presented & Prepared by: Mahmoud R. Alfarra