MIT-Curricular/OOP/Java/Lab/Week5/Time.java

58 lines
1.2 KiB
Java
Raw Normal View History

2024-08-31 10:26:09 +05:30
public class Time {
2024-09-02 00:08:48 +05:30
2024-08-31 10:26:09 +05:30
int hr;
int min;
int sec;
2024-09-02 00:08:48 +05:30
Time() {
2024-08-31 10:26:09 +05:30
// initializing to zero
hr = 0;
min = 0;
min = 0;
}
2024-09-02 00:08:48 +05:30
Time(int hour, int minute, int second) {
2024-08-31 10:26:09 +05:30
hr = hour;
min = minute;
sec = second;
}
2024-09-02 00:08:48 +05:30
public void display() {
System.out.printf(
"%02d:%02d:%02d\n",
(hr % 24),
(min % 60),
(sec % 60)
);
}
2024-08-31 10:26:09 +05:30
2024-09-02 00:08:48 +05:30
public void add(Time t1, Time t2) {
int totalSeconds =
(t1.hr * 3600) +
(t2.hr * 3600) +
(t1.min * 60) +
(t2.min * 60) +
(t1.sec) +
(t2.sec);
2024-08-31 10:26:09 +05:30
this.hr = totalSeconds / 3600;
2024-09-02 00:08:48 +05:30
this.min = (totalSeconds % 3600) / 60;
2024-08-31 10:26:09 +05:30
this.sec = (totalSeconds % 60);
}
public static void main(String[] args) {
2024-09-02 00:08:48 +05:30
Time t1 = new Time(13, 30, 0);
2024-08-31 10:26:09 +05:30
Time t2 = new Time(05, 45, 00);
System.out.print("Time T1 is: ");
t1.display();
System.out.print("Time T2 is: ");
t2.display();
Time sum = new Time();
sum.add(t1, t2);
System.out.print("The sum is: ");
sum.display();
}
}