Lesson 1 of 10
Your first program
- 10 min
- 3 checks
- 3 hints
- Go to task
Goal
The main method, println and a static method
Explanation
In Java everything lives inside a class. The program here is a class called Main, and it starts running from the method public static void main(String[] args), a long line every Java programmer ends up typing more times than they can count.
System.out.println prints text and then moves to a new line, and the + sign joins strings to each other or to numbers, as in "Level " + level. Every statement in Java ends with a semicolon.
Every method also states the type of value it returns before its name, so static int total(int a, int b) takes two integers and returns an integer. Java is a compiled language: the compiler checks all of your code before it runs, and if it finds even one type error, it refuses to run the program and tells you which line the error is on.
Your task
Complete total(a, b) so it returns the sum of the two numbers, then print a single line from main: Total: 12. The number should come from total(5, 7) rather than being typed in by hand. Click “Run and check” to see the result.
Checks
3 checks- total(5, 7) returns 12
- total adds negative numbers too
- "Total: 12" was printed