Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define a class called Month that is an abstract data type for a month. Your clas

ID: 3563610 • Letter: D

Question

Define a class called Month that is an abstract data type for a month. Your class will have one member variable of type int to represent a month (1 for January, 2 for February, and so forth).Include all the following member functions: a constructor to set the month using the first three letters in the name of the month as three arguments, a constructor to set the month using an integer as an argument (1 for JAN, 2 for FEB, 3 for MAR, etc.), a default constructor, an input function that reads the month as an integer, an input function that reads the month as the first three letters in the name of the month, an output function that outputs the month as an integer, an output function that outputs the month as the first three letters in the name of the month, and a member function that returns the next month as a value of type Month. Embed your class definition in a test program.

Explanation / Answer

#include <iostream>

#include <cstdlib> // for exit()

#include <cctype> // for tolower()

using namespace std;

class Month

{

public:

//constructor to set month based on first 3 chars of the month name

Month(char c1, char c2, char c3);   // done, debugged

//a constructor to set month base on month number, 1 = January etc.

Month( int monthNumber);           // done, debugged

//a default constructor (what does it do? nothing)

Month(); // done, no debugging to do

//an input function to set the month based on the month number

void getMonthByNumber(istream&); // done, debugged

//input function to set the month based on a three character input

void getMonthByName(istream&);   // done, debugged

//an output function that outputs the month as an integer,

void outputMonthNumber(ostream&); // done, debugged

//an output function that outputs the month as the letters.

void outputMonthName(ostream&);   // done, debugged

//a function that returns the next month as a month object

Month nextMonth(); //

//NB: each input and output function have a single formal parameter

//for the stream

  

int monthNumber();

private:

int mnth;

};

//added

int Month::monthNumber()

{

return mnth;

}

Month Month::nextMonth()

{

int nextMonth = mnth + 1;

if (nextMonth == 13)

    nextMonth = 1;

return Month(nextMonth);

}

Month::Month( int monthNumber)

{

mnth = monthNumber;

}

void Month::outputMonthNumber( ostream& out )

{

//cout << "The current month is "; // only for debugging

out << mnth;

}

// This implementation could profit greatly from use of an array!

void Month::outputMonthName(ostream& out)

{

// a switch is called for. We don't have one yet!

if (1 == mnth) out << "Jan";

else if (2 == mnth) out << "Feb";

else if (3 == mnth) out << "Mar";

else if (4 == mnth) out << "Apr";

else if (5 == mnth) out << "May";

else if (6 == mnth) out << "Jun ";

else if (7 == mnth) out << "Jul ";

else if (8 == mnth) out << "Aug";

else if (9 == mnth) out << "Sep";

else if (10 == mnth) out << "Oct";

else if (11 == mnth) out << "Nov";

else if (12 == mnth) out << "Dec";

}

void error(char c1, char c2, char c3)

{

cout << endl << c1 << c2 << c3 << " is not a month. Exiting ";

exit(1);

}

void error(int n)

{

cout << endl << n << " is not a month number. Exiting" << endl;

exit(1);

}

void Month::getMonthByNumber(istream& in)

{

in >> mnth; // int Month::mnth;

}

// use of an array and linear search could help this implementation.

void Month::getMonthByName(istream& in)

{

// Calls error(...) which exits, if the month name is wrong.

// An enhancement would be to allow the user to fix this.

char c1, c2, c3;

in >> c1 >> c2 >> c3;

c1 = tolower(c1); //force to lower case so any case

c2 = tolower(c2); //the user enters is acceptable

c3 = tolower(c3);

if('j' == c1)

    if('a' == c2)

      mnth = 1; // jan

    else

      if ('u' == c2)

        if('n' == c3)

           mnth = 6; // jun

         else if ('l' == c3)

           mnth = 7; // jul

        else error(c1, c2, c3); // ju, not n or

      else error(c1, c2, c3); // j, not a or u

else

   if('f' == c1)

       if('e' == c2)

          if('b' == c3)

             mnth = 2; // feb

          else error(c1, c2, c3); // fe, not b

       else error(c1, c2, c3); // f, not e

   else

     if('m' == c1)

        if('a' == c2)

           if('y' == c3)

              mnth = 5; // may

           else

             if('r' == c3)

               mnth = 3; // mar

              else error(c1, c2, c3); // ma not a, r

        else error(c1,c2,c3); // m not a or r

     else

        if('a' == c1)

          if('p' == c2)

             if('r' == c3)

                mnth = 4; // apr

             else error(c1, c2, c3 ); // ap not r

          else

             if('u' == c2)

                if('g' == c3)

                   mnth = 8; // aug

                else error(c1,c2,c3); // au not g

             else error(c1,c2,c3); // a not u or p

        else

          if('s' == c1)

             if('e' == c2)

                if('p' == c3)

                   mnth = 9; // sep

                else error(c1, c2, c3); // se, not p

             else error(c1, c2, c3); // s, not e

          else

             if('o' == c1)

                if('c' == c2)

                   if('t' == c3)

                      mnth = 10; // oct

                   else error(c1, c2, c3); // oc, not t

                else error(c1, c2, c3); // o, not c

             else

                if('n' == c1)

                   if('o' == c2)

                      if('v' == c3)

                         mnth = 11; // nov

                      else error(c1, c2, c3); // no, not v

                   else error(c1, c2, c3); // n, not o

                else

                   if('d' == c1)

                      if('e' == c2)

                        if('c' == c3)

                            mnth = 12; // dec

                        else error(c1, c2, c3);// de, not c

                       else error(c1, c2, c3);// d, not e

                   else error(c1, c2, c3);//c1,not j,f,m,a,s,o,n,or d

   }

Month::Month(char c1, char c2, char c3)

