Page 582 - Computer science 868 Class 12
P. 582

Program 6      Difference of two Time objects
                               Write a program to define a class Time and find the difference between two Time objects.
                               Class name                           :  Time
                               Data Members
                               int hh,mm                            :  To denote hour and minute

                               Data Members
                               Time()                               :  constructor to initialise hour and minute to 0
                               void readtime()                      :  to input hour and minute
                               void displaytime()                   :  to display time in hour and minute
                               int time to minute(time ob)          :   to convert the time object to minute and return
                                                                      the result
                               void minute to time(int n)           :  to convert time in second to hour and minute
                               void diff(time endtime, time starttime)   :   using this method given above find the difference
                                                                      between the 2 time object
                               Also write the main method to create and object and display the result.


                 1       import java.util.*;

                 2       class Time
                 3       {

                 4       int hh,mm;
                 5       Time()

                 6       {
                 7       hh=mm=0;
                 8       }

                 9       void readtime()  // accept hour and minute

                10       {
                11       Scanner sc = new Scanner(System.in);
                12       System.out.println("Enter value in hh && mm");

                13       hh = sc.nextInt();
                14       mm = sc.nextInt();

                15       }
                16       void displaytime()  // display time in hour and minute

                17       {
                18       System.out.println(hh + ":" + mm);

                19       }
                20       int timetominutes(Time ob)  // convert time object to minute

                21       {




                580580  Touchpad Computer Science-XII
   577   578   579   580   581   582   583   584   585   586   587