Question

C++ 3. Day of the Year Modification Modify the DayOfYear class, written in an earlier Programming...

C++

3. Day of the Year Modification

Modify the DayOfYear class, written in an earlier Programming Challenge, to add a

constructor that takes two parameters: a string representing a month and an integer in the

range 0 through 31 representing the day of the month. The constructor should then initialize

the integer member of the class to represent the day specified by the month and day of month

parameters. The constructor should terminate the program with an appropriate error

message if the number entered for a day is outside the range of days for the month given.

Add the following overloaded operators:

++ prefix and postfix increment operators. These operators should modify the DayOfYear

object so that it represents the next day. If the day is already the end of the year, the

new value of the object will represent the first day of the year.

-- prefix and postfix decrement operators. These operators should modify the DayOfYear

object so that it represents the previous day. If the day is already the first day of the

year, the new value of the object will represent the last day of the year.

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

DayofYear.cpp

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
class DayOfYear
{
private:
int year;
int day;
string month;
int count=0;
public:
DayOfYear& operator ++();
DayOfYear& operator --();
public:
DayOfYear(char month[], int day)
   {
       for(int i=0;i<12;i++)
       {
           if(month_names(i) == month)
           {
               if( day > 0 && day <= month_days(i))
               {
                   this->month = month;
                   this->day = day;
                   year =0;
                   for(int j=0;j<count;j++)
                   {
                       year=year+ month_days(j);
                   }
                   year=year+day;
                   cout<<"The Day Of The Year is:"<<year<<endl;
               }
               else
               {
                   cout<<"Incorrect date or Day"<<endl;
                   exit(0);
               }
              
           }
           count++;
       }
   }
   public:
   string month_names(int month_number)
   {
       string month_names[] ={"January","February","March","April", "May","June","July","August","September","October","November","December"};
       return month_names[month_number];
   }
   public:
   int month_days(int month_number)
   {
       int month_days[]={31,28,31,30,31,30,31,31,30,31,30,31};
       return month_days[month_number];
   }
};
int main()
{
   char month[12];
   int day;
   cout<<"-------------------------------------"<<endl;
   cout<<"Day of The Year Modification"<<endl;
   cout<<"Enter The Month of Year:"<<endl;
   cin>>month;
   cout<<"Enter the Day Of The Month"<<endl;
   cin>>day;
   DayOfYear obj(month,day);
   cout<<"------------End---------------"<<endl;
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ 3. Day of the Year Modification Modify the DayOfYear class, written in an earlier Programming...
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
  • Using simple, college level c++ programming Design a class called NumDays. The class’s purpose is to...

    Using simple, college level c++ programming Design a class called NumDays. The class’s purpose is to store a value that represents a number of work hours and convert it to a number of days. For example, 8 hours would be converted to 1 day, 12 hours would be converted to 1.5 days, and 18 hours would be converted to 2.25 days. The class should have a constructor that accepts a number of hours, as well as member functions for storing...

  • Design a class called NumDays. The class’ purpose is to store a value that represents a...

    Design a class called NumDays. The class’ purpose is to store a value that represents a number of work hours and convert it to a number of days. For example, 8 hours would be converted to 1 day, 12 hours would be converted to 1.5 days, and 18 hours would be converted to 2.25 days. The class should have a constructor that accepts a number of hours, as well as member functions for storing and retrieving the hours and days....

  • Assuming that a year has 365 days, write a class named DayOfYear that takes an integer...

    Assuming that a year has 365 days, write a class named DayOfYear that takes an integer representing a day of the year and translates it to a string consisting of the month followed by day of the month. For example, Day 2 would be January 2. Day 32 would be February 1. Day 365 would be December 31. The constructor for the class should take as parameter an integer representing the day of the year, and the class should have...

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

  • C++ edit: You start with the code given and then modify it so that it does...

    C++ edit: You start with the code given and then modify it so that it does what the following asks for. Create an EmployeeException class whose constructor receives a String that consists of an employee’s ID and pay rate. Modify the Employee class so that it has two more fields, idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, throw an EmployeeException if the hourlyWage is less than $6.00 or over $50.00. Write a program that...

  • House Class Specification The House class represents a house. A house has an address (address instance...

    House Class Specification The House class represents a house. A house has an address (address instance variable), a year it was built (built instance variable), and the names of residents of the house (residents instance variable). The declaration of each variable follows. private String address; private int built; private StringBuffer residents; The class methods are: 1. Constructor - Takes a string (representing the address) and an integer (representing the year the house was built) as parameters, and initializes the corresponding...

  • 8). Name five of the fundamental ter ns which encompass object-oriented programming 9). Write a class...

    8). Name five of the fundamental ter ns which encompass object-oriented programming 9). Write a class called NumberOfGoals that represents the total number of goals scored by a ootball team. The NumberOfGioals class should contain a single integer as data, representing the number of goals scored. Write a constructor to initialize the number of goals to Zero. 10). Write a set of instructions to prompt the user for an int value and input it using the Scanner class into the...

  • Please create a C++ class called myDate. It should have the following operations: myDate() – default...

    Please create a C++ class called myDate. It should have the following operations: myDate() – default constructor. This will set the date to May 10, 1959. myDate(int M, int D, int Y) – overloaded constructor. This will set the date to the values passed in through the parameter list represented by Month, Day and Year. void display() – display the date in the following format (May 11, 1959) Do NOT print a linefeed after the date. void incrDate(int N) –...

  • C++ Question Your class should support the following operations on Fraction objects: • Construction of a...

    C++ Question Your class should support the following operations on Fraction objects: • Construction of a Fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero Fraction. Use default parameters so that you only need a single function to implement all three of these constructors. You should check to make sure that the denominator is...

  • USING BLUEJ: You will write from scratch a class called Heater that represents an adjustable thermostat....

    USING BLUEJ: You will write from scratch a class called Heater that represents an adjustable thermostat. Follow the detailed steps below. This is based on Exercises 2.93 - 2.94 (6e) / 2.92 - 2.93 (5e) in the book, but with slight modifications, so be sure to follow the instructions below. Create a new BlueJ project named LastName-heater using your last name. Create a class named Heater At the top of the source code for Heater, add documentation like this: /**...

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