Question

In: Computer Science

C++ 1. Start with a UML diagram of the class definition for the following problem defining...

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

Solutions

Expert Solution

#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


Related Solutions

1: (Passing Objects to Methods) From the following UML Class Diagram, define the Circle class in...
1: (Passing Objects to Methods) From the following UML Class Diagram, define the Circle class in Java. Circle Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the Circle class, you should test this class from the main() method by passing objects of this class to a method “ public static void printAreas(Circle c, int times)” You should display the area of the circle object 5 times with different radii.
1: (Passing Objects to Methods) From the following UML Class Diagram, define the Circle class in...
1: (Passing Objects to Methods) From the following UML Class Diagram, define the Circle class in Java. Circle Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the Circle class, you should test this class from the main() method by passing objects of this class to a method “ public static void printAreas(Circle c, int times)” You should display the area of the circle object 5 times with different radii.
create the UML Diagram to model a Movie/TV viewing site. Draw a complete UML class diagram...
create the UML Diagram to model a Movie/TV viewing site. Draw a complete UML class diagram which shows: Classes (Only the ones listed in bold below) Attributes in classes (remember to indicate privacy level, and type) No need to write methods Relationships between classes (has is, is a, ...) Use a program like Lucid Cart and then upload your diagram. Each movie has: name, description, length, list of actors, list of prequals and sequals Each TV Show has: name, description,...
Draw a UML diagram for the classes. Code for UML: // Date.java public class Date {...
Draw a UML diagram for the classes. Code for UML: // Date.java public class Date {       public int month;    public int day;    public int year;    public Date(int month, int day, int year) {    this.month = month;    this.day = day;    this.year = year;    }       public Date() {    this.month = 0;    this.day = 0;    this.year = 0;    } } //end of Date.java // Name.java public class Name...
Complete the following class UML design class diagram by filling in all the sections based on...
Complete the following class UML design class diagram by filling in all the sections based on the information given below.         The class name is Boat, and it is a concrete entity class. All three attributes are private strings with initial null values. The attribute boat identifier has the property of “key.” The other attributes are the manufacturer of the boat and the model of the boat. Provide at least two methods for this class. Class Name: Attribute Names: Method...
Given the following UML class diagram, implement the class as described by the model. Use your...
Given the following UML class diagram, implement the class as described by the model. Use your best judgment when implementing the code inside of each method. +---------------------------------------------------+ | Polygon | +---------------------------------------------------+ | - side_count : int = 3 | | - side_length : double = 1.0 | +---------------------------------------------------+ | + Polygon() | | + Polygon(side_count : int) | | + Polygon(side_count : int, side_length : double) | | + getSides() : int | | + setSides(side_count : int) | |...
1.) According to the UML Class Diagram, these are the mutator and accessor methods that you...
1.) According to the UML Class Diagram, these are the mutator and accessor methods that you need to define: 1a.) +setName(value:String):void 1b.) +setGPA(value:double):void 1c.) +setID(value: String):void 1d.) +getName(): String 1e.) +getLastName():String 2.) Working with constructors 2a.) +Student() : This is the default constuctor. For the Strings properties, initialize them to null. The order is String ID, String name, String lastName, and double GPA. 2b.) +Student(value:String) - This constructor receives just the ID. 2c.) +Student(value:String, var: String) - This constructor receives...
A UML class diagram has the following description about a member of the class: + getHeaderField(name:String):String...
A UML class diagram has the following description about a member of the class: + getHeaderField(name:String):String Which of the following is a correct declaration of the member in Java? 1 public String getHeaderField(String name) 2 public String getHeaderField; 3 public String getHeaderField(String) 4 public static String getHeaderField( name) ------------------------------------------------------------------------------------------------------------------------------------- Note: all answers are case sensitive. Inside a class, the declaration of a constructor looks like the following: public JButton(String text) 1. From the constructor, you infer that the name of...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the class are: NUM_EMPLOYEES: a constant integer variable to hold the number of employees. employeeId. An array of integers to hold employee identification numbers. hours. An array of integers to hold the number of hours worked by each employee payRate. An array of doubles to hold each employee’s hourly pay rate wages. An array of seven doubles to hold each employee’s gross wages The class...
Draw a UML diagram that describes a class that will be used to describe a product...
Draw a UML diagram that describes a class that will be used to describe a product for sale on Glamazon.com. The product has a name, a description, a price, ratings by many customers (1 to 5 stars), and a group of customer comments. New products have no ratings or comments by customers, but do have a name, description and price. The price can be changed and more customer ratings and comments can be added. A global average rating of all...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT