الكلية الجامعية للعلوم التطبيقية

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Introduction to Java Programming, 4E Y. Daniel Liang.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Introduction to Java Programming Lecture 12 Method Benefits, Declaring, and Calling Methods.
CMT Programming Software Applications Dr. Xiaohong Gao Repetitions and Methods Week 4.
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
Computer Programming Lab(5).
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
Programming Fundamentals I (COSC-1336), Lecture 5 (prepared after Chapter 5 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 5.
Chapter 4 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion Lecture 6 Dr. Musab.
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.
TOPIC 6 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Chapter 4: Methods Method Structure Method Structure Declaring Methods Declaring Methods Calling Methods Calling Methods Passing Parameters Passing Parameters.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Chapter 6 Arrays 1 Fall 2012 CS2302: Programming Principles.
C# Programming Methods.
Methods.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Chapter 5 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
CSE 220 – C Programming Pointers.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Chapter 5 Function Basics
Functions and an Introduction to Recursion
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 5 Functions.
Module 4 Functions – function definition and function prototype.
Chapter 6 Methods 1.
Chapter 6 Methods.
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 5 Function Basics
Chapter 6 Arrays Fall 2012 CS2302: Programming Principles.
Group Status Project Status.
Chapter 5 Function Basics
Chapter 6 Methods.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
CS2011 Introduction to Programming I Methods (II)
Chapter 6 Methods.
Chapter 5 Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Week 4 Lecture-2 Chapter 6 (Methods).
Functions and an Introduction to Recursion
Methods and Data Passing
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Functions Imran Rashid CTO at ManiWeber Technologies.
Recursion Method calling itself (circular definition)
Main() { int fact; fact = Factorial(4); } main fact.
Lecture 03 & 04 Method and Arrays Jaeki Song.
Unit-1 Introduction to Java
Chapter 4 Methods Introducing Methods Declaring Methods
PROGRAMMING ASSIGNMENT I
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

الكلية الجامعية للعلوم التطبيقية برمجة حديثة (1) COMP2301 د. سناء وفا الصايغ الفصل الثاني 2010-2011 Methods الدوال

مقدمة Methods Overloading Methods Math Methods Recursion Methods

لإيجاد الرقم الأكبر بين رقمين Methods مثال 1 لإيجاد الرقم الأكبر بين رقمين public classTestMax { // Main method public static void main(String[]args) Int i = 5; int j = 2; int k = max(i, j); System.out.println("The maximum between " + i + " and " + j + " is " + k); }

// A method for finding a max between two numbers static int max(int num1,int num2) { if (num1 > num2) return num1; else return num2; }

مثال 2 لتصميم دالة لطباعة جملة عدد من المرات مثال 2 لتصميم دالة لطباعة جملة عدد من المرات nPrintln("List my name is seven times.", 7); void nPrintln(String message,int n) { for (int i=0; i<n; i++) System.out.println(message); }

مثال 3 لتبديل رقمين //TestPassByValue.java: Demonstrate passing values to methods public classTestPassByValue { public static void main(String[]args) // Declare and initialize variables int num1 = 1; int num2 = 2; System.out.println("Before invoking the swap method, num1 is " + num1 + " and num2 is " + num2); // Invoke the swap method to attempt to swap two variables swap(num1, num2); System.out.println("After invoking the swap method, num1 is " + }

//TestPassByValue.java: Demonstrate passing values to methods // The method for swapping two variables static void swap(int n1,int n2) { System.out.println(" Inside the swap method"); System.out.println(" Before swapping n1 is " + n1+ " n2 is " + n2); // Swapping n1 with n2 int temp = n1; n1 = n2; n2 = temp; System.out.println(" After swapping n1 is " + n1+ " n2 is " + n2); }

Overloading Methods public classTestMethodOverloading { public static void main(String[]args) { // Invoke the max method within parameters System.out.println("The maximum between 3 and 4 is “ + max(3, 4)); // Invoke the max method with the double parameters System.out.println("The maximum between 3.0 and 5.4 is " + max(3.0, 5.4)); // Invoke the max method with three double parameters System.out.println("The maximum between 3.0, 5.4, and 10.14 is " + max(3.0, 5.4, 10.14)); }

Overloading Methods // Find the max between two int values Static int max(int num1,int num2) { if (num1 > num2) return num1; else return num2; } // Find the max between two double values static double max(double num1, double num2) if (num1 > num2) return num1; else return num2;

Overloading Methods // Find the max among three double values static double max(double num1, double num2, double num3) { return max(max(num1, num2), num3); }

Math Methods //ComputeMeanDeviation.java: Demonstrate using the math methods public classComputeMeanDeviation { // Main method public static void main(String[]args) { Int number = 0; // Store a random number double sum = 0; // Store the sum of the numbers Double squareSum= 0; // Store the sum of the squares // Create 10 numbers, find its sum, and its square sum for (int i=1; i<=10; i++) // Generate a new random number number = (int)Math.round(Math.random()*1000); System.out.println(number);

Math Methods // Add the number to sum sum += number; // Add the square of the number tosquareSum squareSum+= Math.pow(number, 2); // Same as number*number; } // Find mean double mean = sum/10; // Find standard deviation double deviation = Math.sqrt((squareSum-mean)/(10 -1)); // Display result System.out.println("The mean is " + mean); System.out.println("The standard deviation is " + deviation);

Recursion Methods //ComputeFactorial.java: Compute factorial of an integer public classComputeFactorial { public static void main(String[]args) // Prompt the user to enter an integer System.out.println("Please enter a nonnegative integer"); intn =MyInput.readInt(); System.out.println("Factorial of " + n + " is " + factorial(n)); }

Recursion Methods // Recursive method for computing factorial of n static long factorial(int n) { if (n == 0) // Stopping condition return 1; else return n*factorial(n-1); // Call factorial recursively }