{"id":"return-values-calling-methods","slug":"methods-calling-methods","title":"Methods that call methods","text":"Any method can call other methods, not only `main`. A bigger method can then be built from smaller ones that each do one thing:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        System.out.println(distance(3, 4));\n    }\n\n    public static int square(int number) {\n        return number * number;\n    }\n\n    public static double distance(int x, int y) {\n        return Math.sqrt(square(x) + square(y));\n    }\n}\n```\n\n```output\n5.0\n```\n\n`distance` calls `square` twice and uses what it returns. Each small method can be checked on its own, and once `square` works, every method that uses it can rely on it.\n\nMethods that print can call each other too. A method that prints a table can call a method that prints one row, once for each row. The row method then only has to know how to print a single row.\n","task":"Write `daysInYear(int year)`, which returns 366 for a leap year and 365 otherwise. Use the `isLeapYear` method that's already there (it follows the leap year rules from module 6). The check runs `System.out.println(daysInYear(2024));` and expects:\n\n```\n366\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(daysInYear(2024));\n    }\n\n    public static boolean isLeapYear(int year) {\n        if (year % 400 == 0) {\n            return true;\n        }\n        if (year % 100 == 0) {\n            return false;\n        }\n        return year % 4 == 0;\n    }\n\n    // Write the daysInYear method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(daysInYear(2024));\n    }\n\n    public static boolean isLeapYear(int year) {\n        if (year % 400 == 0) {\n            return true;\n        }\n        if (year % 100 == 0) {\n            return false;\n        }\n        return year % 4 == 0;\n    }\n\n    public static int daysInYear(int year) {\n        if (isLeapYear(year)) {\n            return 366;\n        }\n        return 365;\n    }\n}\n","hints":["`isLeapYear(year)` gives `true` or `false`, so it can be the condition of an `if`.","`if (isLeapYear(year)) { return 366; }` and then `return 365;`"],"tests":[{"name":"System.out.println(daysInYear(2024));","stdin":"","call":"System.out.println(daysInYear(2024));\n","expect":"366","hidden":false},{"name":"System.out.println(daysInYear(1900));","stdin":"","call":"System.out.println(daysInYear(1900));\n","expect":"365","hidden":true},{"name":"System.out.println(daysInYear(2000));","stdin":"","call":"System.out.println(daysInYear(2000));\n","expect":"366","hidden":true},{"name":"System.out.println(daysInYear(2023));","stdin":"","call":"System.out.println(daysInYear(2023));\n","expect":"365","hidden":true}],"require":[{"pattern":"\\bisLeapYear\\s*\\(","min":2,"message":"Use `isLeapYear` inside `daysInYear`."}],"forbid":[],"more":[{"task":"Write `priceWithTax(double price)`, which returns the price plus its tax. Use the `tax` method that's already there. The check runs `System.out.println(priceWithTax(100));` and expects:\n\n```\n125.0\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(priceWithTax(100));\n    }\n\n    public static double tax(double price) {\n        return price * 0.25;\n    }\n\n    // Write the priceWithTax method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(priceWithTax(100));\n    }\n\n    public static double tax(double price) {\n        return price * 0.25;\n    }\n\n    public static double priceWithTax(double price) {\n        return price + tax(price);\n    }\n}\n","hints":["`tax(price)` returns the tax on the price.","`return price + tax(price);`"],"tests":[{"name":"System.out.println(priceWithTax(100));","stdin":"","call":"System.out.println(priceWithTax(100));\n","expect":"125.0","hidden":false},{"name":"System.out.println(priceWithTax(8));","stdin":"","call":"System.out.println(priceWithTax(8));\n","expect":"10.0","hidden":true},{"name":"System.out.println(priceWithTax(0.4));","stdin":"","call":"System.out.println(priceWithTax(0.4));\n","expect":"0.5","hidden":true}],"require":[{"pattern":"\\btax\\s*\\(","min":2,"message":"Use the `tax` method inside `priceWithTax`."}],"forbid":[]},{"task":"Write `printTable(int from, int to)`, which prints the rows from `from` to `to` by calling the `printRow` method that's already there. The check runs `printTable(2, 4);` and expects:\n\n```\n2 squared is 4\n3 squared is 9\n4 squared is 16\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        printTable(2, 4);\n    }\n\n    public static void printRow(int number) {\n        System.out.println(number + \" squared is \" + number * number);\n    }\n\n    // Write the printTable method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        printTable(2, 4);\n    }\n\n    public static void printRow(int number) {\n        System.out.println(number + \" squared is \" + number * number);\n    }\n\n    public static void printTable(int from, int to) {\n        for (int i = from; i <= to; i++) {\n            printRow(i);\n        }\n    }\n}\n","hints":["`printTable` is `void`: it prints, and returns nothing.","A loop from `from` to `to` that calls `printRow(i);`"],"tests":[{"name":"printTable(2, 4);","stdin":"","call":"printTable(2, 4);\n","expect":"2 squared is 4\n3 squared is 9\n4 squared is 16","hidden":false},{"name":"printTable(5, 5);","stdin":"","call":"printTable(5, 5);\n","expect":"5 squared is 25","hidden":true},{"name":"printTable(1, 3);","stdin":"","call":"printTable(1, 3);\n","expect":"1 squared is 1\n2 squared is 4\n3 squared is 9","hidden":true}],"require":[{"pattern":"\\bprintRow\\s*\\(","min":2,"message":"Print each row by calling `printRow`."}],"forbid":[]}]}