Question

In: Computer Science

Build a Date class and a main function to test it Specifications Below is the interface...

Build a Date class and a main function to test it

Specifications
Below is the interface for the Date class: it is our "contract" with you: you have to implement everything it describes, and show us that it works with a test harness that puts it through its paces. The comments in the interface below should be sufficient for you to understand the project (use these comments in your Date declaration), without the need of any further documentation.

class Date
{
 private:
   unsigned day;
   unsigned month;
   string monthName;
   unsigned year;

 public:
   // creates the date January 1st, 2000.
   Date();


   /* parameterized constructor: month number, day, year 
       - e.g. (3, 1, 2010) will construct the date March 1st, 2010

       If any of the arguments are invalid (e.g. 15 for month or 32 for day)
       then the constructor will construct instead a valid Date as close
       as possible to the arguments provided - e.g. in above example,
       Date(15, 32, 2010), the Date would be corrected to Dec 31st, 2010.
       In case of such invalid input, the constructor will issue a console error message: 

       Invalid date values: Date corrected to 12/31/2010.
       (with a newline at the end).
   */
   Date(unsigned m, unsigned d, unsigned y);


   /* parameterized constructor: month name, day, year
 ­      - e.g. (December, 15, 2012) will construct the date December 15th, 2012

       If the constructor is unable to recognize the string argument as a valid month name,
       then it will issue a console error message: 

       Invalid month name: the Date was set to 1/1/2000.
       (with a newline at the end).

       If the day argument is invalid for the given month (but the month name was valid),
       then the constructor will handle this error in the same manner as the other
       parameterized constructor. 

       This constructor will recognize both "december" and "December"
       as month name.
   */
   Date(const string &mn, unsigned d, unsigned y);


   /* Outputs to the console (cout) a Date exactly in the format "3/1/2012". 
      Does not output a newline at the end.
   */
   void printNumeric() const;


   /* Outputs to the console (cout) a Date exactly in the format "March 1, 2012".
      The first letter of the month name is upper case, and the month name is
      printed in full - January, not Jan, jan, or january. 
      Does not output a newline at the end.
   */
   void printAlpha() const;

 private:

   /* Returns true if the year passed in is a leap year, otherwise returns false.
   */
   bool isLeap(unsigned y) const;


   /* Returns number of days allowed in a given month
      -  e.g. daysPerMonth(9, 2000) returns 30.
      Calculates February's days for leap and non-­leap years,
      thus, the reason year is also a parameter.
   */
   unsigned daysPerMonth(unsigned m, unsigned y) const;

   /* Returns the name of a given month
      - e.g. name(12) returns the string "December"
   */
   string name(unsigned m) const;

   /* Returns the number of a given named month
      - e.g. number("March") returns 3
   */
   unsigned number(const string &mn) const;
};

Private Member Functions

The functions declared private above, isLeap, daysPerMonth, name, number, are helper functions - member functions that will never be needed by a user of the class, and so do not belong to the public interface (which is why they are "private"). They are, however, needed by the interface functions (public member functions), which use them to test the validity of arguments and construct valid dates. For example, the constructor that passes in the month as a string will call the number function to assign a value to the unsigned member variable month.

isLeap: The rule for whether a year is a leap year is:

(year % 4 == 0) implies leap year

except (year % 100 == 0) implies NOT leap year

except (year % 400 == 0) implies leap year

So, for instance, year 2000 is a leap year, but 1900 is NOT a leap year. Years 2004, 2008, 2012, 2016, etc. are all leap years. Years 2005, 2006, 2007, 2009, 2010, etc. are NOT leap years.

Output Specifications

Read the specifications for the print function carefully. The only cout statements within your Date member functions should be:

1. the "Invalid Date" warnings in the constructors

2. in your two print functions

Required main function to be used:

Date getDate();

int main() {

   Date testDate;
   testDate = getDate();
   cout << endl;
   cout << "Numeric: ";
   testDate.printNumeric();
   cout << endl;
   cout << "Alpha:   ";
   testDate.printAlpha();
   cout << endl;

   return 0;
}

Date getDate() {
   int choice;
   unsigned monthNumber, day, year;
   string monthName;

   cout << "Which Date constructor? (Enter 1, 2, or 3)" << endl
      << "1 - Month Number" << endl
      << "2 - Month Name" << endl
      << "3 - default" << endl;
   cin >> choice;
   cout << endl;

   if (choice == 1) {
      cout << "month number? ";
      cin >> monthNumber;
      cout << endl;
      cout << "day? ";
      cin >> day;
      cout << endl;
      cout << "year? ";
      cin >> year;
      cout << endl;
      return Date(monthNumber, day, year);
   } else if (choice == 2) {
      cout << "month name? ";
      cin >> monthName;
      cout << endl;
      cout << "day? ";
      cin >> day;
      cout << endl;
      cout << "year? ";
      cin >> year;
      cout << endl;
      return Date(monthName, day, year);
   } else {
      return Date();
   }
}

