Chapter 3 · Data

Math and recursion

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 19 · Concept

Math does the arithmetic

In this lesson you use Java's ready-made math, then write methods that call themselves.

The class Math holds static methods, called on the class's name (lesson 6). It needs no import.

callgives
Math.max(3, 8)8, the larger
Math.min(3, 8)3, the smaller
Math.abs(-5)5, the value without its sign
Math.pow(2, 10)1024.0: 2 to the power 10
Math.sqrt(49)7.0, the square root
import java.util.Random;

public class Tools {
    public static void main(String[] args) {
        System.out.print(Math.max(3, 8) + " " + Math.min(3, 8));
        System.out.println(" " + Math.abs(-5));
        System.out.print(Math.pow(2, 10) + " ");
        System.out.println(Math.sqrt(49));
        System.out.println((int) Math.sqrt(50));
        System.out.println(Math.max(2, 7.5));

        Random dice = new Random(42);
        for (int i = 1; i <= 3; i++) {
            int roll = dice.nextInt(6) + 1;
            System.out.println("Roll " + i + ": " + roll);
        }
    }
}
public class Distance {
    // write straight here: it returns a double
    // write steps here: it returns an int

    public static void main(String[] args) {
        double d = straight(2, 3, -4, -5);
        System.out.println("Straight: " + d);
        System.out.println("Steps: " + steps(2, 3, -4, -5));
    }
}
public class Recursion {
    static int factorial(int n) {
        if (n == 0) {
            return 1;
        }
        return n * factorial(n - 1);
    }

    static int fib(int n) {
        if (n < 2) {
            return n;
        }
        return fib(n - 1) + fib(n - 2);
    }

    public static void main(String[] args) {
        System.out.println("5! = " + factorial(5));
        String line = "Fibonacci:";
        for (int n = 0; n <= 10; n++) {
            line += " " + fib(n);
        }
        System.out.println(line);
    }
}
public class Digits {
    static int sumDigits(int n) {
        // the base case first, then a smaller call
        return 0;
    }

    public static void main(String[] args) {
        System.out.print(sumDigits(7) + " ");
        System.out.print(sumDigits(123) + " ");
        System.out.print(sumDigits(2026) + " ");
        System.out.println(sumDigits(99999));
    }
}
public class Forever {
    static int factorial(int n) {
        return n * factorial(n - 1);
    }

    public static void main(String[] args) {
        System.out.println(factorial(5));
    }
}
import java.util.Random;

public class Check {
    // base times itself, exp times, with recursion;
    // power(base, 0) is 1
    static int power(int base, int exp) {
        return 0;
    }

    // one die: a number from 1 to 6
    static int roll(Random dice) {
        return 0;
    }

    // a text of n stars, with recursion;
    // stars(0) is ""
    static String stars(int n) {
        return "";
    }

    public static void main(String[] args) {
        // 1024 = 1024
        System.out.print(power(2, 10) + " = ");
        System.out.println((int) Math.pow(2, 10));
        // 81 = 81
        System.out.print(power(3, 4) + " = ");
        System.out.println((int) Math.pow(3, 4));
        Random dice = new Random(2026);
        // ******, *****, **, *
        for (int i = 0; i < 4; i++) {
            System.out.println(stars(roll(dice)));
        }
    }
}
public class Backwards {
    // text backwards, with recursion;
    // backwards("") is ""
    static String backwards(String text) {
        return text;
    }

    public static void main(String[] args) {
        System.out.println(backwards("stressed"));
        System.out.println(backwards("Java"));
    }
}
Terminal
Step 1 of 7Math and Random

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)