Add OOP/Java/Lab/Week5/Time.java

This commit is contained in:
Aadit Agrawal 2024-08-31 10:26:09 +05:30
parent 3957b1c51b
commit cb924d0b60

View File

@ -0,0 +1,46 @@
public class Time {
int hr;
int min;
int sec;
Time(){
// initializing to zero
hr = 0;
min = 0;
min = 0;
}
Time (int hour, int minute, int second){
hr = hour;
min = minute;
sec = second;
}
public void display(){
System.out.printf("%02d:%02d:%02d\n", (hr%24), (min%60), (sec%60)); }
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);
this.hr = totalSeconds / 3600;
this.min = (totalSeconds%3600) / 60;
this.sec = (totalSeconds % 60);
}
public static void main(String[] args) {
Time t1 = new Time(13, 30, 0);
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();
}
}