Solutions

Expert Solution

main.cpp

#include <iostream>
#include "Date.h"

using namespace std;
Date getDate();

int main() {

   Date testDate;
   testDate = getDate();
   cout << endl;
   cout << "Numeric: ";
   testDate.printNumeric();
   cout << endl;
   cout << "Alpha:   ";
   testDate.printAlpha();
   cout << endl;

   return 0;
}

Date getDate() {
   int choice;
   unsigned monthNumber, day, year;
   string monthName;

   cout << "Which Date constructor? (Enter 1, 2, or 3)" << endl
      << "1 - Month Number" << endl
      << "2 - Month Name" << endl
      << "3 - default" << endl;
   cin >> choice;
   cout << endl;

   if (choice == 1) {
      cout << "month number? ";
      cin >> monthNumber;
      cout << endl;
      cout << "day? ";
      cin >> day;
      cout << endl;
      cout << "year? ";
      cin >> year;
      cout << endl;
      return Date(monthNumber, day, year);
   } else if (choice == 2) {
      cout << "month name? ";
      cin >> monthName;
      cout << endl;
      cout << "day? ";
      cin >> day;
      cout << endl;
      cout << "year? ";
      cin >> year;
      cout << endl;
      return Date(monthName, day, year);
   } else {
      return Date();
   }
}

Date.h

#include<iostream>
#include<string>

using namespace std;

class Date
{
    private:
        unsigned day;
        unsigned month;
        string monthName;
        unsigned year;
        bool isLeap(unsigned y) const;
        unsigned daysPerMonth(unsigned m, unsigned y) const;
        string name(unsigned m) const;
        unsigned number(const string &mn) const;
    public:
        Date();
        Date(unsigned m, unsigned d, unsigned y);
        Date(const string &mn, unsigned d, unsigned y);
        void printNumeric() const;
        void printAlpha() const;
        Date addDays( int ) const;
};

Date.cpp

#include <iostream>
#include <string>
#include "Date.h"

using namespace std;

Date::Date()
:day(1),month(1),year(2000)
{

}

Date::Date(unsigned m, unsigned d, unsigned y)
:day(d),month(m),year(y)
{
    if(d > daysPerMonth(m,y))
    {
        cout << "Invalid date values: Date corrected to 12/31/2010." << endl;
    }
    if(d == 29 && m == 2 && !isLeap(y))
    {
        day = 28;
    }
}

Date::Date(const string &mn, unsigned d, unsigned y)
:day(d),monthName(mn),year(y)
{
    month = number(mn);
}

void Date::printNumeric() const
{
    cout << month << "/" << day << "/" << year;
}

void Date::printAlpha() const
{
    cout << name(month) << " " << day << "," << ' ' << year;
}

bool Date::isLeap(unsigned y) const
{
    return ((y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0)));
}

unsigned Date::daysPerMonth(unsigned m, unsigned y) const
{
    unsigned days;
  
    if(m == 1)
    {
        days = 31;
    }
    else if(m == 2)
    {
        if(isLeap(y))
        {
            days = 29;
        }
        else
        {
            days = 28;
        }
    }
    else if(m == 3)
    {
        days = 31;
    }
    else if(m == 4)
    {
        days = 30;
    }
    else if(m == 5)
    {
        days = 31;
    }
    else if(m == 6)
    {
        days = 30;
    }
    else if(m == 7)
    {
        days = 31;
    }
    else if(m == 8)
    {
        days = 31;
    }
    else if(m == 9)
    {
        days = 30;
    }
    else if(m == 10)
    {
        days = 31;
    }
    else if(m == 11)
    {
        days = 30;
    }
    else if(m >= 12)
    {
        days = 31;
    }
  
    return days;
}

string Date::name(unsigned m) const
{
    string mname;
  
    if(m == 1)
    {
        mname = "January";
    }
    else if(m == 2)
    {
        mname = "February";
    }
    else if(m == 3)
    {
        mname = "March";
    }
    else if(m == 4)
    {
        mname = "April";
    }
    else if(m == 5)
    {
        mname = "May";
    }
    else if(m == 6)
    {
        mname = "June";
    }
    else if(m == 7)
    {
        mname = "July";
    }
    else if(m == 8)
    {
        mname = "August";
    }
    else if(m == 9)
    {
        mname = "September";
    }
    else if(m == 10)
    {
        mname = "October";
    }
    else if(m == 11)
    {
        mname = "November";
    }
    else if(m >= 12)
    {
        mname = "December";
    }
  
    return mname;
}

