Programming Assignment 2 - Date Class

Due March 4, 2010

Overview

For this project you will implement a Date class similar to the date class found in the C++ Standard Library. Your date must be able to represent any valid date from January 1, 1900 into the future accepting only valid input (if invalid, set to default Jan 1, 1900). For example, February 29th is a valid date for any leap year. Leap years are years that are divisible by 4, with one exception. If the year is divisible by 100 and not by 400 (like 2100), then it is not a leap year. (e.g. 2004 was, 2008 was, 1900 was not, 2100 will not be, 2400 will be, and so on).

Instructions

  1. Write a file Date.h that defines the Date class. It must have 3 private integers to hold the month, day, and year. It also needs the following functions:
    1. Date()
      Default constructor which initializes the date to January 1, 1900.
    2. Date(int m, int d, int y)
      Constructor which takes in values for day, month, and year.
    3. ~Date()
      Default destructor. Does it need to do anything?
    4. int getDay() const, int getMonth() const, int getYear() const
      Accessor functions for each of the data members.
    5. bool setDay(int d), bool setMonth(int m), bool setYear(int y)
      Mutator functions for each of the data members. These should return false for invalid inputs.
    6. bool setDate(int m, int d, int y)
      Sets the date object if provided with valid date values, otherwise it leaves the date object alone and returns false.
    7. bool equals(Date const & date) const
      Return true if the current object and the parameter object represent the same date.
    8. void displayDate() const
      Output function which displays the date to standard out. Output it in your choice of standard date representations (e.g. 09/06/2006, 06/09/2006, September 6, 2006, etc.).
  2. You will need a file called Date.cpp that implements the above functionality.
  3. You will write your own tests in a main function inside testDate.cpp to test all functionality of your Date class.
  4. Add the following to your test program:
#include <iostream>
#include "Date.h" 
using namespace std;

int main() { 
   Date a, b, c; 
   Date d(2,29,2012); 
   Date e(4,15,2012); 
   Date f(4,31,2011); 
   Date g(1,1,2020);

   a=e; 
   b=a;
   a.increment(); // only required for extra credit
   cout << a.getDay() << "/" << b.getMonth() << "/" << d.getYear() << endl; 
   c.setDate(a.getDay(),a.getMonth(),e.getYear());

   a.displayDate(); 
   b.displayDate(); 
   c.displayDate(); 
   d.displayDate(); 
   e.displayDate(); 
   f.displayDate(); 
   g.displayDate();

   if (a.equals(c)) 
      cout << "a=c" << endl; 
   else 
      cout << "a!=c" << endl;

   c.increment(); // only required for extra credit

   if (a.equals(c)) 
      cout << "a=c" << endl; 
   else 
      cout << "!a=c" << endl;

   g.setDate(2,5,1999); f=g;

   g.add(134); f.sub(1023);  // only required for extra credit

   a.displayDate(); 
   b.displayDate(); 
   c.displayDate(); 
   d.displayDate(); 
   e.displayDate(); 
   f.displayDate(); 
   g.displayDate();
   if (a.setMonth(13)) 
      cout << "13 is a month" << endl; 
   else 
      cout << "13 is not a month" << endl;

   if (b.setMonth(5)) 
      cout << "5 is a month" << endl; 
   else 
      cout << "5 is not a month" << endl;

   a.displayDate(); 
   b.displayDate();
   c.setDate(12,23,2011); 

   // only required for extra credit
   for (int i = 1; i<50 ; i++) { 
      c.increment(); 
      c.displayDate(); 
   }
} 

  1. Compile and run your code.
  2. When you're happy with your code use moodle to submit your 3 files: Date.h, Date.cpp, and testDate.cpp. MAKE SURE YOUR NAME IS ON THE TOP OF EACH FILE! Also turn in a run that test each operation in your code.

Submission Checklist

  • Date.h Date.cpp testDate.cpp output.txt

Extra Credit (+10%)

Implement the following extra methods in your Date class.

  • void increment()
    Add 1 day to the current date.
  • void add(int n)
    Adds n number of days to the current date.
  • void sub(int n)
    Subtracts n number of days from the current date. If the subtraction would return a date before 1/1/1900, print an appropriate error message and set the date to 1/1/1900.
  • Date(string date)
    Constructor that takes a string date and parses it to create a valid date object. If the input is invalid, it outputs an error message and returns a date object representing January 1, 1900. You decide what type of date strings that you will accept.
  • readDate()
    Read a string containing a date from standard input and sets the current object to that date. Should this method print a prompt to standard out? Make sure you put a comment in your code about the types of input your code will accept and that your driver program tests them all.
  • int difference(Date const & date) const
    Calculates the number of days between this object and the parameter object. You should return a negative number if the current object is after the parameter object.
-- JimSkon - 2011-02-14
Topic revision: r9 - 2011-03-15 - JimSkon
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback