Download presentation
Presentation is loading. Please wait.
Published byLinette Lawson Modified over 8 years ago
1
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5
2
Lecture Contents: Method definition –Method signature –Formal parameters –Return value type Method invocation –Actual arguments Parameter passing mechanism: By value Benefits of using methods
3
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Opening Problem Find the sum of integers: from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
4
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 4 Problem int sum = 0; for (int i = 1; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for (int i = 20; i <= 30; i++) sum += i; System.out.println("Sum from 20 to 30 is " + sum); sum = 0; for (int i = 35; i <= 45; i++) sum += i; System.out.println("Sum from 35 to 45 is " + sum);
5
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 5 Problem int sum = 0; for (int i = 1; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for (int i = 20; i <= 30; i++) sum += i; System.out.println("Sum from 20 to 30 is " + sum); sum = 0; for (int i = 35; i <= 45; i++) sum += i; System.out.println("Sum from 35 to 45 is " + sum);
6
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 6 Solution public static int sum(int i1, int i2) { int sum = 0; for (int i = i1; i <= i2; i++) sum += i; return sum; } public static void main(String[] args) { System.out.println("Sum from 1 to 10 is " + sum(1, 10)); System.out.println("Sum from 20 to 30 is " + sum(20, 30)); System.out.println("Sum from 35 to 45 is " + sum(35, 45)); }
7
7 Valid reasons to create a method t Avoid duplicate code –Usually functions are specified to be called many times.
8
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 8 Defining Methods A method is a collection of statements that are grouped together to perform an operation.
9
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 9 Method Signature Method signature is the combination of the method name and the parameter list.
10
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 10 Formal Parameters The variables defined in the method header are known as formal parameters.
11
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 11 Actual Parameters/Arguments When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.
12
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 12 Return Value Type 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.
13
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 13 Calling Methods Testing the max method This program demonstrates calling a method max to return the largest of the int values TestMax
14
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 14 Calling Methods public class TestMax { 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); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; }
15
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 15 Calling Methods, cont.
16
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 16 Trace Method Invocation i is now 5
17
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 17 Trace Method Invocation j is now 2
18
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 18 Trace Method Invocation invoke max(i, j)
19
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 19 Trace Method Invocation invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2
20
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 20 Trace Method Invocation declare variable result
21
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 21 Trace Method Invocation (num1 > num2) is true since num1 is 5 and num2 is 2
22
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 22 Trace Method Invocation result is now 5
23
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 23 Trace Method Invocation return result, which is 5
24
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 24 Trace Method Invocation return max(i, j) and assign the return value to k
25
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 25 Trace Method Invocation Execute the print statement
26
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 26 Reuse Methods from Other Classes NOTE: Another One of the benefits of methods is for reuse or so called Code Reusability. The max method can be invoked from any class besides TestMax. If you create a new class Test, you can invoke the max method using ClassName.methodName (e.g., TestMax.max).
27
27 Valid reasons to create a method t Code reusability t Avoid duplicate code –Usually functions are specified to be called many times.
28
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 28 Call Stacks
29
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 29 Trace Call Stack i is declared and initialized animation
30
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 30 Trace Call Stack j is declared and initialized animation
31
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 31 Trace Call Stack Declare k animation
32
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 32 Trace Call Stack Invoke max(i, j) animation
33
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 33 Trace Call Stack pass the values of i and j to num1 and num2 animation
34
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 34 Trace Call Stack Declare result animation
35
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 35 Trace Call Stack (num1 > num2) is true animation
36
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 36 Trace Call Stack Assign num1 to result animation
37
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 37 Trace Call Stack Return result and assign it to k animation
38
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 38 Trace Call Stack Execute print statement animation
39
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 39 void Method Example This type of method does not return a value. The method performs some actions: Display student grade as a letter (A … F) depending on student score as a numeric value (0.0 … 100.0) TestVoidMethod
40
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 40 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? Can you invoke the method using nPrintln(15, “Computer Science”);
41
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 41 Pass by Value This program demonstrates passing values to the methods. Increment
42
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 42 Pass by Value public class Increment { public static void main(String[] args) { int x = 1; System.out.println("Before the call, x is " + x); increment(x); System.out.println("after the call, x is " + x); } public static void increment(int n) { n++; System.out.println("n inside the method is " + n); }
43
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 43 Pass by Value Testing Pass by value: important example public static void swap(int n1, int n2) {…} This program demonstrates passing values to the methods. TestPassByValue
44
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 44 Pass by Value, cont.
45
45 Valid reasons to create a method: t Reduce complexity –“Properly designed functions permit to ignore how a job’s done. Knowing what is done is sufficient.” B.Kernighan & D.Ritchie –“A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation. ” B.Kernighan & D.Ritchie t Avoid duplicate code t Code reusability
46
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 46 Modularizing Code Methods can be used to reduce redundant coding and enable code reuse. Methods can also be used to modularize code and improve the quality of the program. GreatestCommonDivisorMethod PrimeNumberMethod
47
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 47 Modularizing Code Methods can be used to reduce redundant coding and enable code reuse. Methods can also be used to modularize code and improve the quality of the program. Modify the PrimeNumberMethod to PerfectNumberMethod PerfectNumberMethod
48
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 48 Problem: Converting Decimals to Hexadecimals Write a method that converts a decimal integer to a hexadecimal. Decimal2HexConversion
49
49 Converting Decimals to Hexadecimals import java.util.Scanner; public class Decimal2HexConversion { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Create a Scanner System.out.print("Enter a decimal number: "); int decimal = input.nextInt(); System.out.println("The hex number for decimal " + decimal + " is " + decimalToHex(decimal)); } // end of main
50
50 Converting Decimals to Hexadecimals /** Convert a decimal to a hex as a string */ public static String decimalToHex(int decimal) { String hex = ""; while (decimal != 0) { int hexValue = decimal % 16; hex = toHexChar(hexValue) + hex; decimal = decimal / 16; } return hex; } public static char toHexChar(int hexValue) { if(hexValue =0) return (char)(hexValue+'0'); else return (char)(hexValue - 10 + 'A'); }
51
51 Problem: Converting Decimals to Octals Write a method that converts a decimal integer to an octal.
52
52 Problem: Converting Decimals to Binaries Write a method that converts a decimal integer to a binary.
53
Thank You For Your Attention!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.