Page 277 - Computer science 868 Class 12
P. 277

4. A class Adder has been defined to add any two accepted time.                              [ISC 2017]
                       Example:
                       Time A – 6 hours 35 minutes
                       Time B – 7 hours 45 minutes
                       Their sum is – 14 hours 20 minutes (where 60 minutes = 1 hour)
                       The details of the members of the class are given below:
                       Class name                                   :   Adder
                       Data Member/Instance variable
                       a[ ]                                         :   integer array to hold two elements (hours and minutes)
                       Member Functions/Methods
                       Adder ()                                     :   constructor to assign 0 to the array elements
                       void readtime ()                             :   to enter the elements of the array
                       void addtime (Adder X, Adder Y)              :    adds  the  time  of  the  two  parameterized  objects  X  and  Y  and
                                                                      stores the sum in the current calling object
                       void disptime()                              :   displays the array elements with an appropriate message (i.e.,
                                                                      hours= and minutes=)
                       Specify the class Adder giving details of the constructor( ), void readtime( ), void addtime(Adder, Adder) and void disptime().
                      Define the main() function to create objects and call the functions accordingly to enable the task.
                   Ans.  import java.io.*;
                       class Adder {
                       int a[];
                       Adder() {
                       a = new int[2];
                       }
                       void readtime() throws IOException {
                       InputStreamReader x = new InputStreamReader(System.in);
                       BufferedReader y = new BufferedReader(x);
                       System.out.println("Time");
                       System.out.println("Enter hour:");
                       a[0] = Integer.parseInt(y.readLine());
                       System.out.println("Enter minute :");
                       a[1] = Integer.parseInt(y.readLine());
                       }
                       void addtime(Adder X, Adder Y) {
                       int hour1 = X.a[0];
                       int min1 = X.a[1];
                       int hour2 = Y.a[0];
                       int min2 = Y.a[1];
                       int hourSum = hour1 + hour2;
                       int minSum = min1 + min2;
                       a[0] = hourSum + (minSum/60);
                       a[1] = minSum%60;
                       }
                       void disptime() {
                       System.out.println("Their sum is-");
                       System.out.println("hours = " + a[0] +" minutes = " + a[1]);
                       }
                       public static void main(String args[ ]) throws IOException {
                       Adder obj1 = new Adder();
                       Adder obj2 = new Adder();
                       Adder sumObj = new Adder();
                       obj1.readtime();
                       obj2.readtime();
                       sumObj.addtime(obj1, obj2);
                       sumObj.disptime();
                       }

                                                                                                                       275
                                                                                                            Methods    275
   272   273   274   275   276   277   278   279   280   281   282