-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.java
131 lines (106 loc) · 2.65 KB
/
time.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.util.Scanner;
class Time2{
int hr,min,sec;
public Time2()
{
// Default
}
public Time2(int hr , int min , int sec)
{
this.hr = hr;
this.min = min;
this.sec = sec;
}
public void sethr(int hr)
{
this.hr = hr;
}
public int gethr()
{
return hr;
}
public int getmin()
{
return min;
}
public void setmin(int min)
{
this.min = min;
}
public void setsec(int sec)
{
this.sec = sec;
}
public int getsec()
{
return sec;
}
void get(){
gethr();
getmin();
getsec();
}
void display ()
{
if ((hr < 12 && hr>=0) || hr > 23 ) {
System.out.println("\nTime is : " +hr+ " hours "+ " : " +min+ " minutes "+ " : " +sec+ " seconds " + " AM\n");
}
else{
System.out.println("\nTime is : " +hr+ " hours "+ " : " +min+ " minutes "+ " : " +sec+ " seconds " + " PM\n");
}
}
public void difer(Time2 T)
{
double h ;
if (this.gethr() < T.gethr()) {
h = (T.gethr() - this.gethr());
}
else
{
h = this.gethr() - T.gethr();
}
double m;
if (this.getmin() < T.getmin()) {
m = (T.getmin() - this.getmin());
}
else
{
m = this.getmin() - T.getmin();
}
double s;
if (this.getsec() < T.getsec()) {
s = (T.getsec() - this.getsec());
}
else
{
s = this.getsec() - T.getsec();
}
System.out.println("Time difference is : " +h + " Hours " + " : " +m+ " Minutes " + " : " + s + " Seconds ");
}
}
public class time
{
public static void main (String []args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter hrs, min and secs : ");
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int z1 = sc.nextInt();
Time2 t2 = new Time2();
t2.sethr(x1);
t2.setmin(y1);
t2.setsec(z1);
t2.display();
System.out.println("Enter hrs, min and secs : ");
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int z2 = sc.nextInt();
Time2 t3 = new Time2();
t3.sethr(x2);
t3.setmin(y2);
t3.setsec(z2);
t3.display();
t2.difer(t3);
sc.close();
}
}