forked from anishrauniyar/EasySolutionMUM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputeHMS.java
More file actions
65 lines (55 loc) · 1.5 KB
/
computeHMS.java
File metadata and controls
65 lines (55 loc) · 1.5 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
60
61
62
63
64
65
import java.util.Scanner;
/*
* Write a method named computeHMS that computes the number of hours, minutes and seconds
* in a given number of seconds.
* Example: 3735 returns {1, 2, 15}
* because 3735 = 1*3600 + 2*60 + 15.
* In other words, 3,735 is the number of seconds in 1 hour 2 minutes and 15 seconds
*/
public class computeHMS {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the second: ");
int second = sc.nextInt();
if (second < 0) {
System.out.println("The seconds can not be negative.");
} else {
int[] hms = computesHMS(second);
System.out.println("Hour: " + hms[0] + " Minute: " + hms[1] + " Second: " + hms[2]);
}
sc.close();
}
private static int[] computesHMS(int second) {
// TODO Auto-generated method stub
int hour = 0;
int minute = 0;
int sec = 0;
if (second >= 3600) {
hour = second / 3600;
second = second - hour * 3600;
if (second >= 60) {
minute = second / 60;
second = second - minute * 60;
if (second < 60) {
sec = second;
}
} else if (second < 60) {
minute = 0;
sec = second;
}
} else if (second < 3600 && second >= 60) {
hour = 0;
minute = second / 60;
second = second - minute * 60;
if (second < 60) {
sec = second;
}
} else if (second < 60) {
hour = 0;
minute = 0;
sec = second;
}
int[] hms = new int[]{hour, minute,sec};
return hms;
}
}