From cb924d0b603d0a35a4af56bdc70efb5c6959323e Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Sat, 31 Aug 2024 10:26:09 +0530 Subject: [PATCH] Add OOP/Java/Lab/Week5/Time.java --- OOP/Java/Lab/Week5/Time.java | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 OOP/Java/Lab/Week5/Time.java diff --git a/OOP/Java/Lab/Week5/Time.java b/OOP/Java/Lab/Week5/Time.java new file mode 100644 index 0000000..5f6a7bf --- /dev/null +++ b/OOP/Java/Lab/Week5/Time.java @@ -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(); + + } + +}