forked from client69/Open
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.java
More file actions
60 lines (57 loc) · 1.79 KB
/
Clock.java
File metadata and controls
60 lines (57 loc) · 1.79 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
//An algorithm which takes in time in 24 hour format and displays it in 12hour format(AM/PM)
//for example: 15hr 125mins 69secs will be calculated and displayed as 5:6:9 PM
import java.util.*;
class Clock {
int hours , minutes, seconds;
//parametrised constructor
Clock(int h,int m,int s){
hours=h;
minutes= m;
seconds=s;
}
void time(){
//method to display time in 12hour format
String amPm = "AM", time;
if (hours >= 12){
if (hours>12){
hours -= 12;
}
amPm = "PM";
}
time="Time: "+hours+":"+minutes+":"+seconds+" "+amPm;
System.out.println(time);
}
public static void main(String args[]){
int hours, minutes, seconds;
//Scanner class object
Scanner sc = new Scanner(System.in);
//user input
System.out.println("Enter the hour for the time : ");
hours = sc.nextInt();
System.out.println("Enter the minutes for the time : ");
minutes = sc.nextInt();
System.out.println("Enter the seconds for the time : ");
seconds = sc.nextInt();
sc.close();
//validation of the time input
if (hours>=0 && hours <=24 && minutes >=0 && seconds >=0){
if (minutes>=60){
hours += (int)minutes/60;
minutes -= 60*((int)minutes/60);
}
if (seconds >=60){
minutes += (int)seconds/60;
seconds -= 60*((int)seconds/60);
}
if (hours>=24){
hours -= 24;
}
Clock ob = new Clock(hours,minutes,seconds);
ob.time();
}
else{
System.out.println("Wrong Input.Try Again! Enter correct time format.");
main(args);
}
}
}