Upcoming SlideShare
×

# 04slide

560 views

Published on

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

Views
Total views
560
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
9
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 04slide

1. 1. Chapter 5 Methods <ul><li>Introducing Methods </li></ul><ul><li>Declaring Methods </li></ul><ul><li>Calling Methods </li></ul><ul><li>Passing Parameters </li></ul><ul><li>Pass by Value </li></ul><ul><li>Overloading Methods </li></ul><ul><li>Method Abstraction </li></ul><ul><li>local variable, global variable </li></ul><ul><li>The Math Class (library functions) </li></ul><ul><li>Java Documentation </li></ul>
2. 2. Introducing Methods Method Structure A method is a collection of statements that are grouped together to perform an operation.
3. 3. Declaring Methods <ul><li>public static int max(int num1, int num2) </li></ul><ul><li>{ </li></ul><ul><li>if (num1 > num2) </li></ul><ul><li>return num1; </li></ul><ul><li>else </li></ul><ul><li>return num2; </li></ul><ul><li>} </li></ul>
4. 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. 5. Passing Parameters <ul><li>You pass arguments to the corresponding parameters in the method signature </li></ul><ul><li>For e.g: </li></ul><ul><li>void nPrintln(String message, int n) </li></ul><ul><li>{ </li></ul><ul><li>for (int i=0; i<n; i++) </li></ul><ul><li>System.out.println(message); </li></ul><ul><li>} </li></ul><ul><li>To call the above method: </li></ul><ul><li>nPrintln(m, i); //where m is String and i is int </li></ul><ul><li>message and n are parameters </li></ul><ul><li>m and i are arguments </li></ul>
6. 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. 7. Overloading Methods <ul><li>Methods defined with the same name are overloaded. The parameter lists are different, the return type is not relevant. </li></ul><ul><li>Example 4.3 Overloading the max Method </li></ul><ul><li>double max(double num1, double num2) </li></ul><ul><li>{ </li></ul><ul><li>if (num1 > num2) </li></ul><ul><li>return num1; </li></ul><ul><li>else </li></ul><ul><li>return num2; </li></ul><ul><li>} </li></ul>TestMethodOverloading
8. 8. The scope of variables <ul><li>Local variable </li></ul><ul><li>Global variable </li></ul><ul><li>Scope of variable </li></ul>
9. 9. Method Abstraction <ul><li>You can think of the method body as a black box that contains the detailed implementation for the method. </li></ul>
10. 10. The Math Class <ul><li>Class constants: </li></ul><ul><ul><li>PI </li></ul></ul><ul><ul><li>E (the base of natural logarithms) </li></ul></ul><ul><li>Class methods: </li></ul><ul><ul><li>Trigonometric Methods </li></ul></ul><ul><ul><li>Exponent Methods </li></ul></ul><ul><ul><li>Miscellaneous </li></ul></ul>
11. 11. Trigonometric Methods <ul><li>sin(double a) </li></ul><ul><li>cos(double a) </li></ul><ul><li>tan(double a) </li></ul><ul><li>acos(double a) </li></ul><ul><li>asin(double a) </li></ul><ul><li>atan(double a) </li></ul>
12. 12. Exponent Methods <ul><li>exp(double a) </li></ul><ul><ul><li>Returns e raised to the power of a . </li></ul></ul><ul><li>log(double a) </li></ul><ul><ul><li>Returns the natural logarithm of a . </li></ul></ul><ul><li>pow(double a, double b) </li></ul><ul><ul><li>Returns a raised to the power of b . </li></ul></ul><ul><li>sqrt(double a) </li></ul><ul><ul><li>Returns the square root of a . </li></ul></ul>
13. 13. Rounding Methods <ul><li>double ceil(double x) </li></ul><ul><ul><li>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. </li></ul></ul><ul><li>double floor(double x) </li></ul><ul><ul><li>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. </li></ul></ul><ul><li>double rint(double x) </li></ul><ul><ul><li>Returns the double value that is closest in value to the argument and is equal to a mathematical integer. </li></ul></ul><ul><li>int round(float x) </li></ul><ul><ul><li>Returns the closest int to the argument. </li></ul></ul><ul><li>Long round(double x) </li></ul><ul><ul><li>Returns the closest long to the argument. </li></ul></ul>
14. 14. min, max, abs, and random <ul><li>max(a, b) and min(a, b) </li></ul><ul><ul><li>Returns the maximum or minimum of two parameters. </li></ul></ul><ul><li>abs(a) </li></ul><ul><ul><li>Returns the absolute value of the parameter. </li></ul></ul><ul><li>random() </li></ul><ul><ul><li>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). </li></ul></ul>
15. 15. Java Documentation <ul><li>http://java.sun.com/javase/6/docs/api/ </li></ul>
16. 16. Using Math Methods <ul><li>Example 4.4 Computing Mean and Standard Deviation </li></ul><ul><li>Generate 10 random numbers and compute the mean and standard deviation </li></ul>ComputeMeanDeviation
17. 17. Case Studies <ul><li>Example 4.5 Displaying Calendars </li></ul><ul><li>The program reads in the month and year and displays the calendar for a given month of the year. </li></ul>PrintCalendar
18. 18. Design Diagram printCalendar (main) readInput printMonth printMonthTitle printMonthBody getMonthName getStartDay getTotalNumberOfDays getNumberOfDaysInMonth isLeapYear
19. 19. Recursion <ul><li>E.g. 4.6 Factorial </li></ul><ul><li>Factorial(0)=1; </li></ul><ul><li>Factorial(n)=n*factorial(n-1) </li></ul>
20. 20. Recursion <ul><li>E.g. 4.7 fibonacci numbers </li></ul><ul><li>Fib(0)=1; </li></ul><ul><li>Fib(1)=1; </li></ul><ul><li>Fib(n)=Fib(n-2)+Fib(n-1), n>=2 </li></ul>
21. 21. Recursion <ul><li>E.g. 4.8 Towers of Hanoi </li></ul><ul><li>3 Poles and n disks </li></ul><ul><ul><li>Move n disks from pole A to pole B. </li></ul></ul><ul><ul><li>One disk can be moved at any time </li></ul></ul><ul><ul><li>Big disk can’t placed on top of small disk </li></ul></ul>
22. 22. Recursion <ul><li>Towers of Hanoi </li></ul><ul><li>Solution </li></ul><ul><ul><li>If n=1, move disk 1 from A to B </li></ul></ul><ul><ul><li>Else </li></ul></ul><ul><ul><ul><li>Move n-1 disks from A to C, B as temporary </li></ul></ul></ul><ul><ul><ul><li>Move disk n from A to B </li></ul></ul></ul><ul><ul><ul><li>Move n-1 disks from C to B, A as temporary </li></ul></ul></ul>