Problem overview
Two variables record how long you worked on a project: hours, which is 13, and minutes, which is 47. The task is to print the total time in seconds. The lesson's example is 1 hour and 2 minutes, which is 3720 seconds.
Approach
Seconds are the smallest unit here, so everything has to be converted down to them. The usual route goes through minutes:
- An hour has 60 minutes, so
hours * 60is the number of minutes in the hours. - Adding
minutesgives the total in minutes:hours * 60 + minutes. - A minute has 60 seconds, so multiplying that total by 60 gives seconds.
Checking with the example: 1 hour is 60 minutes, plus 2 is 62 minutes, and 62 times 60 is 3720.
Step by step
- Turn the hours into minutes.
hours * 60is 780. - Add the minutes.
hours * 60 + minutesis 827. Multiplication runs first, so no parentheses are needed yet. - Convert the total to seconds. Multiply by 60 again. Here the parentheses are essential: the sum must be computed before the final multiplication.
- Print the result.
printlntakes the whole expression.
The solution in Kotlin
val hours: Int = 13
val minutes: Int = 47
println((hours * 60 + minutes) * 60)
The program prints 49620, which is 827 minutes times 60. Details worth knowing:
- The parentheses change the answer. Without them,
hours * 60 + minutes * 60would multiply only the minutes by 60 a second time. It converts the hours to minutes but the minutes to seconds, then adds the two, and prints 3600. Both parts must be in the same unit before they are added. - Another correct form.
hours * 3600 + minutes * 60converts each part to seconds separately and then adds them. For these inputs it also prints49620. - The result fits in an
Int. AnIntholds values up to 2147483647, which is about 596,000 hours in seconds. A realistic number of hours never gets near that.
Thinking in units
Unit conversion is mostly bookkeeping. Each factor of 60 moves a quantity one unit down: hours to minutes, minutes to seconds. Multiplying makes the number bigger because the same time is counted in a smaller unit. A good habit is to name the unit of every intermediate value. After hours * 60 you hold minutes, after adding minutes you still hold minutes, and only the last multiplication produces seconds.
The same pattern works for other conversions, such as days to hours (a factor of 24) or kilometres to metres (1000). To go the other way, from seconds back to hours and minutes, integer division and the remainder operator do the work: dividing by 3600 gives whole hours, and the remainder of that division, divided by 60, gives the minutes.
Edge cases and common mistakes
- Mixing units.
hours * 60 + minutes * 60adds minutes to seconds and prints 3600 for these values, not 49620. - Stopping at minutes.
hours * 60 + minutesis 827, the total in minutes. The task asks for seconds, so the final factor of 60 is needed. - Using 24 or 100. A minute and an hour both convert by 60, not by 100.
- Zero values. With
hours = 0andminutes = 0the answer is0, as it should be.
Testing
hours = 1,minutes = 2prints3720.hours = 0,minutes = 1prints60.hours = 2,minutes = 0prints7200.hours = 13,minutes = 47prints49620.
Key takeaways
- Convert every part to the same unit before adding.
- Parentheses make Kotlin finish the sum before the last multiplication.
- Two formulas can be equal:
(hours * 60 + minutes) * 60andhours * 3600 + minutes * 60give the same number. - An
Intholds far more seconds than a normal working time needs.