20-CS-122-001 Computer Science II Spring 2012
A Date Class

Virtual functions, classes, inheritance, lists, queues, stacks, applications

   
//  A Date Class

class Date {
    int month, day, year;
    static String name[] = { "nothing", "January", "February", "March",
          "April", "May", "June", "July", "August", "September", "October",
          "November", "December" };
    static int length[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

    public Date () {                   // Constructor
       month = day = year = 1;
       System.out.println("Creating " + this + " (" + display() + ")");
    }
   
    Date ( int mn, int dy, int yr) {   // Constructor
       month = Math.max(1, mn);
       month = Math.min(month,12);

       day = Math.max(1,dy);
       day = Math.min(day, length[month]);

       year = Math.max(1, yr);
       System.out.println("Creating " + this + " (" + display() + ")");
    }
      
    String display () {                 // Method to display date
       return  name[month] + " " + day + ", " + year;
    }
   
    int getMonth () {  return month; }  // Get month
   
    void setMonth (int mn) {            // Set month
       month = Math.max(1, mn);
       month = Math.min(month, 12);
    }
   
    int daysSoFar () {                  // Number of days since first of year
       int total = 0;

       for (int i=1 ; i < month ; i++) total += length[i];
       total += day;
       return total;
    }
};

public class date {
    public static void main(String args[]) {
       System.out.print("Declare date1 01/02/1999:\t");
       Date date1 = new Date (1,  2, 1999);
       System.out.print("Declare date2 04/12/1999:\t");
       Date date2 = new Date (4, 12, 1999);
       System.out.print("Declare date3 01/01/0001:\t");
       Date date3 = new Date (1,  1,    1);
       System.out.print("Declare date4  (no date):\n");
       Date date4;
       Date date5;  // (no concept of *date5)
       Date date6;  // (no concept of *date6)

       System.out.print("Create  date5 45/45/4455:\t");
       date5 = new Date(45, 45, 4455);

       System.out.print("Show date1:\t");
       System.out.println(date1.display() + " (" + date1.daysSoFar() + ")");
       System.out.print("Show date2:\t");
       System.out.println(date2.display() + " (" + date2.daysSoFar() + ")");
       System.out.print("Show date5:\t");
       System.out.println(date5.display() + " (" + date5.daysSoFar() + ")");

       System.out.println("Show Month of date5:\t" + date5.getMonth());
       System.out.println("Do date5.setMonth(4):");
       date5.setMonth(4);
       System.out.println("Show Month of date5:\t" + date5.getMonth());

       System.out.print("Delete date5:\t");
       // (no concept of delete(date5));
       System.out.println("(Delete and) create  date5 01/01/0001:\t");
       date5 = new Date(1,1,1);

       System.out.println("Show Month of date5:\t" + date5.getMonth());
       System.out.println("Do date5.setMonth(4):");
       date5.setMonth(4);
       System.out.println("Show Month of date5:\t" + date5.getMonth());

       System.out.print("Show date5:\t");
       System.out.println(date5.display() + " (" + date5.daysSoFar() + ")");
       //System.out.print("Show date4:\t");
       //System.out.println(date4.display() + " (" + date4.daysSoFar() + ")");

       System.out.println("Do date3.setMonth(3):\n");
       date3.setMonth(3);
    }
}

// Sample Output:
//   Declare date1 01/02/1999:       Creating Date@80ce246 (January 2, 1999)
//   Declare date2 04/12/1999:       Creating Date@80ce26c (April 12, 1999)
//   Declare date3 01/01/0001:       Creating Date@80ce253 (January 1, 1)
//   Declare date4  (no date):
//   Create  date5 45/45/4455:       Creating Date@80ce28c (December 31, 4455)
//   Show date1:     January 2, 1999 (2)
//   Show date2:     April 12, 1999 (102)
//   Show date5:     December 31, 4455 (365)
//   Show Month of date5:    12
//   Do date5.setMonth(4):
//   Show Month of date5:    4
//   Delete date5:   (Delete and) create  date5 01/01/0001:
//   Creating Date@80ce31c (January 1, 1)
//   Show Month of date5:    1
//   Do date5.setMonth(4):
//   Show Month of date5:    4
//   Show date5:     April 1, 1 (91)
//   Do date3.setMonth(3):