In: Computer Science
C++
1. Start with a UML diagram of the class definition for the
following problem defining
a utility class named Month.
2. Write 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.
• Write 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
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
class Month{
private:
int monthNumber;
public:
// default constructor
Month(){
monthNumber = 1;
}
// constructor with month number as parameter
Month(int number){
if(number >=1 && number <=12)
monthNumber = number;
else
monthNumber = 1;
}
// constructor for month name as parameter
Month(string name){
transform(name.begin(), name.end(), name.begin(), ::tolower);
if(name == "january"){
monthNumber = 1;
}else if(name == "january"){
monthNumber = 1;
}else if(name == "february"){
monthNumber = 2;
}else if(name == "march"){
monthNumber = 3;
}else if(name == "april"){
monthNumber = 4;
}else if(name == "may"){
monthNumber = 5;
}else if(name == "june"){
monthNumber = 6;
}else if(name == "july"){
monthNumber = 7;
}else if(name == "august"){
monthNumber = 8;
}else if(name == "september"){
monthNumber = 9;
}else if(name == "october"){
monthNumber = 10;
}else if(name == "november"){
monthNumber = 11;
}else if(name == "december"){
monthNumber = 11;
}else{
monthNumber = 1;
}
}
// Method to set month number
void setMonthNumber(int number){
if(number >=1 && number <=12)
monthNumber = number;
else
monthNumber = 1;
}
// Method to get month number
int getMonthNumber(){
return monthNumber;
}
// Method to get month name
string getMonthName(){
string name= "January";
switch(monthNumber){
case 1:
name = "January";
break;
case 2:
name = "February";
break;
case 3:
name = "March";
break;
case 4:
name = "April";
break;
case 5:
name = "May";
break;
case 6:
name = "June";
break;
case 7:
name = "July";
break;
case 8:
name = "August";
break;
case 9:
name = "September";
break;
case 10:
name = "October";
break;
case 11:
name = "November";
break;
case 12:
name = "December";
break;
}
return name;
}
// Method to print month number and month name
void print(){
cout << getMonthNumber() <<" " << getMonthName()
<< endl;
}
};
int main() {
int number;
string name;
Month* m = new Month();
cout << "using default constructor"<<endl;
m -> print();
cout<<"Enter a month number between 1 and
12"<<endl;
cin>>number;
cout<<"Using the constructor with a
parameter"<<endl;
m = new Month(number);
m->print();
cout<<"Now please enter a month name"<<endl;
cin>>name;
cout<<"Using the constructor that accepts a month
name"<<endl;
m = new Month(name);
m->print();
cout<<"Enter a number representing a month number
"<<endl;
cin>>number;
m = new Month();
m->setMonthNumber(number);
cout<<"Month number is :
"<<m->getMonthNumber()<<endl;
cout<<"Month name for the number you entered is :
"<<m->getMonthName()<<endl;
}
//Output