CPCS202 06 Methods S19
CPCS202 06 Methods S19
Methods
Objectives
To define methods with formal parameters (§6.2).
To invoke methods with actual parameters (i.e., arguments) (§6.2).
To define methods with a return value (§6.3).
To define methods without a return value (§6.4).
To pass arguments by value (§6.5).
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).
To use method overloading and understand ambiguous overloading
(§6.8).
To determine the scope of variables (§6.9).
To apply the concept of method abstraction in software development
(§6.10).
To design and implement methods using stepwise refinement (§6.10).
sum = 0;
for (int i = 20; i <= 37; i++)
sum += i;
System.out.println("Sum from 20 to 37 is " + sum);
sum = 0;
for (int i = 35; i <= 49; i++)
sum += i;
System.out.println("Sum from 35 to 49 is " + sum);
}
}
Syntax:
modifier returnValueType methodName(list of parameters) {
// Method body
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
49 page 49
© Dr. Jonathan Cazalas Chapter 6: Methods
animation
50 page 50
© Dr. Jonathan Cazalas Chapter 6: Methods
animation
51 page 51
© Dr. Jonathan Cazalas Chapter 6: Methods
animation
52 page 52
© Dr. Jonathan Cazalas Chapter 6: Methods
animation
Declare result
53 page 53
© Dr. Jonathan Cazalas Chapter 6: Methods
animation
54 page 54
© Dr. Jonathan Cazalas Chapter 6: Methods
animation
55 page 55
© Dr. Jonathan Cazalas Chapter 6: Methods
animation
56 page 56
© Dr. Jonathan Cazalas Chapter 6: Methods
animation
57 page 57
© Dr. Jonathan Cazalas Chapter 6: Methods
Calling a Method
Activation Record and Call Stacks:
– While the last animated example was great, here is a
single image that shows the full process:
Click here to
view and trace
code
Remember:
– Step 1: Problem-solving Phase
– Step 2: Implementation Phase
Clearly, getting input from the user is easy and can be left for
later discussion
The main work is in printing the calendar
© Dr. Jonathan Cazalas Chapter 6: Methods page 135
Program 8: PrintCalendar
Step 1: Problem-solving Phase
– Problem Components:
And printing the calendar can also be broken down into two
components:
– Print the month title
– Print the month body
if (month == 2)
return isLeapYear(year) ? 29 : 28;
return 0; // If month is incorrect
}
readInput printMonth
printMonthTitle printMonthBody
getMonthName getStartDay
getTotalNumOfDays
getNumOfDaysInMonth
isLeapYear
readInput printMonth
printMonthTitle printMonthBody
getMonthName getStartDay
getTotalNumOfDays
getNumOfDaysInMonth
isLeapYear
readInput printMonth
printMonthTitle printMonthBody
getMonthName getStartDay
getTotalNumOfDays
getNumOfDaysInMonth
isLeapYear
readInput printMonth
printMonthTitle printMonthBody
getMonthName getStartDay
getTotalNumOfDays
getNumOfDaysInMonth
isLeapYear
readInput printMonth
printMonthTitle printMonthBody
getMonthName getStartDay
getTotalNumOfDays
getNumOfDaysInMonth
isLeapYear
readInput printMonth
printMonthTitle printMonthBody
getMonthName getStartDay
getTotalNumOfDays
getNumOfDaysInMonth
isLeapYear
readInput printMonth
printMonthTitle printMonthBody
getMonthName getStartDay
getTotalNumOfDays
getNumOfDaysInMonth
isLeapYear