Task 5 of 7 · 0 solved · 7 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

3.4 Methods — building from helpers

Lesson

Breaking a task into small methods

A good way to solve a bigger problem: split it into small, well-named helper methods, then combine them. Each helper does one clear job; the main method (or another method) calls them in order. Passing one method's result straight into another is called composing them.


Worked example

// line() prints "----", title() prints "MENU"
line();
title();
line();

Three tiny helpers, called in order, build a framed heading: ----, MENU, ----.


▶ Try it: the helpers line() and title() are defined for you. Run this, then reorder the calls and re-run.



doubleIt(n) returns n * 2 and addOne(n) returns n + 1. What is addOne(doubleIt(5))?


Type exactly what this program prints.

public class Main {

    static void line() {
        System.out.println("--");
    }

    static void title() {
        System.out.println("HI");
    }

    public static void main(String[] args) {
        line();
        title();
        line();
    }
}

Sort each practice as a good use of helper methods or not.


True or false?