-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTimeCalculator.java
More file actions
59 lines (48 loc) · 1.81 KB
/
Copy pathTimeCalculator.java
File metadata and controls
59 lines (48 loc) · 1.81 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import javax.swing.JOptionPane;
/**@author Lemuel M. Uhuru
* @Date June 9, 2013
* Book Title: From Control Structures through Objects by Tony Gaddis
* Page: 189 Problem 6: TimeCalculator
*
* @Description Write a program that asks the user to enter
* a number of seconds. Design a decision structure to return
* the number of minutes, hours, and days, contingent upon the
* amount of seconds entered. Minutes if the number of seconds
* is >= to 60, hours if >= 3600, or days if
* >= 86,400 seconds.
*/
public class TimeCalculator {
public static void main(String[] args) {
// Declare number constants to hold time values in seconds
int MIN = 60;
int HOUR = 3600;
int DAY = 86400;
// Declare value to hold user input
String input;
// Declare int variable to store amount in seconds
int seconds;
// Prompt user to enter a number of seconds and
// store its value in the variable 'input'
input = JOptionPane.showInputDialog(null, "Enter a number of seconds");
// Convert user input into data-type int
seconds = Integer.parseInt(input);
// Design decision structure to display number of
// minutes, hours, or days based on the number of
// seconds chosen by the user.
if (seconds < DAY){
if (seconds >= HOUR){
JOptionPane.showMessageDialog(null, (seconds/HOUR) + " hours " + ((seconds%HOUR)/MIN) +
" minutes and " + ((seconds%HOUR)%MIN) + " seconds.");
}
else if(seconds >= MIN){
JOptionPane.showMessageDialog(null, seconds + " seconds is equal to " +
seconds/MIN + " minutes and " + seconds%MIN + " seconds.");
}
}
else{
JOptionPane.showMessageDialog(null, (seconds/DAY) + " days " + (seconds%DAY)/HOUR + " hours " +
((seconds%DAY)%HOUR)/MIN + " minutes " + ((seconds%DAY)%HOUR)%MIN +
" seconds.");
}
}
}