Turning an English Task into a Method
Where you are: Week 0 review > Turning an English task into a method
Try This First
Read this task and decide the return type and the parameters before writing any code:
“Write code that takes two
intvalues and returns the larger one.”
Reveal
The return type is int (it returns one of the int values). The parameters are two int values (int a, int b). The English sentence already names the return type and the parameters; the body is the last thing to write.
Before You Start
Check each box you can do from memory. A box you cannot check yet is not a problem; it points you to a quick refresher, not a grade.
See The Method Header and How a Method Works if any box above is unclear.
Not sure? Take the 60-second self-check.
Try each from memory, then read the answer under it.
- What does the return type in a method header promise? The type of value the method hands back to its caller.
- What does a parameter list declare? The inputs the method needs, each with a name and a type.
What You Need To Walk In With
Walk into the next class able to state these:
- The English sentence hands you the signature: a noun for the return type, the supplied data for the parameters, and a verb for the name.
- Decide the return type, the name, and the parameters before writing any body.
- If a method name needs the word “and,” it is doing two jobs; split it into two methods.
- A typed method must return a value on every path that reaches the end.
You should be able to: turn a one-sentence task into a correct method header, and recognize a method that should be split.
How It Works
Run the task through six steps that map onto the header slots:
- Identify the return type. Find the noun for what the method produces. “Returns the larger value” gives
int; “prints the table” givesvoid; “returns true if prime” givesboolean. - Choose a name. Use a verb phrase for
voidmethods and a verb or noun phrase for typed ones. If the name contains “and,” two methods have been fused; split them. - List the parameters. Ask what data the body needs that the caller must supply. Each piece becomes one
Type nameparameter. - List the constructs the body will need: a conditional, a loop, an accumulator. A comment is fine at first.
- Write the body, then add the
returnon every branch that needs one. - Check the contract. Read the header aloud: does the name match what the body does, is every parameter used, is the return type what the caller expects?
Worked application
Task: “Take an int count and a char symbol, and return a String of that character repeated count times.”
- Return type:
String. Name:repeatChar(one job, no “and”). Parameters:int count, char symbol. - Constructs: a loop and a
Stringaccumulator.
public static String repeatChar(int count, char symbol) {
String result = "";
for (int i = 0; i < count; i++) {
result = result + symbol;
}
return result;
}
Worked Example: Predict, Then Check
A task reads: “Write a method that prints a countdown from n to 1.” What is the return type?
Reveal
void. The task says “prints,” which is a side effect, not a value handed back. The single parameter is int n.
A Common Mistake
A method named with “and,” such as readAndAverage, announces that two jobs live in one body. The method can then be wrong for two unrelated reasons, and it is harder to test. Split at every “and”: one method reads, another averages. A main that reads as a short outline of single-job calls is easier to get right. (Source: BJP (Reges and Stepp), Ch 3.)
Go Deeper (optional)
For the curious: deriving the signature before the body is the small-scale version of designing from a specification, which becomes a graded skill later in the sequence. The same move (read the contract, name the inputs and the output, only then write the implementation) scales from a one-line method up to a whole class.
Check Yourself
Close the notes and answer each one from memory, then reveal it. Pulling an idea back from memory is one of the strongest ways to make it stick.
Check your understanding
Task: take two int values and return the larger one. What is the return type?
A task says ‘prints the multiplication table’. What is the return type?
You name a method readAndAverage. What does that signal?
How do you decide a method’s parameters?