{

c1 = tolower(c1);

c2 = tolower(c2);

c3 = tolower(c3);

if('j' == c1)

     if('a' == c2)

        mnth=1; // jan

     else if ('u' == c2)

        if('n' == c3)

           mnth = 6; // jun

        else if('l' == c3)

           mnth = 7; // jul

        else error(c1, c2, c3); // ju, not n or

     else error(c1, c2, c3); // j, not a or u

else if('f' == c1)

     if('e' == c2)

        if('b' == c3)

           mnth = 2; // feb

        else error(c1, c2, c3); // fe, not b

     else error(c1, c2, c3); // f, not e

else

     if('m' == c1)

        if('a' == c2)

           if('y' == c3)

              mnth = 5; // may

           else

              if('r' == c3)

                 mnth = 3; // mar

              else error(c1, c2, c3); // ma not a, r

       else error(c1,c2,c3); // m not a or r

    else

      if('a' == c1)

        if('p' == c2)

           if('r' == c3)

              mnth = 4; // apr

           else error(c1, c2, c3 ); // ap not r

        else

          if('u' == c2)

            if('g' == c3)

               mnth = 8; // aug

            else error(c1,c2,c3); // au not g

          else error(c1,c2,c3); // a not u or p

     else

       if('s' == c1)

          if('e' == c2)

             if('p' == c3)

                mnth = 9; // sep

             else error(c1, c2, c3); // se, not p

          else error(c1, c2, c3); // s, not e

       else

          if('o' == c1)

             if('c' == c2)

               if('t' == c3)

                  mnth = 10; // oct

             else error(c1, c2, c3); // oc, not t

          else error(c1, c2, c3); // o, not c

       else

          if('n' == c1)

             if('o' == c2)

               if('v' == c3)

                 mnth = 11; // nov

               else error(c1, c2, c3); // no, not v

             else error(c1, c2, c3); // n, not o

          else

             if('d' == c1)

                 if('e' == c2)

                   if('c' == c3)

                      mnth = 12; // dec

                   else error(c1, c2, c3); // de, not c

                else error(c1, c2, c3); // d, not e

             else error(c1, c2, c3);//c1 not j,f,m,a,s,o,n,or d

}

Month::Month()

{

   // body deliberately empty

}

int main()

{

    cout << "testing constructor Month(char, char, char)" << endl;

    Month m;

    m = Month( 'j', 'a', 'n');

    m.outputMonthNumber( cout ); cout << " ";

    m.outputMonthName(cout); cout << endl;

    m = Month( 'f', 'e', 'b');

    m.outputMonthNumber( cout ); cout << " ";

    m.outputMonthName(cout); cout << endl;

    m = Month( 'm', 'a', 'r');

    m.outputMonthNumber( cout ); cout << " ";

  m.outputMonthName(cout); cout << endl;

    m = Month( 'a', 'p', 'r');

    m.outputMonthNumber( cout ); cout << " ";

    m.outputMonthName(cout); cout << endl;

    m = Month( 'm', 'a', 'y');

    m.outputMonthNumber( cout ); cout << " ";

    m.outputMonthName(cout); cout << endl;

    m = Month( 'j', 'u', 'n');

    m.outputMonthNumber( cout ); cout << " ";

    m.outputMonthName(cout); cout << endl;

    m = Month( 'j', 'u', 'l');

    m.outputMonthNumber( cout ); cout << " ";

    m.outputMonthName(cout); cout << endl;

    m = Month( 'a', 'u', 'g');

    m.outputMonthNumber( cout ); cout << " ";

    m.outputMonthName(cout); cout << endl;

    m = Month( 's', 'e', 'p');

    m.outputMonthNumber( cout ); cout << " ";

    m.outputMonthName(cout); cout << endl;

    m = Month( 'o', 'c', 't');

    m.outputMonthNumber( cout ); cout << " ";

    m.outputMonthName(cout); cout << endl;

    m = Month( 'n', 'o', 'v');

    m.outputMonthNumber( cout ); cout << " ";

    m.outputMonthName(cout); cout << endl;

    m = Month( 'd', 'e', 'c');

    m.outputMonthNumber( cout ); cout << " ";

    m.outputMonthName(cout); cout << endl;

    cout << endl << "Testing Month(int) constructor" << endl;

    int i = 1;

    while (i <= 12)

    {

        Month mm(i);

        mm.outputMonthNumber( cout ); cout << " ";

        mm.outputMonthName(cout); cout << endl;

        i = i+1;

    }

    cout << endl

         << "Testing the getMonthByName and outputMonth* ";

    i = 1;

    Month mm;

    while (i <= 12)

    {

        mm.getMonthByName(cin);

       mm.outputMonthNumber( cout ); cout << " ";

        mm.outputMonthName(cout); cout << endl;

        i = i+1;

    }

    cout << endl

        << "Testing getMonthByNumber and outputMonth* " << endl;

    i = 1;

    while (i <= 12)

    {

        mm.getMonthByNumber(cin);

        mm.outputMonthNumber( cout ); cout << " ";

        mm.outputMonthName(cout); cout << endl;

        i = i+1;

    }

    cout << endl << "end of loops" << endl;

    cout << endl << "Testing nextMonth member" << endl;

    cout << "current month ";

    mm.outputMonthNumber(cout); cout << endl;

    cout << "next month ";

    mm.nextMonth().outputMonthNumber(cout); cout << " ";

    mm.nextMonth().outputMonthName(cout); cout << endl;

    cout << endl << "new Month created " << endl;

    Month mo(6);

    cout << "current month ";

    mo.outputMonthNumber(cout); cout << endl;

    cout << "nextMonth ";

    mo.nextMonth().outputMonthNumber(cout); cout << " ";

    mo.nextMonth().outputMonthName(cout); cout << endl;

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote