Upload files to "OOP/Java/Lab/Week5"

This commit is contained in:
Aadit Agrawal 2024-09-02 00:08:48 +05:30
parent ba0ee79bc1
commit f7d1aa3358
4 changed files with 160 additions and 11 deletions

View file

@ -1,33 +1,46 @@
public class Time {
int hr;
int min;
int sec;
Time(){
Time() {
// initializing to zero
hr = 0;
min = 0;
min = 0;
}
Time (int hour, int minute, int second){
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 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);
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.min = (totalSeconds % 3600) / 60;
this.sec = (totalSeconds % 60);
}
public static void main(String[] args) {
Time t1 = new Time(13, 30, 0);
Time t1 = new Time(13, 30, 0);
Time t2 = new Time(05, 45, 00);
System.out.print("Time T1 is: ");
@ -40,7 +53,5 @@ public class Time {
sum.add(t1, t2);
System.out.print("The sum is: ");
sum.display();
}
}