9/21/18 Opening Problem Chapter 6: Methods Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 1 2 rights reserved. rights reserved. Problem Problem int sum = 0; int sum = 0; for (int i = 1; i <= 10; i++) for (int i = 1; i <= 10; i++) sum += i; sum += i; System.out.println("Sum from 1 to 10 is " + sum); System.out.println("Sum from 1 to 10 is " + sum); sum = 0; sum = 0; for (int i = 20; i <= 30; i++) for (int i = 20; i <= 30; i++) sum += i; sum += i; System.out.println("Sum from 20 to 30 is " + sum); System.out.println("Sum from 20 to 30 is " + sum); sum = 0; sum = 0; for (int i = 35; i <= 45; i++) for (int i = 35; i <= 45; i++) sum += i; sum += i; System.out.println("Sum from 35 to 45 is " + sum); System.out.println("Sum from 35 to 45 is " + sum); Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 3 4 rights reserved. rights reserved. Solution Objectives public static int sum( int i1, int i2) { To define methods with formal parameters (§6.2). § int sum = 0; To invoke methods with actual parameters (i.e., arguments) (§6.2). § for (int i = i1; i <= i2; i++) To define methods with a return value (§6.3). § To define methods without a return value (§6.4). sum += i; § To pass arguments by value (§6.5). return sum; § To develop reusable code that is modular, easy to read, easy to debug, and } § easy to maintain (§6.6). To write a method that converts hexadecimals to decimals (§6.7). § public static void main(String[] args) { To use method overloading and understand ambiguous overloading § System.out.println("Sum from 1 to 10 is " + sum(1, 10)); (§6.8). System.out.println("Sum from 20 to 30 is " + sum(20, 30)); To determine the scope of variables (§6.9). § To apply the concept of method abstraction in software development System.out.println("Sum from 35 to 45 is " + sum(35, 45)); § (§6.10). } To design and implement methods using stepwise refinement (§6.10). § Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 5 6 rights reserved. rights reserved. 1
9/21/18 Defining Methods Defining Methods A method is a collection of statements that are A method is a collection of statements that are grouped together to perform an operation. grouped together to perform an operation. Define a method Invoke a method Define a method Invoke a method return value method formal modifier type name parameters int z = max(x, y); int z = max(x, y); method public static int max( int num1, int num2) { public static int max( int num1, int num2) { header actual parameters actual parameters int result; int result; (arguments) (arguments) method body parameter list if (num1 > num2) if (num1 > num2) result = num1; result = num1; else else method result = num2; result = num2; signature return result; return result; return value } } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 7 8 rights reserved. rights reserved. Method Signature Formal Parameters Method signature is the combination of the method name and the The variables defined in the method header are known as parameter list. formal parameters . Define a method Invoke a method Define a method Invoke a method return value return value method formal method formal modifier type name modifier type name parameters parameters int z = max(x, y); int z = max(x, y); method method public static int max( int num1, int num2) { public static int max( int num1, int num2) { header header actual parameters actual parameters int result; int result; (arguments) (arguments) method method body parameter list body parameter list if (num1 > num2) if (num1 > num2) result = num1; result = num1; else else method method result = num2; result = num2; signature signature return result; return result; return value return value } } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 9 10 rights reserved. rights reserved. Actual Parameters Return Value Type A method may return a value. The returnValueType is the data type When a method is invoked, you pass a value to the parameter. This of the value the method returns. If the method does not return a value is referred to as actual parameter or argument . value, the returnValueType is the keyword void. For example, the returnValueType in the main method is void. Define a method Invoke a method Define a method Invoke a method return value method formal return value method formal modifier type name parameters modifier type name parameters int z = max(x, y); int z = max(x, y); method method public static int max( int num1, int num2) { public static int max( int num1, int num2) { header header actual parameters actual parameters int result; int result; (arguments) (arguments) method method body parameter list parameter list body if (num1 > num2) if (num1 > num2) result = num1; result = num1; else else method method result = num2; result = num2; signature signature return result; return value return result; return value } } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 11 12 rights reserved. rights reserved. 2
9/21/18 animation Calling Methods Calling Methods, cont. Testing the max method This program demonstrates calling a method max pass the value of i pass the value of j to return the largest of the int values 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; } Run TestMax Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 13 14 rights reserved. rights reserved. animation animation Trace Method Invocation Trace Method Invocation i is now 5 j is now 2 public static void main(String[] args) { public static int max(int num1, int num2) { public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int i = 5; int result; int j = 2; int j = 2; int k = max(i, j); if (num1 > num2) int k = max(i, j); if (num1 > num2) result = num1; result = num1; System.out.println( else System.out.println( else "The maximum between " + i + result = num2; "The maximum between " + i + result = num2; " and " + j + " is " + k); " and " + j + " is " + k); } } return result; return result; } } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 15 16 rights reserved. rights reserved. animation animation Trace Method Invocation Trace Method Invocation invoke max(i, j) 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) { public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int i = 5; int result; int j = 2; int j = 2; int k = max(i, j); if (num1 > num2) int k = max(i, j); if (num1 > num2) result = num1; result = num1; System.out.println( else System.out.println( else "The maximum between " + i + result = num2; "The maximum between " + i + result = num2; " and " + j + " is " + k); " and " + j + " is " + k); } } return result; return result; } } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 17 18 rights reserved. rights reserved. 3
Recommend
More recommend