Section 9.1 Worked Example: Writing Method Headers
Subgoals for Writing Methods.
- Define method header based on problem
- Define return statement at the end
-
Define method body/logic
- Determine types of logic (expression, selection, loop, etc.)
- Define internal variables
- Write statements
Note: This worked example will only cover subgoal 1.
Subsection 9.1.1
You can watch this video or read through the content below it.
Subsection 9.1.2
Problem 1: Write a public method header that accepts as parameters 2 integers and a String in this order and calculates the maximum time of something and returns a double value.
For this Worked Exmaple, we will focus on SG1: Define method header based on problem.
-
When you pick your method name:
- Make it something related to the use of the method.
- Recall that method names are never capitalized at the first letter.
- We will call this method maxTime
-
A full method header contains:
- access modifier,
- return type,
- method name, and
- full parameter list (data type parameter_name)
public double maxTime (int a, int b, String c) {}
A call to this method would look like:
double x = obj.maxTime(5, 3, "happy");
Problem 2: Write a public method that does not return anything but accepts as parameters 3 Strings and prints out a cheerful sentence with the input in it.
Note: The video for this problem is on page 9.4.
-
When you pick your method name:
- Make it something related to the use of the method.
- Recall that method names are never capitalized at the first letter.
- We will call this method cheerful
-
A full method header contains:
- access modifier,
- return type,
- method name, and
- full parameter list (data type parameter_name)
public void cheerful (String a, String b, String c) {}
A call to this method would look like:
obj.cheerful("alpha", "beta", "gamma");
Subsection 9.1.3 Practice Pages
You have attempted of activities on this page.