Java Arena
Step 1 of 6

Arithmetic and precedence

Java calculates with the usual operators: + adds, - subtracts, * multiplies and / divides. A calculation can use numbers, variables, or both, and its result can be stored in a variable:

int minutes = 3 * 60;
int total = minutes + 45;
System.out.println(total);
Output
225

Which part first

As in maths, * and / are done before + and -. Parentheses change the order: whatever is inside them is calculated first.

System.out.println(2 + 3 * 4);
System.out.println((2 + 3) * 4);
Output
14
20

A piece of code that has a value, like 2 + 3 * 4 or minutes + 45, is called an expression. A statement such as int total = minutes + 45; calculates the expression and does something with its value, here storing it.

Your task

Challenge 1 of 3

Add parentheses in the two blanks, so the program prints 20.

Challenge 2

Read a number of hours and print how many minutes that is. For the input 3 the program prints:

How many hours?
That is 180 minutes.

Challenge 3

Read a number of weeks and print how many seconds they last: 7 days a week, 24 hours a day, 60 minutes an hour and 60 seconds a minute. For the input 1 the program prints:

Weeks: 1
Seconds: 604800