Page 58 - Computer science 868 Class 12
P. 58

31.  Design a class Convert to find the date and the month from a given day number for a particular year. Example: If day number
                    is 64 and the year is 2020, then the corresponding date would be: March 4, 2020 i.e. (31 + 29 + 4 = 64) Some of the members
                    of the class are given below:                                                              [ISC 2020]
                     Classname                                    :   Convert
                     Data Members/Instance variables
                     n                                            :   integer to store the day number
                     d                                            :   integer to store the day of the month (date)
                     m                                            :   integer to store the month
                     y                                            :   integer to store the year
                     Methods/Member functions
                     Convert ()                                   :    constructor to initialise the data members with legal initial values
                     void accept()                                :   to accept the day number and the year
                     void day_to_date()                           :   converts  the  day  number  to  its  corresponding  date  for  a
                                                                    particular year and stores the date in ‘d’ and the month in ‘m’
                     void display()                               :   displays the month name, date and year
                     Specify the class Convert giving details of the constructor(),void accept(), void day_to_date() and void display(). Define a
                    main() function to create an object and call the functions accordingly to enable the task.

                Ans. import java.util.*;
                    class Convert
                    {
                       int n,d,m,y;
                       Convert( )
                       {
                          n=0;
                          y=0;
                       }
                       void accept()
                       {
                          Scanner x=new Scanner(System.in) ;
                          System.out.println("Enter day number and year");
                          n=x.nextInt() ;
                          y=x.nextInt() ;
                       }
                       void day_to_date()
                       {
                          int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
                          if(y%4==0)
                          a[2]=29;
                          int s=0, c=0;
                          while(s<n)
                            s=s+a[c++];
                          s=s-a[--c];
                          d=n-s;
                          m=c;
                       }
                         void display()
                       {
                           String x[]={"","JANUARY","FEBRUARY","MARCH","APRIL","MAY", "JUNE","JULY","AUGUST",
                           "SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
                          System.out.print("\n Day Number: " + n);
                          System.out.print("\n Date: " );
                          System.out.print(x[m]+" " +d + "," + y);
                       }
                       public static void main(String[] args)
                       {
                          Convert obj =new Convert();
                          obj.accept( ) ;


                5656  Touchpad Computer Science-XII
   53   54   55   56   57   58   59   60   61   62   63