Question

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 month...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Month UML class diagram

Month monthNumber: int month: String +Month() +Month (monthNumber:int) +setMonthName ( +getMonthNumber ) int +Month (month:St

Java code:

//Month.java
import java.util.Random;
public class Month
{
   private int monthNumber;
   String month;

   /*Default constructor*/
   public Month()
   {
       monthNumber = 1;
   }
/*Constructor that takes monthNumber as input*/
   public Month(int monthNumber)
   {
       /*Calling setMonthNumber method*/
       setMonthNumber(monthNumber);
   }

   /*Method to set month number*/
   public void setMonthNumber(int monthNumber)
   {
       if (monthNumber >= 1 && monthNumber <= 12)
           this.monthNumber = monthNumber;
       else
           monthNumber = 1;
   }
   /*Returns the monthNumber*/
   public int getMonthNumber()
   {
       return monthNumber;
   }

   /*Set month number */
   public Month(String month)
   {
       this.month = month;
       if (this.month.equalsIgnoreCase("January"))
           this.monthNumber = 1;
       else if (this.month.equalsIgnoreCase("February"))
           this.monthNumber = 2;
       else if (this.month.equalsIgnoreCase("March"))
           this.monthNumber = 3;
       else if (this.month.equalsIgnoreCase("April"))
           this.monthNumber = 4;
       else if (this.month.equalsIgnoreCase("May"))
           this.monthNumber = 5;
       else if (this.month.equalsIgnoreCase("June"))
           this.monthNumber = 6;
       else if (this.month.equalsIgnoreCase("July"))
           this.monthNumber = 7;
       else if (this.month.equalsIgnoreCase("August"))
           this.monthNumber = 8;
       else if (this.month.equalsIgnoreCase("September"))
           this.monthNumber = 9;
       else if (this.month.equalsIgnoreCase("October"))
           this.monthNumber = 10;
       else if (this.month.equalsIgnoreCase("November"))
           this.monthNumber = 11;
       else if (this.month.equalsIgnoreCase("December"))
           this.monthNumber = 12;
   }

   /*Method that returns the month name*/
   public String getMonthName()
   {
       switch (monthNumber)
       {
       case 1:
           return "Janaury";
       case 2:
           return "February";
       case 3:
           return "March";
       case 4:
           return "April";
       case 5:
           return "May";
       case 6:
           return "June";
       case 7:
           return "July";
       case 8:
           return "August";
       case 9:
           return "September";
       case 10:
           return "October";
       case 11:
           return "November";
       case 12:
           return "December";
       default:
           return "January";
       }
   }
  
   public void print()
   {
       System.out.printf("Month Number : %d, Month Name: %s\n",
               monthNumber,getMonthName());
   }
   public static void main(String[] args)
   {
       /*Create an instance of Month class*/
       Month month=new Month();
       //calling paint method
       month.print();
       //set 12 as month number
       month.setMonthNumber(12);
       //calling paint method
       month.print();
       //create Random class
       Random rand=new Random();
       /*Generate 5 random number in a range of 1 to 12
       * and print the corresponding month names
       * */
       for(int i=0;i<5;i++)
       {
           /*Generate a random number in a range of 1 to 12*/
           int randomNumber=rand.nextInt(12)+1;
           /*Set random number as month number*/
           month.setMonthNumber(randomNumber);
           month.print();
       }
      
   }
}

Sample Output:

Month Number : 1, Month Name: Janaury
Month Number : 12, Month Name: December
Month Number : 10, Month Name: October
Month Number : 5, Month Name: May
Month Number : 8, Month Name: August
Month Number : 12, Month Name: December
Month Number : 2, Month Name: February

Add a comment
Know the answer?
Add Answer to:
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 month...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class named Month. The class should have an int field named monthNumber that holds...

    Write a class named Month. The class should have an int field named monthNumber that holds the number of the month. For example. January would be 1. February would be 2. and so forth. In addition, provide the following methods A no-arg constructor that sets the monthNumber field to 1 A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument. If a value less than...

  • Design a class named Month. The class should have the following private members:

    Design a class named Month. The class should have the following private members:   • name - A string object that holds the name of a month, such as "January", "February", etc.   • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.  In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...

  • Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks...

    Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks for the help. Specifications: The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods. A no- arg constructor that sets the monthNumber field to 1. A constructor that accepts the number of the month as an argument. It should...

  • All files require Javadoc documentation comments at the top to include a description of the program...

    All files require Javadoc documentation comments at the top to include a description of the program and an @author tag with your name  only.    -------------------------------------- Develop an exception class named InvalidMonthException that extends the Exception class.   Instances of the class will be thrown based on the following conditions: The value for a month number is not between 1 and 12 The value for a month name is not January, February, March, … December -------------------------------------- Develop a class named Month.  The class should define an integer...

  • Create a simple Java class for a Month object with the following requirements:  This program...

    Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...

  • I need help with my homework please. I should write this program in C++ language and...

    I need help with my homework please. I should write this program in C++ language and use Inventory.h file (header file), Inventory.cpp file (implementation file), and main.cpp file (main program). This is the program: Demonstrate the class in a driver program. Input validation, don’t accept negative values for item number, quantity, or cost. 6. Inventory Class Design an Inventory class that can hold information and calculate data for items ma retail store's inventory. The class should have the following private...

  • Write in C++ please: Write a class named MyVector using the following UML diagram and class...

    Write in C++ please: Write a class named MyVector using the following UML diagram and class attribute descriptions. MyVector will use a linked listed instead of an array. Each Contact is a struct that will store a name and a phone number. Demonstrate your class in a program for storing Contacts. The program should present the user with a menu for adding, removing, and retrieving Contact info. MyVector Contact name: string phone: string next: Contact -head: Contact -tail: Contact -list_size:...

  • Write a C++ program Write a class named Employee that has the following member variables: name...

    Write a C++ program Write a class named Employee that has the following member variables: name A string that holds the employee name idNumber An int to hold employee id number department A string to hold the name of the department position A string to hold the job position The class will have three constructors: 1. A constructor that accepts all the internal data such as: Employee susan("Susan Meyers", 47899, "Accounting", "Vice President"); 2. A constructor that accepts some of...

  • a) You will write a void function to get a positive integer between 1 and 12...

    a) You will write a void function to get a positive integer between 1 and 12 inclusive. The twelve numbers represent the months of the year. The user will be asked to enter an integer between 1 and 12 inclusive. If the number entered by the user is not within range, an appropriate error message will be displayed, and the user will be asked to try again until a valid number is entered. b) You will write a void function...

  • A. (C++) Write a class declaration named Circle, with a private member variable, radius. Write set...

    A. (C++) Write a class declaration named Circle, with a private member variable, radius. Write set and get functions to access the radius variable, and a function named get getArea which returns the area of the circle. The area is calculated as 3.14 * radius * radius. B. Write a constructor to the circle that accepts an argument and assign its value to the radius member variable. C. Add a static member variable that accounts for the currently active circle...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT