🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Chapter 1.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

GodlikeBoron

Uploaded by GodlikeBoron

Tags

programming java methods

Full Transcript

Ahmad Khoj King Abdulaziz University KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ KING ABDULAZIZ UNIVERSITY Chapter 1 Methods KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ Introducing Method...

Ahmad Khoj King Abdulaziz University KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ KING ABDULAZIZ UNIVERSITY Chapter 1 Methods KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ Introducing Methods A method is a collection of statements that are grouped together to perform an operation. Define a method Invoke a method modifier return value type method name formal parameters method public static int max(int num1, int num2) { int z = max(x, y); header int result; method actual parameters body parameter list (arguments) if (num1 > num2) result = num1; else result = num2; return value return result; } 3 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ Introducing Methods, cont. Method signature is the combination of the method name and the parameter list. The variables defined in the method header are known as formal parameters. When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. 4 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ Introducing Methods, cont. A method may return a value. The returnValueType is the data type of the value the method returns. If the method does not return a value, the returnValueType is the keyword void. For example, the returnValueType in the main method is void. 5 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ animation Calling Methods, cont. pass the value of i pass the value of j public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; int k = max(i, j); if (num1 > num2) result = num1; System.out.println( else "The maximum between " + i + result = num2; " and " + j + " is " + k); } return result; } 6 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ animation Trace Method Invocation i is now 5 public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; int k = max(i, j); if (num1 > num2) result = num1; System.out.println( else "The maximum between " + i + result = num2; " and " + j + " is " + k); } return result; } 7 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ animation Trace Method Invocation j is now 2 public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; int k = max(i, j); if (num1 > num2) result = num1; System.out.println( else "The maximum between " + i + result = num2; " and " + j + " is " + k); } return result; } 8 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ animation Trace Method Invocation invoke max(i, j) public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; int k = max(i, j); if (num1 > num2) result = num1; System.out.println( else "The maximum between " + i + result = num2; " and " + j + " is " + k); } return result; } 9 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ animation Trace Method Invocation invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2 public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; int k = max(i, j); if (num1 > num2) result = num1; System.out.println( else "The maximum between " + i + result = num2; " and " + j + " is " + k); } return result; } 10 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ animation Trace Method Invocation declare variable result public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; int k = max(i, j); if (num1 > num2) result = num1; System.out.println( else "The maximum between " + i + result = num2; " and " + j + " is " + k); } return result; } 11 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ animation Trace Method Invocation (num1 > num2) is true since num1 is 5 and num2 is 2 public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; int k = max(i, j); if (num1 > num2) result = num1; System.out.println( else "The maximum between " + i + result = num2; " and " + j + " is " + k); } return result; } 12 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ animation Trace Method Invocation result is now 5 public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; int k = max(i, j); if (num1 > num2) result = num1; System.out.println( else "The maximum between " + i + result = num2; " and " + j + " is " + k); } return result; } 13 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ animation Trace Method Invocation return result, which is 5 public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; int k = max(i, j); if (num1 > num2) result = num1; System.out.println( else "The maximum between " + i + result = num2; " and " + j + " is " + k); } return result; } 14 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ animation Trace Method Invocation return max(i, j) and assign the return value to k public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; int k = max(i, j); if (num1 > num2) result = num1; System.out.println( else "The maximum between " + i + result = num2; " and " + j + " is " + k); } return result; } 15 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ animation Trace Method Invocation Execute the print statement public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; int k = max(i, j); if (num1 > num2) result = num1; System.out.println( else "The maximum between " + i + result = num2; " and " + j + " is " + k); } return result; } 16 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ Passing Parameters public static void nPrintln(String message, int n) { for (int i = 0; i < n; i++) System.out.println(message); } Suppose you invoke the method using nPrintln(“Welcome to Java”, 5); What is the output? Suppose you invoke the method using nPrintln(“Computer Science”, 15); What is the output? 17 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ CAUTION A return statement is required for a nonvoid method. The following method is logically correct, but it has a compilation error, because the Java compiler thinks it possible that this method does not return any value. public static int sign(int n) { if (n > 0) return 1; else if (n == 0) return 0; else if (n < 0) return –1; } To fix this problem, delete if (n num2) return num1; else return num2; } You can create more than methods in the same name if one of the following conditions is met: - Change datatype of parameters - Rearrange datatype of parameters - Change number of parameters 23 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ change datatype of parameters Overloading Methods Error 24 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ Rearrange datatype of parameters Overloading Methods Error 25 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ change number of parameters Overloading Methods Error 26 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ Overloading Methods Output: Output: First method Second method 27 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ Ambiguous Invocation Sometimes there may be two or more possible matches for an invocation of a method, but the compiler cannot determine the most specific match. This is referred to as ambiguous invocation. Ambiguous invocation is a compilation error. 28 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ Ambiguous Invocation 29 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ Overloading Methods 30 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ The Math Class 31 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬  Create the following methods to calculate area of (Triangle and Rectangle): 1. public static void triangleArea (double base , double height) 2. public static double recArea(double length , double width)  What you should have inside each method: 1. triangleArea method: calculate triangle area using this formula: triangle = 0.5 * base * height 2. recArea method: calculate rectangle area using this formula: rectangle = length * width  Main – Make decision by using switch to choose which area will be calculated – When user chooses one, prompt him to enter base and height for triangle – When user chooses two, prompt him to enter length and width for rectangle – When user chooses other numbers, display this message “Wrong Choice” 32 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ 33 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬ Benefits of Methods Write a method once and reuse it anywhere. Information hiding: hide the implementation from the user. Reduce complexity. 34 KING ABDULAZIZ UNIVERSITY ‫جامعة الملك عبد العزيز‬

Use Quizgecko on...
Browser
Browser