forked from khushal87/Data-structures-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.java
More file actions
78 lines (67 loc) · 1.93 KB
/
Time.java
File metadata and controls
78 lines (67 loc) · 1.93 KB
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
/*
Time program to add two times in hours, minutes and seconds.
Use concept of:
1) Object as an argument,
2) Object as a return type.
*/
// Created by akashbhalotia
import java.util.Scanner;
class Time
{
private int hour,min,sec;
public Time(){} //default constructor
public Time(int hour, int min, int sec)//parameterised constructor
{
this.hour=hour;
this.min=min;
this.sec=sec;
}
public Time add(Time a, Time b) //Object as an argument
{
Time tmp=new Time();
tmp.sec=a.sec+b.sec;
tmp.min=a.min+b.min;
tmp.hour=a.hour+b.hour;
tmp.min+=tmp.sec/60;
tmp.sec%=60;
tmp.hour+=tmp.min/60;
tmp.min%=60;
return tmp; //return resulting object
}
public void display() //to display data
{
System.out.println("The total time is: ");
System.out.println("Hours = "+hour);
System.out.println("Minutes = "+min);
System.out.println("Seconds = "+sec);
}
}
class Solver
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int hour,min,sec;
System.out.println("Entering time 1...");
System.out.println("Enter number of hours");
hour=sc.nextInt();
System.out.println("Enter number of minutes");
min=sc.nextInt();
System.out.println("Enter number of seconds");
sec=sc.nextInt();
System.out.println();
Time t1=new Time(hour,min,sec);
System.out.println("Entering time 2...");
System.out.println("Enter number of hours");
hour=sc.nextInt();
System.out.println("Enter number of minutes");
min=sc.nextInt();
System.out.println("Enter number of seconds");
sec=sc.nextInt();
System.out.println();
Time t2=new Time(hour,min,sec);
Time t3=new Time();
t3=t3.add(t1,t2);
t3.display();
}
}