The task and the formula
The exercise starts with two int variables, hours set to 13 and minutes set to 47. The goal is to print how many seconds that amount of time adds up to. For 1 hour and 2 minutes the answer is 3720, because 62 minutes times 60 seconds is 3720.
The conversion has two stages, since there are 60 minutes in an hour and 60 seconds in a minute:
- Hours to minutes:
hours * 60, then add the looseminutes. That is the total number of minutes. - Minutes to seconds: multiply that total by 60.
The general formula for converting hours to seconds is hours * 3600, since an hour has 3600 seconds, and one minute is 60 seconds. In one expression for both: (hours * 60 + minutes) * 60. With 13 and 47: 13 * 60 is 780, plus 47 is 827 minutes, and 827 * 60 is 49,620 seconds.
Why the parentheses matter
Java follows the usual precedence rules: multiplication and division run before addition and subtraction, and operators of the same level run left to right. Without parentheses, hours * 60 + minutes * 60 would multiply minutes by 60 but leave hours counted as minutes, giving 780 + 2820 = 3600 instead of 49,620. Writing hours * 60 + minutes inside parentheses forces the addition to happen first, so the final * 60 applies to the whole sum.
An equivalent form skips the parentheses by converting each part separately: hours * 3600 + minutes * 60. Both give 49,620. The first matches the way people think about the problem; the second does fewer operations. Either is a fine answer.
The solution in Java
int hours = 13;
int minutes = 47;
System.out.println((hours * 60 + minutes) * 60);
This prints 49620. The statement gives println a single int expression, so it prints the number followed by a line break.
Printing with a label
To print text with the number, concatenate with +. The trap is that + also joins strings, and the order of evaluation decides which meaning applies:
System.out.println("Seconds: " + (hours * 60 + minutes) * 60);
System.out.println("Seconds: " + hours * 60 + minutes);
The first line prints Seconds: 49620. The second prints Seconds: 78047: the multiplication runs first and gives 780, but then the string is joined with 780 and after that with 47, as text. When a number must be added before it is joined to a string, put it in parentheses.
Keeping the steps in variables
For a longer calculation, store the intermediate results, which also gives each one a name:
int totalMinutes = hours * 60 + minutes;
int totalSeconds = totalMinutes * 60;
System.out.println(totalSeconds);
This prints 49620 as well. Declaring variables costs nothing and makes it clear what each number means, which is useful once formulas get longer.
Common mistakes
- Forgetting one factor of 60.
hours * 60 + minutesis minutes, not seconds. The result 827 is a sign that the last multiplication is missing. - Dropping the parentheses, as shown above, so the final
* 60applies only tominutes. - Mixing up the unit. Multiplying
hoursby 100 orminutesby 100 comes from decimal habits. Time uses 60. - Integer overflow with very large values. An
intholds up to 2,147,483,647. Withhours = 600000,hours * 3600overflows and prints-2134967296. Usinglongfixes it:600000 * 3600Lis2160000000. For the lesson's values (13 and 47)intis plenty. - Fractions of an hour. With
double hours = 1.5, the same formula works and the result is5400.0, because the arithmetic is done on doubles.
Testing
hours = 1,minutes = 2gives3720.hours = 0,minutes = 0gives0.hours = 1,minutes = 0gives3600.hours = 0,minutes = 1gives60.hours = 10,minutes = 59gives39540.hours = 13,minutes = 47gives49620.
