{"id":"methods-define-and-call","slug":"defining-methods","title":"Defining and calling a method","text":"So far every program has been one long `main` method. As programs grow, that gets hard to read. A **method** is a named piece of code: you write it once and run it by its name, as often as you like.\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        System.out.println(\"Let's begin.\");\n        greet();\n        greet();\n        System.out.println(\"Done.\");\n    }\n\n    public static void greet() {\n        System.out.println(\"Hello from a method!\");\n    }\n}\n```\n\n```output\nLet's begin.\nHello from a method!\nHello from a method!\nDone.\n```\n\n- `public static void greet() { ... }` **defines** the method. It sits inside the class, next to `main`, never inside another method.\n- `greet();` **calls** it: Java runs the method's lines, then carries on after the call.\n- Every method in these lessons starts with `public static`; the modules on objects explain what that means. `void` means the method gives nothing back. Module 11 is about methods that do.\n- Method names follow the same style as variable names: a small first letter, and a capital for each new word, such as `printMenu` or `showScore`.\n\nThe order of the methods in the class doesn't matter: Java always starts at `main`.\n","task":"Fill in the blanks, so `main` calls `printBanner` before and after the welcome. The program prints:\n\n```\n**********\nWelcome!\n**********\n```\n","kind":"fill","seed":"public class Main {\n    public static void main(String[] args) {\n        [[printBanner]]();\n        System.out.println(\"Welcome!\");\n        [[printBanner]]();\n    }\n\n    public static void printBanner() {\n        System.out.println(\"**********\");\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        printBanner();\n        System.out.println(\"Welcome!\");\n        printBanner();\n    }\n\n    public static void printBanner() {\n        System.out.println(\"**********\");\n    }\n}\n","hints":["A call is the method's name followed by `()`.","Both blanks are `printBanner`: the `();` is already there."],"tests":[{"name":"Output","stdin":"","expect":"**********\nWelcome!\n**********","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Write a method `printRules()` that prints two lines, `No running` and then `No diving`. The check calls your method:\n\n```\nNo running\nNo diving\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        printRules();\n    }\n\n    // Write the printRules method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        printRules();\n    }\n\n    public static void printRules() {\n        System.out.println(\"No running\");\n        System.out.println(\"No diving\");\n    }\n}\n","hints":["The method goes inside the class but outside `main`: after `main`'s closing `}`.","Start it with `public static void printRules() {` and put the two print lines inside."],"tests":[{"name":"printRules();","stdin":"","call":"printRules();\n","expect":"No running\nNo diving","hidden":false},{"name":"Test 2","stdin":"","call":"printRules();\nprintRules();\n","expect":"No running\nNo diving\nNo running\nNo diving","hidden":true}],"require":[],"forbid":[]},{"task":"Write two methods: `morning()` prints `Good morning` and `evening()` prints `Good evening`. Then make `main` call them, so the program prints:\n\n```\nGood morning\nGood evening\nGood morning\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        morning();\n        evening();\n        morning();\n    }\n\n    public static void morning() {\n        System.out.println(\"Good morning\");\n    }\n\n    public static void evening() {\n        System.out.println(\"Good evening\");\n    }\n}\n","hints":["Each method has one print line. `main` then has three calls and no print lines of its own.","`main` calls `morning();`, then `evening();`, then `morning();` again."],"tests":[{"name":"The program","stdin":"","expect":"Good morning\nGood evening\nGood morning","hidden":false},{"name":"evening();","stdin":"","call":"evening();\n","expect":"Good evening","hidden":true},{"name":"morning();","stdin":"","call":"morning();\n","expect":"Good morning","hidden":true}],"require":[{"pattern":"\\bmorning\\s*\\(\\s*\\)\\s*;","min":2,"message":"Let `main` call `morning()` for both of its lines."},{"pattern":"\\bevening\\s*\\(\\s*\\)\\s*;","message":"Let `main` call `evening()`."}],"forbid":[]}]}