This document discusses methods in Java. It defines methods as collections of statements grouped together to perform an operation. It covers defining methods, invoking methods, passing parameters, return values, scope of variables, overloading methods, and abstracting method implementation through black box abstraction. Examples are provided to demonstrate key concepts like defining methods with different parameter types, passing arguments by value, and generating random characters through a method. The document is from a chapter in an introductory Java programming textbook.
If this PowerPoint presentation contains mathematical equations, you may need to check that your computer has the following installed:
1) MathType Plugin
2) Math Player (free versions available)
3) NVDA Reader (free versions available)
Slides in this presentation contain hyperlinks. JAWS users should be able to get a list of links by using INSERT+F7
for left parenthesis int i = 1 semi colon, i is less than or equal to 10 semi colon i ++ right parenthesis
sum + = i semi colon
system.out.prinln left parenthesis start double quotation marks Sum from 1 to 10 is end double quotation marks + sum right parenthesis semi colon
sum = 0 semi colon
for left parenthesis int i = 20 semi colon, i is less than or equal to 30 semi colon i ++ right parenthesis
sum + = i semi colon
system.out.prinln left parenthesis start double quotation marks Sum from 20 to 30 is end double quotation marks + sum right parenthesis semi colon
sum = 0 semi colon
for left parenthesis int i = 35 semi colon, i is less than or equal to 45 semi colon i ++ right parenthesis
sum + = i semi colon
system.out.prinln left parenthesis start double quotation marks Sum from 35 to 45 is end double quotation marks + sum right parenthesis semi colon
int sum = 0 semi colon
for left parenthesis int i = i1 semi colon i is less than or equal to i2 semi colon i ++ right parenthesis
sum + = i semi colon
return sum semi colon
right brace
public static void main left parenthesis string left bracket right bracket args right parenthesis left brace
system.out.println left parenthesis start double quotation marks Sum from 1 to 10 is end double quotation marks + sum left parenthesis 1, 10 right parenthesis, right parenthesis semi colon (sum left parenthesis 1, 10 right parenthesis is highlighted)
system.out.println left parenthesis start double quotation marks Sum from 20 to 30 is end double quotation marks + sum left parenthesis 20, 30 right parenthesis, right parenthesis semi colon (sum left parenthesis 20, 30 right parenthesis is highlighted)
system.out.println left parenthesis start double quotation marks Sum from 35 to 45 is end double quotation marks + sum left parenthesis 35, 45 right parenthesis, right parenthesis semi colon (sum left parenthesis 35, 45 right parenthesis is highlighted)
right brace
MethodDemo: https://liveexample.pearsoncmg.com/html/MethodDemo.html
Define a method
public static int max open parenthesis int num 1, int num 2 close parenthesis open braces int result Semicolon
if open parenthesis num 1 greater than num 2 close parenthesis result = num 1 Semicolon
else
result = num 2 Semicolon
return result Semicolon close braces
Invoke a method.
int z = max open parenthesis x, y close parenthesis Semicolon
x and y are labeled as actual parameters, arguments
Define a method.
Public static int max open parenthesis num 1, int num 2 close parenthesis open braces int results Semicolon This line is labeled, method header. Public static are labeled, modifier. int is labeled return value type. max is labeled method name. num 1 and num 2 are labeled formal parameters. information within the parenthesis is labeled parameter list. max open parenthesis int num 1, int num 2 close parenthesis is labeled, method signature.
Below is the method body.
if open parenthesis num 1 greater than num 2 close parenthesis result = num 1 Semicolon
else
result = num 2
return result Semicolon It is labeled return value.
Invoke a method.
int z = max open parenthesis x, y close parenthesis Semicolon
x and y are labeled as actual parameters (arguments).
Define a method.
Public static int max open parenthesis num 1, int num 2 close parenthesis open braces int results Semicolon This line is labeled, method header. Public static are labeled, modifier. int is labeled return value type. max is labeled method name. num 1 and num 2 are labeled formal parameters. information within the parenthesis is labeled parameter list. max open parenthesis int num 1, int num 2 close parenthesis is labeled, method signature.
Below is the method body.
if open parenthesis num 1 greater than num 2 close parenthesis result = num 1 Semicolon
else
result = num 2
return result Semicolon It is labeled return value.
Invoke a method.
int z = max open parenthesis x, y close parenthesis Semicolon
x and y are labeled as actual parameters, arguments.
Define a method.
Public static int max open parenthesis num 1, int num 2 close parenthesis open braces int results Semicolon This line is labeled, method header. Public static are labeled, modifier. int is labeled return value type. max is labeled method name. num 1 and num 2 are labeled formal parameters. information within the parenthesis is labeled parameter list. max open parenthesis int num 1, int num 2 close parenthesis is labeled, method signature.
Below is the method body.
if open parenthesis num 1 greater than num 2 close parenthesis result = num 1 Semicolon
else
result = num 2
return result Semicolon It is labeled return value.
Invoke a method.
int z = max open parenthesis x, y close parenthesis Semicolon
x and y are labeled as actual parameters, arguments.
Define a method.
Public static int max open parenthesis num 1, int num 2 close parenthesis open braces int results Semicolon This line is labeled, method header. Public static are labeled, modifier. int is labeled return value type. max is labeled method name. num 1 and num 2 are labeled formal parameters. information within the parenthesis is labeled parameter list. max open parenthesis int num 1, int num 2 close parenthesis is labeled, method signature.
Below is the method body.
if open parenthesis num 1 greater than num 2 close parenthesis result = num 1 Semicolon
else
result = num 2
return result Semicolon It is labeled return value.
Invoke a method.
int z = max open parenthesis x, y close parenthesis Semicolon
x and y are labeled as actual parameters (arguments).
Define a method.
Public static int max open parenthesis num 1, int num 2 close parenthesis open braces int results Semicolon This line is labeled, method header. Public static are labeled, modifier. int is labeled return value type. max is labeled method name. num 1 and num 2 are labeled formal parameters. information within the parenthesis is labeled parameter list. max open parenthesis int num 1, int num 2 close parenthesis is labeled, method signature.
Below is the method body.
if open parenthesis num 1 greater than num 2 close parenthesis result = num 1 Semicolon
else
result = num 2
return result Semicolon It is labeled return value.
Invoke a method.
int z = max open parenthesis x, y close parenthesis Semicolon
x and y are labeled as actual parameters, arguments.
Public static void main open parenthesis String open bracket close bracket args close parenthesis open braces int i = 5 Semicolon
int j = 2 Semicolon
int k = max open parenthesis I, j close parenthesis Semicolon
System.out.printIn open parenthesis open double quotes The maximum between close double quotes + i + open double quotes and close double quotes + j + open double quotes is close double quotes + k close parenthesis Semicolon
public static int max open parenthesis int num 1, int num 2 close parenthesis open braces int result Semicolon
if open parenthesis num 1 greater than num 2 close parenthesis
result = num 1 Semicolon
else
result = num 2 Semicolon
return result Semicolon
The i and j are written as num 1 and num 2 respectively.
public static int max is written in both the images.
Semicolon after max open parenthesis i, j close parenthesis is written as close braces in the second image.
invoke max open parenthesis i, j close parenthesis, Pass the value of i to num 1, Pass the value of j to num 2, points to the text box, int max open parenthesis num 1, num 2 close parenthesis. The text box with int k = max open parenthesis i, j close parenthesis, and the text box int max open parenthesis num 1, num 2 close parenthesis.
open parenthesis num 1 greater than num 2 close parenthesis is true since num 1 is 5 and num 2 is 2, points to the text if open parenthesis num 1 greater than num 2 close parenthesis. The arrow is also connected to the text, int k = max open parenthesis i, j close parenthesis.
Execute the print statement, points to the text, System.out.printIn open parenthesis open double quotes The maximum between close double quotes + i + open double quotes and close double quotes + j + open double quotes is close double quotes + k close parenthesis.
A. The main method is invoked. The stack has activation record for the main method, with the code k: blank, j: 2, and i: 5.
B. The max method is invoked. The stack is divided into two parts. The upper part shows Activation record for the max method, with result: blank, num 2: 2, and num 1: 5. The lower part shows Activation record for the main method, with k: blank, j: 2, and i: 5. An arrow points from j: 2 to num 2: 2. Another arrow points from i: 5 points to num 1: 5.
C. The max method is being executed. The stack is divided into two parts. The upper part shows Activation record for the max method, with result: 5, num 2: 2, and num 1: 5. The lower part shows Activation record for the main method, with k: blank, j: 2, and i: 5.
d. The max method is finished and the return value is sent to k. The stack reads, Activation record for the main method, with k: 5, j: 2, and i: 5. An arrow from stack C's result: 5 points to k: 5 in stack D.
E. The main method is finished. The stack is empty.
Invoke max open parenthesis i, j close parenthesis, points to the text, int k = max open parenthesis i, j close parenthesis. The text, max open parenthesis i, j close parenthesis is connected to public static int max open parenthesis int num 1, int num 2 close parenthesis.
pass the values of i and j to num 1 and num 2, points to the text, max open parenthesis int num 1, int num 2 close parenthesis. This text is also connected to the call stack, that is divided into two parts. The upper part shows num 2: 2, and num 1: 1. The lower part shows Space required for the main method k: blank, j: 2, i: 5. An arrow points from j: 2 to num 2: 2. Another arrow points from i: 5 to num 1: 5.
Declare result, points to the text, int result. The text is also connected to the upper part of the call stack that depicts result: blank, num 2: 2, an num 1: 5. the lower part of the stack reads, Space required for the main method. K: blank. j: 2. i: 5. An arrow points from j: 2 in the lower part of call stack to num 2: 2 in its upper part. Another arrow points from i: 5 in the lower part of call stack to num 1: 5 in its upper part.
open parenthesis num 1 greater than num 2 close parenthesis is true, points to the text, if open parenthesis num 1 greater than num 2 close parenthesis. An arrow points from j: 2 in the lower part of call stack to num 2: 2 in its upper part. Another arrow points from i: 5 in the lower part of call stack to num 1: 5 in its upper part.
Assign num 1 to result, points to the text, result = num 1. The text is connected to result: 5 in the upper part of call stack. An arrow points from j: 2 in the lower part of call stack to num 2: 2 in its upper part. Another arrow points from i: 5 in the lower part of call stack to num 1: 5 in its upper part.
Return result and assign it to k, points to the text, return result. The text is connected to int k = max open parenthesis i, j close parenthesis and also with k: 5 in the lower part of call stack. An arrow points from j: 2 in the lower part of the call stack to num 2: 2 in its upper part. Another arrow points from i: 5 in the lower part of the stack to num 1: 5 in its upper part. An arrow points from result: 5 in the upper part of the call stack to k: 5 in its lower part.
Execute print statement, points to the text, System.out.printIn open parenthesis open double quotes The maximum between close double quotes + i + open double quotes and close double quotes + j + open double quotes is close double quotes + k close parenthesis.
The main method is invoked. The stack has activation record for the main method, with the code num 2: 2, and num 1: 1.
The swap method is invoked. The stack is divided into two parts. The upper part shows Activation record for the swap method, with temp: blank, n 2: 1, and n 1: 2. The lower part shows Activation record for the main method, with num 2: 2, and num 1: 1. An arrow points from num 2: 2 to n 2: 2. This arrow is labeled as The values of num 1 and num 2 are passed to n 1 and n 2. Another arrow points from num 1: 1 points to n 1: 1.
The swap method is executed. The stack is divided into two parts. The upper part shows Activation record for the swap method, with temp: 1, n 2: 1, and n 1: 2. The values for n 1 and n 2 are swapped, but it does not affect num 1 and num 2. The lower part shows Activation record for the main method, with num 2: 2, and num 1: 5.
The swap method is finished. The stack reads, Activation record for the main method, with num 2: 2, num 1: 1, and i: 5.
The main method is finished. The stack is empty.
A times 16 to the third power + B times 16 to the second power + C times 16 to the first power + D times 16 to the 0 power
= left parenthesis, left parenthesis A times 16 + B right parenthesis times 16 + C right parenthesis times 16 + D
= left parenthesis, left parenthesis 10 times 16 + 11 right parenthesis times 16 + 12 right parenthesis times 16 + 13 = question mark
Hex2Dec: https://liveexample.pearsoncmg.com/html/Hex2Dec.html
public static void method 1 open parenthesis close parenthesis open braces, vertical ellipsis.
For open parenthesis int i = 1 Semicolon i less than 10 Semicolon i plus plus close parenthesis open braces, vertical ellipsis.
Int j, vertical ellipsis. open braces open braces.
from int j to open braces, it is under bracket, labeled as The scope of j. From for open parenthesis int i = 1 to open braces, it is under bracket, labeled as, The scope of i.
public static void method open parenthesis close parenthesis open braces int x = 1 Semicolon
int y = 1 Semicolon
for open parenthesis int i = 1 Semicolon I less than 10 Semicolon I plus plus close parenthesis open braces x + = i Semicolon
close braces
for open parenthesis int i = 1 Semicolon I less than 10 Semicolon I plus plus close parenthesis y + = i Semicolon
An arrow labeled, It is fine to declare i in two non-nesting blocks, point to the close braces after x + = i, and y + = i.
public static void method 2 open parenthesis close parenthesis open braces int i = 1 Semicolon
int sum = 0 Semicolon
for open parenthesis int i = 1 Semicolon i less than 10 Semicolon i plus plus close parenthesis sum + = i Semicolon close braces close braces.
Two arrows labeled, It is wrong to declare i in two nesting blocks, point to the region of for open parenthesis int i = 1 Semicolon i less than 10 Semicolon i plus plus close parenthesis sum + = i Semicolon close braces, and the region of int i = 1 till the last close brace.
A downward arrow labeled Optional arguments for input, points to Method header. An upward arrow moves up from Method header. This arrow is labeled as Optional return value.
showing dates from 1 to 30, for the month of April. While April 1 is on Wednesday, April 30 is on Thursday.
The information written above the dates is:
C: backslash symbol of greater than java Print Calendar. Enter full year open parenthesis example 2001 close parenthesis 2009. Enter month in number between 1 and 12: 4 April 2009.
PrintCalendar: https://liveexample.pearsoncmg.com/html/PrintCalendar.html