{"id":"methods-in-loops","slug":"calling-in-a-loop","title":"Calling a method from a loop","text":"A method call is a statement like any other, so it can go inside a loop. The method's lines are written once and run every time the loop comes round:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        for (int round = 1; round <= 3; round++) {\n            System.out.println(\"Round \" + round);\n            printDivider();\n        }\n    }\n\n    public static void printDivider() {\n        System.out.println(\"-----\");\n    }\n}\n```\n\n```output\nRound 1\n-----\nRound 2\n-----\nRound 3\n-----\n```\n\nMethods also make a program easier to change. To make the divider longer, you change one line in `printDivider`, and every call prints the new one.\n","task":"Make `main` print the line three times, with a single call to `printLine()` inside a loop. The program prints:\n\n```\n==========\n==========\n==========\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n\n    }\n\n    public static void printLine() {\n        System.out.println(\"==========\");\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        for (int i = 0; i < 3; i++) {\n            printLine();\n        }\n    }\n\n    public static void printLine() {\n        System.out.println(\"==========\");\n    }\n}\n","hints":["A `for` loop that runs three times: `for (int i = 0; i < 3; i++) { ... }`.","The only line inside the loop is the call `printLine();`."],"tests":[{"name":"Output","stdin":"","expect":"==========\n==========\n==========","hidden":false}],"require":[{"pattern":"\\b(for|while)\\s*\\(","message":"Use a loop."},{"pattern":"\\bprintLine\\s*\\(\\s*\\)\\s*;","min":1,"max":1,"message":"Write the call `printLine();` once, inside the loop: the loop repeats it."}],"forbid":[],"more":[{"task":"Read how many cheers the crowd wants, and call `cheer()` that many times. For the input `3` the program prints:\n\n```\nHip hip hooray!\nHip hip hooray!\nHip hip hooray!\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int times = Integer.valueOf(scanner.nextLine());\n\n    }\n\n    public static void cheer() {\n        System.out.println(\"Hip hip hooray!\");\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int times = Integer.valueOf(scanner.nextLine());\n        for (int i = 0; i < times; i++) {\n            cheer();\n        }\n    }\n\n    public static void cheer() {\n        System.out.println(\"Hip hip hooray!\");\n    }\n}\n","hints":["The loop runs `times` times instead of a fixed number.","`for (int i = 0; i < times; i++) { cheer(); }`"],"tests":[{"name":"Test 1","stdin":"3\n","expect":"Hip hip hooray!\nHip hip hooray!\nHip hip hooray!","hidden":false},{"name":"Test 2","stdin":"1\n","expect":"Hip hip hooray!","hidden":true},{"name":"Test 3","stdin":"5\n","expect":"Hip hip hooray!\nHip hip hooray!\nHip hip hooray!\nHip hip hooray!\nHip hip hooray!","hidden":true}],"require":[{"pattern":"\\bcheer\\s*\\(\\s*\\)\\s*;","min":1,"max":1,"message":"Write the call `cheer();` once, inside a loop."}],"forbid":[]},{"task":"Write two methods: `tick()` prints `tick` and `tock()` prints `tock`. Then read a number of rounds, and in each round call `tick()` and then `tock()`. For the input `2` the program prints:\n\n```\ntick\ntock\ntick\ntock\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int rounds = Integer.valueOf(scanner.nextLine());\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int rounds = Integer.valueOf(scanner.nextLine());\n        for (int i = 0; i < rounds; i++) {\n            tick();\n            tock();\n        }\n    }\n\n    public static void tick() {\n        System.out.println(\"tick\");\n    }\n\n    public static void tock() {\n        System.out.println(\"tock\");\n    }\n}\n","hints":["Write the two methods first, each with one print line.","The loop runs `rounds` times, and its body has two calls."],"tests":[{"name":"Test 1","stdin":"2\n","expect":"tick\ntock\ntick\ntock","hidden":false},{"name":"Test 2","stdin":"1\n","expect":"tick\ntock","hidden":true},{"name":"Test 3","stdin":"3\n","expect":"tick\ntock\ntick\ntock\ntick\ntock","hidden":true},{"name":"Test 4","stdin":"","call":"tock();\ntick();\n","expect":"tock\ntick","hidden":true}],"require":[{"pattern":"\\b(for|while)\\s*\\(","message":"Use a loop for the rounds."}],"forbid":[]}]}