Lecture10-2024
Lecture10-2024
‣ Functions
‣ Access modifiers
‣ Javadoc comments for methods
‣ Variable scope and Parameter
passing
h t t p : / / ho r st m a n n . c o m / b j l o 1/ i n de x . h t m l ‣ Dry-running a class
J AVA F UNCTIONS /M ETHODS
‣ Functions
‣ Access modifiers
‣ Javadoc comments for methods
‣ Variable scope and Parameter
passing
h t t p : / / ho r st m a n n . c o m / b j l o 1/ i n de x . h t m l ‣ Dry-running a class
Functions
Benefits Input
• Allows for program modularization
• Allows for code reuse
• Allows for code sharing
• Effective code management and
maintenance
Function f
Output
3
A closer look at a Java static method aka function
Access
Return Method Formal
modifier
type name parameters
Return statements
4
Java class structure – A Java Library!
Imports
public class ABC
{
public static int fOne(...) { // body }
public static String fTwo(...) { // body }
.
.
.
public static double fM(...) { // body }
public static void main(String[] args) {
.
.
.
}
}
6
Example 1 – A void function
❑ This function does not return a value but performs some
functionality
Write a Java function
public class Lib that displays the
{ following menu:
public static void displayMenu() { 1. Multiplication
System.out.printf(“1. Multiplication\n”); 2. Division
System.out.printf(“2. Division\n”); 3. Addition
System.out.printf(“3. Addition\n”); 4. Subtraction
System.out.printf(“4. Subtraction\n”); 5. Quit
System.out.printf(“5. Quit\n”);
}
public static void main(String[] args) {
Lib.displayMenu(); // invocation
}
}
7
Example 1 – A void function with proper
implementation
❑ All functions/methods must have Javadoc comments.
❑ - it includes a description of what the method does
❑ - it describes what the formal parameters are
❑ - it describes what is returned (if any) by the method
9
Example 2 – A boolean function with proper
implementation
10
Example 3 – A number function
❑ This function returns an integer or double value
}
}
return N; // N is prime!
}
public static void main(String[] args) {
int factor = Lib.largestFactor(1000);
}
}
11
J AVA F UNCTIONS /M ETHODS
‣ Functions
‣ Access modifiers
‣ Javadoc comments for methods
‣ Variable scope and Parameter
passing
h t t p : / / ho r st m a n n . c o m / b j l o 1/ i n de x . h t m l ‣ Dry-running a class
Example 3 – A number function with
implementation
public class Lib {
/**
* Method to compute the largest factor of a given integer other
* than 1 and N.
*
* @param N the integer to compute the largest factor of.
* @return the largest factor of the given integer, or N itself
* if N is prime
*/
public static int largestFactor(int N) {
⋮
}
}
13
Example 4 – A String function
❑ This function returns a String value
result = “Weekday”;
return result;
14
Calling functions from another class
15
POP QUIZ
‣ Functions
‣ Access modifiers
‣ Javadoc comments for methods
‣ Variable scope and Parameter
passing
h t t p : / / ho r st m a n n . c o m / b j l o 1/ i n de x . h t m l ‣ Dry-running a class
Access Modifiers
18
J AVA F UNCTIONS /M ETHODS
‣ Functions
‣ Access modifiers
‣ Javadoc comments for methods
‣ Local variables and Formal
parameters
h t t p : / / ho r st m a n n . c o m / b j l o 1/ i n de x . h t m l ‣ Variable scope and Parameter
passing
‣ Dry-running a class
Local variables and formal parameters
21
J AVA F UNCTIONS /M ETHODS
‣ Functions
‣ Access modifiers
‣ Javadoc comments for methods
‣ Variable scope and Parameter
passing
h t t p : / / ho r st m a n n . c o m / b j l o 1/ i n de x . h t m l ‣ Dry-running a class
Variable scope
import java.util.Scanner;
public class MyLib {
public static void main(String[] args) {...}
public static int max(int num1, int num2) { int i;... }
public static String myst(String i) { int num1; ...}
}
23
Variable scope
public class Example {
int x;
public void swap(int x, int y) {
int temp = x;
}
public static int max(int num1, int num2) {
int m = num1;
}
public static void main(String[] args) {
int x = 5;
for(int y = 1; y<=10; y++) {
int w = 5;
} Arrows show where
while (x <= 200) { visibility of a variable
int w = 6; ends.
}
}
}
24
What is the output?
public class Example {
public void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
public static int max(int num1, int num2) {
int m = num1;
if (num2 > m ) { m = num2; } return m;
}
public static void main(String[] args) {
int x = 5; int t = 7;
System.out.printf(“x = %d t = %d\n”, x, t);
Example test = new Example(); // magic!
test.swap(x, t);
System.out.printf(“x = %d t = %d\n”, x, t);
System.out.printf(“Max = %d\n”, Example.max(x, t));
}
}
25
J AVA F UNCTIONS /M ETHODS
‣ Functions
‣ Access modifiers
‣ Javadoc comments for methods
‣ Variable scope and Parameter
passing
h t t p : / / ho r st m a n n . c o m / b j l o 1/ i n de x . h t m l ‣ Dry-running a class
Executing a Java class
❑ Execution starts in the main method
❑ If there is no main method, a Java class can not be
executed.
main
x t args
5 7
test 123
123
test: Example
Console
x = 5, t = 7
Object diagram
_
27
Executing a Java class…
test.swap
temp ? x 5
y 7
Console
x = 5, t = 7
_
28
Executing a Java class…
test.swap
temp 5 x 5
y 7
Console
x = 5, t = 7
_
29
Executing a Java class…
test.swap
temp 5 x 7
y 7
Console
x = 5, t = 7
_
30
Executing a Java class…
test.swap
temp 5 x 7
y 5
Console
x = 5, t = 7
_
31
Executing a Java class…
❑ Method swap terminates and its box/frame is deleted
test.swap
temp 5 x 7
y 5
Console
x = 5, t = 7
x = 5, t = 7
32
What is the output?
public class Example2 {
public void changeXtox(String s) {
String t = “”;
for(int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ‘X’) { t = t + ‘x’;}
else { t = t + s.charAt(i);}
}
}
public static String middle(String s, int num) {
int mid = s.length / 2
return (mid % 2 ==0)? s.substring(mid-1, mid+num+1) :
s.substring(mid, mid+num+1);
}
public static void main(String[] args) {
String str = “Xmen live in the X-dimension”;
Example2 test = new Example2(); // magic!
test.changeXtox(str);
System.out.printf(“str = %s\n”, str);
System.out.printf(“mid = %s\n”, Example.middle(str, 2));
}
}
33
How arguments are passed to methods in Java
Arguments are passed-by-value in Java,
regardless of whether the value is an
address/reference to an object or its just a
value (primitive)
34
Method signature
❑ Used to differentiate methods – different signatures implies
different methods.
❑ Is made up of method name and data types of formal
parameters, in the order they appear in the declaration
❑ This allows us to reuse a method name!
Signatures:
isPrimeNumber( int) Same method name
gcd(int, int) overloaded method BUT different
randomChar(String) signatures!
main(String[])
gcd(long , long) overloaded method
35
Summary
36