Java Arena
Step 5 of 8

Methods that call methods

Any method can call other methods, not only main. A bigger method can then be built from smaller ones that each do one thing:

public class Main {
    public static void main(String[] args) {
        System.out.println(distance(3, 4));
    }

    public static int square(int number) {
        return number * number;
    }

    public static double distance(int x, int y) {
        return Math.sqrt(square(x) + square(y));
    }
}
Output
5.0

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.

Methods 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.

Your task

Challenge 1 of 3

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:

366

Challenge 2

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:

125.0

Challenge 3

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:

2 squared is 4
3 squared is 9
4 squared is 16