home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap02 / App1_2 / App1_2.java < prev    next >
Encoding:
Java Source  |  1996-06-19  |  1.8 KB  |  60 lines

  1. // this statement includes the java I/O libraries
  2. // (more on this later)
  3. import java.io.*;
  4.  
  5. // this program calculates the number of days from a given
  6. // date to the beginning of the year
  7. class App1_2
  8. {
  9.     // execution starts here
  10.     public static void main(String args[])
  11.     {
  12.         // read in the month, day and year
  13.         int nMonth = Integer.parseInt(args[0]);
  14.         int nDay   = Integer.parseInt(args[1]);
  15.         int nYear  = Integer.parseInt(args[2]);
  16.  
  17.         // initialize accumulator to zero
  18.         int nDayInYear = 0;
  19.  
  20.         // now loop through the months, adding
  21.         // in the number of days in each month
  22.         for (int nM = 1; nM < nMonth; nM++)
  23.         {
  24.             switch(nM)
  25.             {
  26.                 // the following months have 30 days
  27.                 case 4: case  6: case  9: case 11:
  28.                     nDayInYear += 30;
  29.                     break;
  30.               
  31.                 // in February, gotta' consider leap year
  32.                 case 2:
  33.                     nDayInYear += 28;
  34.  
  35.                     // if it's leap year...
  36.                     if (((nYear % 4) == 0) && ((nYear % 100) != 0))
  37.                     {
  38.                         // ...kick in an extra day
  39.                         nDayInYear++;
  40.                     }
  41.                     break;
  42.  
  43.                 // all the rest have 31 days
  44.                 default:
  45.                     nDayInYear += 31;
  46.             }
  47.         }
  48.  
  49.         // now add in the day of the current month
  50.         nDayInYear += nDay;
  51.  
  52.         // print out the result
  53.         // print out the results
  54.         System.out.print  (nMonth + "-" + nDay + "-" + nYear);
  55.         System.out.println(" is day number "
  56.                            + nDayInYear
  57.                            + " in the year");
  58.         }
  59. }
  60.