Question: //PLEASE CORRECT MY CODE //THE CODE HAS TO MATCH WITH THE SAMPLE RUN /* 2. a class named Month. The class should have an integer
//PLEASE CORRECT MY CODE
//THE CODE HAS TO MATCH WITH THE SAMPLE RUN
/*
2. a class named Month. The class should have an integer field named monthNumber that holds the number of the month (January is 1, February is 2, etc.). Also provide the following functions: A default constructor that sets the monthNumber field to 1. A constructor that accepts the number of month as an argument and sets the monthNumber field to the passed value. Set the monthNumber to 1 if the passed argument's value is not a value between 1 and 12. A constructor that accepts the name of month, such as "January" as an argument and sets the monthNumber field to the correct corresponding value. Make sure this is not case sensitive, so entering "January" will return the same value as if "JaNUary" was entered. If an invalid month is entered, set the monthNumber to 1. A setMonthNumber function that accepts an integer argument to assign to the monthNumber field. Set the monthNumber to 1 if the passed argument's value is not a value between 1 and 12. A getMonthNumber function that returns the value in the monthNumber field. A getMonthName function that returns the name of the month. For example, if the monthNumber field is 1, then this function will return "January". Note that this function does not accept any parameters. It looks up the monthNumber field to make its decision. A function named print that prints the month number and the month name. a main program that uses the class Month and test various operations on the objects of the class Month. Perform a series of operations to test each of the functions and the constructors. Make sure to prompt the user to enter a value for monthNumber as you test your code. As part of your testing, generate a set of random numbers for month number (between 1 and 12) and display the month number along with the month name. Here is a possible sample run: Using the default constructor - Month number = 1 Please enter a month number between 1 and 12: 9 Using the constructor with a parameter - Month number = 9 Now please enter a month name: aprillll Using the constructor that accepts a month name: January Enter a number representing a month number 12 Month number is: 12 Month name for the number you entered is: December Testing the print function 12 December Testing the functions to assign a month number and check month name Generating a random number between 1 and 12: 8 Month name is: August Generating a random number between 1 and 12: 2 Month name is: February Generating a random number between 1 and 12: 12 Month name is: December
Generating a random number between 1 and 12: 4 Month name is: April Another run: Using the default constructor - Month number = 1
Please enter a month number between 1 and 12: 0
Using the constructor with a parameter - Month number = 1
Now please enter a month name: MAy
Using the constructor that accepts a month name: May
Enter a number representing a month number 123 Month number is: 1
Month name for the number you entered is: January
Testing the print function 1 January
Testing the functions to assign a month number and check month name
Generating a random number between 1 and 12: 7 Month name is: July
Generating a random number between 1 and 12: 3 Month name is: March
Generating a random number between 1 and 12: 7 Month name is: July
Generating a random number between 1 and 12: 5 Month name is: May
MY CODE IS:
*/
//Test Program Month
#include
#include
#include
using namespace std;
//interface
class Month
{
public:
Month();
//Constructor
//Sets monthNumber to 1
Month(int numMonth);
//Constructor
//Sets numMonth and monthNumber according to the parameters.
//Postcondition: monthNumber = numMonth
Month(string month);
//Constructor
//Sets month and monthName according to the parameters.
//Postcondition: monthName = month;
void print();
//Function to output the monthNumber: numMonth and monthName: month
void setMonthNumber(int numMonth);
//Function to set number month according
//to the parameters.
//Postcondition: monthNumber = numMonth
void setMonthName(string month);
//Function to set month name
//to the parameters.
//Postcondition: monthName = month
int getMonthNumber();
//Function to return the month number.
//Postcondition: The value of month number is returned.
string getMonthName();
//Function to return the month name.
//Postcondition: The value of month name is returned.
private:
int monthNumber;//variable to store the number month
//string monthName; //variable to store the month name
};
//implementation
/// prints the object's attributes
/// parameters
///
/// @return - none
int main()
{
Month m;
int monthNumber;
cout << "Using default constructor - Month Number= " << m.getMonthNumber() << endl;
cout << "Please enter a month number between 1 and 12: " << endl;
cin >> monthNumber;
m.setMonthNumber(m.getMonthNumber());
cout << "Using the constructor with a parameter - Month Number= " << m.getMonthNumber() << endl;
cout << " Now please enter a month name" << endl;
cin >> monthNumber;
m.setMonthNumber(m.getMonthNumber());
cout << "Using the constructor that accepts a month name: " << m.getMonthName() << endl;
cout << "Enter a number representing a month number: " << endl;
cin >> monthNumber;
m.setMonthNumber(m.getMonthNumber());
cout << "Month number is: " << m.getMonthNumber() << endl;
cout << "Month name for the number you entered is: " << m.getMonthName() << endl;
cout << "Testing the print function" << endl;
cout << m.getMonthNumber() <<" " << m.getMonthName() << endl;
cout << "Testing the functions to assign a month number and check month name" << endl;
srand(time(0));
for(int i=0;i<4;i++)
{
monthNumber = (rand() % 12) + 1;
m.setMonthNumber(m.getMonthNumber());
cout << "Generating a random number between 1 and 12:" << m.getMonthNumber() << endl;
cout << "Month name is: " << m.getMonthName() << endl;
}
m.getMonthNumber();
m.getMonthName();
m.print();
return 0;
}
Month::Month()//default - no arguments
{
monthNumber = 1;
}
Month::Month(int numMonth)
{
if(monthNumber<1 && monthnumber>12)
monthNumber=1;
monthNumber = numMonth;
}
//constructor
/// sets the monthName to month, monthNumber to numMonth
/// parameters
Month::Month(string monthName)
{
//monthName = tolower(monthName);
if (monthName == "January")
monthNumber = 1;
else if(monthName == "February")
monthNumber = 2;
else if(monthName == "March")
monthNumber = 3;
else if(monthName == "April")
monthNumber = 4;
else if(monthName == "May")
monthNumber = 5;
else if(monthName == "June")
monthNumber = 6;
else if(monthName == "July")
monthNumber = 7;
else if(monthName == "August")
monthNumber = 8;
else if(monthName == "September")
monthNumber = 9;
else if(monthName == "October")
monthNumber = 10;
else if(monthName == "November")
monthNumber = 11;
else if(monthName == "December")
monthNumber = 12;
else
monthNumber = 1;
//monthNumber = monthName;
}
//constructor
/// sets the monthName to month, monthNumber to numMonth
/// parameters
void Month::setMonthNumber(int numMonth)
{
if(monthNumber<1 && monthnumber>12)
monthNumber=1;
monthNumber = numMonth;
}
/// sets the values of attribute based on its parameter
/// parameters
/// @return - none
int Month::getMonthNumber() {
return monthNumber;
}
/// get the month number
/// parameters
///
/// @return - monthNumber
string Month::getMonthName()
{
string monthName;
switch (monthNumber)
{
case 1:
monthName = "January";
break;
case 2:
monthName = "February";
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
case 7:
monthName = "July";
break;
case 8:
monthName = "August";
break;
case 9:
monthName = "September";
break;
case 10:
monthName = "October";
break;
case 11:
monthName = "November";
break;
case 12:
monthName = "December";
break;
default:
return "January";
}
return monthName;
}
void Month::print()
{
cout << "Month number is: " << monthNumber << endl;
cout << "Month name for the number you entered is: " << getMonthName() << endl;
}
/// sets the values of attribute based on its parameter
/// parameters
/// @param - month [in], monthName
///
///@param - numMonth [in], monthNumber
/// @return - none
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
