forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.java
More file actions
40 lines (31 loc) · 1.29 KB
/
Copy pathTime.java
File metadata and controls
40 lines (31 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Time {
public static void main(String[] args) {
// initialize vars and assign values
int hour, minute, second;
hour = 17; //24 hour time
minute = 58;
second = 30;
//calculate and display number of seconds since midnight
int minuteTotal = minute + hour * 60;
int secondTotal = second + minuteTotal * 60;
System.out.println("It has been " + secondTotal + " seconds since midnight.");
// calculate and display seconds remaining in the day
int secondsInDay = 86400;
int remainingSeconds = secondsInDay - secondTotal;
System.out.println("There are " + remainingSeconds + " seconds left in the day.");
//calculate and display percentage of day passed
double secondsInDayFloat = secondsInDay * 1.0; //needed for floating point division
double percentage = 100 * (secondTotal / secondsInDayFloat);
System.out.println(percentage + "% of the day has passed");
// calculate and display the elapsed time since working on this program
int hourNow, minuteNow, secondNow;
hourNow = 18;
minuteNow = 24;
secondNow = 45;
int minuteNowTotal = minuteNow + hourNow * 60;
int secondNowTotal = secondNow + minuteNowTotal * 60;
int secondDiff = secondNowTotal - secondTotal;
System.out.println("It has been " + secondDiff +
" seconds since you started working");
}
}