Think java exercise answers

Started by
1 comment, last by RulerOfNothing 11 years, 8 months ago
Does anyone know if there are any answers floating around to the exercises in Think Java: Think like a computer scientist by Allen Downey?

I'm stuck on exercise 2.11 - the 24 hour clock.

Thanks for any replies
Advertisement
Any one care to help me understanding this?
I found what is supposed to be the answer but I can't wrap my head around it. Like where did the 3600 come from on this line
seconds_since_midnight = hour * 3600 + minute * 60 + second;

Thanks for any help
[source lang="java"]public class time { public static void main(String[] args) {
int hour, minute, second, seconds_since_midnight, seconds_remaining, percentage_passed;
hour = 20;
minute = 14;
second = 29;
seconds_since_midnight = hour * 3600 + minute * 60 + second;
System.out.print("Seconds since midnight: ");
System.out.println(seconds_since_midnight);
seconds_remaining = (24 - hour) * 3600 + (60 - minute) * 60 + (60 - second);
System.out.print("Seconds remaining in the day: ");
System.out.println(seconds_remaining);
percentage_passed = seconds_since_midnight * 100 / (3600 * 24);
System.out.print("Percentage of the day that has passed: ");
System.out.println(percentage_passed);

}

}[/source]
An hour is 60 minutes, and a minute is 60 seconds. You get the 3600 seconds to an hour by simple multiplication.

This topic is closed to new replies.

Advertisement