← Skill tree CS Skill Tree 0 CSCD210

Turning an English Task into a Method

Textbook: BJP (Reges and Stepp)

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 int values 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.

  1. What does the return type in a method header promise? The type of value the method hands back to its caller.
  2. 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:

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:

  1. Identify the return type. Find the noun for what the method produces. “Returns the larger value” gives int; “prints the table” gives void; “returns true if prime” gives boolean.
  2. Choose a name. Use a verb phrase for void methods and a verb or noun phrase for typed ones. If the name contains “and,” two methods have been fused; split them.
  3. List the parameters. Ask what data the body needs that the caller must supply. Each piece becomes one Type name parameter.
  4. List the constructs the body will need: a conditional, a loop, an accumulator. A comment is fine at first.
  5. Write the body, then add the return on every branch that needs one.
  6. 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.”

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?

Tier 1 · BJP (Reges and Stepp), Ch 3

A task says ‘prints the multiplication table’. What is the return type?

Tier 1 · BJP (Reges and Stepp), Ch 3

You name a method readAndAverage. What does that signal?

Tier 2 · BJP (Reges and Stepp), Ch 3

How do you decide a method’s parameters?

Tier 2 · BJP (Reges and Stepp), Ch 3