unsigned Date::number(const string &mn) const
{
    unsigned mnum;
  
    if(mn == "January" || mn == "january")
    {
        mnum = 1;
    }
    else if(mn == "February" || mn == "february")
    {
        mnum = 2;
    }
    else if(mn == "March" || mn == "march")
    {
        mnum = 3;
    }
    else if(mn == "April" || mn == "april")
    {
        mnum = 4;
    }
    else if(mn == "May" || mn == "may")
    {
        mnum = 5;
    }
    else if(mn == "June" || mn == "june")
    {
        mnum = 6;
    }
    else if(mn == "July" || mn == "july")
    {
        mnum = 7;
    }
    else if(mn == "August" || mn == "august")
    {
        mnum = 8;
    }
    else if(mn == "September" || mn == "september")
    {
        mnum = 9;
    }
    else if(mn == "October" || mn == "october")
    {
        mnum = 10;
    }
    else if(mn == "November" || mn == "november")
    {
        mnum = 11;
    }
    else if(mn == "December" || mn == "december")
    {
        mnum = 12;
    }
  
    return mnum;
}

Date Date::addDays(int d) const
{
    return Date(month + (day + d)/daysPerMonth(month, year), (day + d)%daysPerMonth(month, year), year + (month + (day + d)/daysPerMonth(month, year))/12);
}



Related Solutions

The Date Class This class will function similarly in spirit to the Date class in the...
The Date Class This class will function similarly in spirit to the Date class in the text, and behaves very much like a “standard” class should. It should store a month, day, and year, in addition to providing useful functions like date reporting (toString()), date setting (constructors and getters/setters), as well as some basic error detection (for example, all month values should be between 1-12, if represented as an integer). Start this by defining a new class called “Date” or...
Python: What are the defintions of the three method below for a class Date? class Date(object):...
Python: What are the defintions of the three method below for a class Date? class Date(object): "Represents a Calendar date"    def __init__(self, day=0, month=0, year=0): "Initialize" pass def __str__(self): "Return a printable string representing the date: m/d/y" pass    def before(self, other): "Is this date before that?"
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use deep copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use shallow copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
C Programming build a function that is outside of main domain but can be called Build...
C Programming build a function that is outside of main domain but can be called Build a function that will : - Take 3 arrays and their lengths as input: Array 1 for even numbers, Array 2 for odd numbers. Both 1 and 2 have the same size - put all elements in array 3 elements from array 1 should be placed in the even indexes and elements of array 2 should be placed in the odd indexes, in increasing...
(Using Date Class) The following UML Class Diagram describes the java Date class: java.util.Date +Date() +Date(elapseTime:...
(Using Date Class) The following UML Class Diagram describes the java Date class: java.util.Date +Date() +Date(elapseTime: long) +toString(): String +getTime(): long +setTime(elapseTime: long): void Constructs a Date object for the current time. Constructs a Date object for a given time in milliseconds elapsed since January 1, 1970, GMT. Returns a string representing the date and time. Returns the number of milliseconds since January 1, 1970, GMT. Sets a new elapse time in the object. The + sign indicates public modifer...
Write the following function and provide a program to test it (main and function in one...
Write the following function and provide a program to test it (main and function in one .py) def repeat(string, n, delim) that returns the string string repeated n times, separated by the string delim. For example repeat(“ho”, 3, “,”) returns “ho, ho, ho”. keep it simple.Python
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha so the main function is working properly. #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class alpha { private: int data; public: //YOUR CODE }; //////////////////////////////////////////////////////////////// int main() { alpha a1(37); alpha a2; a2 = a1; cout << "\na2="; a2.display(); //display a2 alpha a3(a1); //invoke copy constructor cout << "\na3="; a3.display(); //display a3 alpha a4 = a1; cout << "\na4="; a4.display(); cout << endl; return 0;...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main method. In the main method you should read in all of the rows of data from the Instructors Table in the Registration Database, then print out this data. Follow the steps outlined in class. Remember to use try-catch blocks for all database access code. The example in the book does not use Try-catch blocks, but you should
C++ * Program 2:      Create a date class and Person Class.   Compose the date class...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class in the person class.    First, Create a date class.    Which has integer month, day and year    Each with getters and setters. Be sure that you validate the getter function inputs:     2 digit months - validate 1-12 for month     2 digit day - 1-3? max for day - be sure the min-max number of days is validate for specific month...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT