Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 F local variable, global variable  The Math Class (library functions) F Java Documentation

2 Introducing Methods Method Structure A method is a collection of statements that are grouped together to perform an operation.

3 Declaring Methods public static int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; }

4 Calling Methods Example 4.1 Testing the max method This program demonstrates calling a method max to return the largest of the int values TestMax

5 Passing Parameters You pass arguments to the corresponding parameters in the method signature For e.g: void nPrintln(String message, int n) { for (int i=0; i<n; i++) System.out.println(message); } To call the above method: nPrintln(m, i); //where m is String and i is int message and n are parameters m and i are arguments

6 Pass by Value Pass by value: the parameter is allocated the required memory storage and the value of the argument is copied to the parameter. There are 2 separate copies of the same values. Example 4.2 Testing Pass by value This program demonstrates passing values to the methods. TestPassByValue

7 Overloading Methods Methods defined with the same name are overloaded. The parameter lists are different, the return type is not relevant. Example 4.3 Overloading the max Method double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; } TestMethodOverloading

8 The scope of variables Local variable Global variable Scope of variable

9 Method Abstraction You can think of the method body as a black box that contains the detailed implementation for the method.

10 The Math Class F Class constants: –PI –E (the base of natural logarithms) F Class methods: –Trigonometric Methods –Exponent Methods –Miscellaneous

11 Trigonometric Methods F sin(double a) F cos(double a) F tan(double a) F acos(double a) F asin(double a) F atan(double a)

12 Exponent Methods  exp(double a) Returns e raised to the power of a.  log(double a) Returns the natural logarithm of a.  pow(double a, double b) Returns a raised to the power of b.  sqrt(double a) Returns the square root of a.

13 Rounding Methods  double ceil(double x) R eturns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.  double floor(double x) R eturns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. F double rint(double x) Returns the double value that is closest in value to the argument and is equal to a mathematical integer.  int round(float x) Returns the closest int to the argument.  Long round(double x) Returns the closest long to the argument.

14 min, max, abs, and random  max(a, b) and min(a, b) Returns the maximum or minimum of two parameters.  abs(a) Returns the absolute value of the parameter.  random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returns a random double value in the range [0.0, 1.0).

15 Java Documentation F http://java.sun.com/javase/6/docs/api/

16 Using Math Methods Example 4.4 Computing Mean and Standard Deviation Generate 10 random numbers and compute the mean and standard deviation ComputeMeanDeviation

17 Case Studies Example 4.5 Displaying Calendars The program reads in the month and year and displays the calendar for a given month of the year. PrintCalendar

18 Design Diagram printCalendar (main) readInputprintMonth printMonthTitleprintMonthBody getMonthName getStartDay getTotalNumberOfDays getNumberOfDaysInMonth isLeapYear

19 Recursion F E.g. 4.6 Factorial F Factorial(0)=1; F Factorial(n)=n*factorial(n-1)

20 Recursion F E.g. 4.7 fibonacci numbers F Fib(0)=1; F Fib(1)=1; F Fib(n)=Fib(n-2)+Fib(n-1), n>=2

21 Recursion F E.g. 4.8 Towers of Hanoi F 3 Poles and n disks –Move n disks from pole A to pole B. –One disk can be moved at any time –Big disk can’t placed on top of small disk

22 Recursion F Towers of Hanoi F Solution –If n=1, move disk 1 from A to B –Else u Move n-1 disks from A to C, B as temporary u Move disk n from A to B u Move n-1 disks from C to B, A as temporary


Download ppt "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."

Similar presentations


Ads by Google