Chapter 1 · Foundations

Methods and input

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

Step 1 of 20 · Concept

Name a piece of code

You have used two methods already: main, where Java starts, and println. A method is a named piece of code. You call it by its name, and it runs.

In this lesson you will:

  • write methods of your own,
  • give them values and get answers back,
  • read numbers that the user types.
public class Squares {
    static int square(int n) {
        return n * n;
    }

    public static void main(String[] args) {
        for (int i = 1; i <= 4; i++) {
            System.out.println(i + " squared is " + square(i));
        }
    }
}
public class Max {
    // write the method max here

    public static void main(String[] args) {
        System.out.println("max(3, 8) is " + max(3, 8));
        System.out.println("max(10, 2) is " + max(10, 2));
    }
}
public class Banner {
    static String repeat(String s, int n) {
        String result = "";
        // add s to result, n times
        return result;
    }

    public static void main(String[] args) {
        System.out.println(repeat("=", 12));
        System.out.println("| " + repeat("ab", 4) + " |");
        System.out.println(repeat("=", 12));
    }
}
import java.util.Scanner;

public class Sum {
    static int sum(Scanner in) {
        int total = 0;
        // read every number with a while loop
        return total;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Sum: " + sum(in));
    }
}
public class Main {
    public static void main(String[] args) {
        System.out.println(Greeter.greet("Ada"));
        System.out.println(Greeter.greet("Grace"));
    }
}
import java.util.Scanner;

public class Stats {
    // write stats(in): read every number,
    // then print the count and the largest

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        stats(in);
    }
}
import java.util.Scanner;

public class Mean {
    static double mean(Scanner in) {
        // return the average of every number
        return 0;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Mean: " + mean(in));
    }
}
Terminal
Step 1 of 7A method of your own

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)