ChapterFivePartII

ChapterFivePartII - IT108 – Introduction to Methods Part...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: IT108 – Introduction to Methods, Part II George Mason University Irene Bruno, modified by Bill Pegram Reuse Methods from Other Classes NOTE: One of the benefits of methods is for reuse. If the max method is defined in the class TestMax, it can be invoked from other classes as well. If you create a new class Test , you can invoke the max method using ClassName.methodName (e.g., TestMax.max ). Trace Call Stack 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; } i is declared and initialized The main method is invoked. i: 5 Call Stacks The main method is invoked. Space required for the main method k: j: 2 i: 5 The max method is invoked. Space required for the max method result: 5 num2: 2 num1: 5 The max method is finished and the return value is sent to k. The main method is finished. Stack is empty Space required for the main method k: j: 2 i: 5 Space required for the main method k: 5 j: 2 i: 5 Trace Call Stack 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; } j is declared and initialized The main method is invoked. j: 2 i: 5 Trace Call Stack 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; } Declare k The main method is invoked. Space required for the main method k: j: 2 i: 5 Trace Call Stack 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; } Invoke max(i, j) The main method is invoked....
View Full Document

{[ snackBarMessage ]}

Page1 / 27

ChapterFivePartII - IT108 – Introduction to Methods Part...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online