Question

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 a member function print() that prints the day in the month–day format. The class should have an integer member variable to represent the day and should have static member variables holding string objects that can be used to assist in the translation from the integer format to the month-day format. Test your class by inputting various integers representing days and printing out their representation in the month–day format.

I don't what is wrong with my code, could anyone help me solve this problem? Thanks a lot!

// This program is to

#include <iostream>

#include <string>

#include <iomanip>

using namespace std;

static unsigned int MONTH_DAY[] = { 31, 59, 90,

120, 151, 181,

212, 243, 273,

304, 334, 365};

static string MONTH_NAME[] = { "January", "February", "March",

"April", "May", "June",

"July", "August", "September",

"October", "November", "December"};

class DayOfYear

{

private:

unsigned int day;

public:

// Constructor

DayOfYear(unsigned int d = 0)

{ this -> day = d; }

static unsigned int MONTH_DAY[];

static string MONTH_NAME[];

void print()

{

unsigned int month = 0;

while (MONTH_DAY[month] < this -> day)

{

month ++;

}

unsigned int d = this -> day - MONTH_DAY[month];

cout << MONTH_DAY[month] << " " << d << endl;

}

};



int main()

{

unsigned int day;

const unsigned int FIRST_DAY = 1;

const unsigned int LAST_DAY = 365;

cout << "Please enter the day: " << day;

cin >> day;

while (day < FIRST_DAY || day > LAST_DAY)

{

cout << "Invalid, please re-enter: ";

cin >> day;

}

DayOfYear day_of_year(day);

day_of_year.print();

return 0;

}

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

Hi,

The problem has been resolved. Some changes has been done in already written code. Now the code is working find. I'm attaching the screenshot of running code with expected output and the changes in code have been marked in bold to identify. Please refer to the below image.

Screenshot:

You are defining "MONTH_DAY" and "MONTH_NAME" twice in code so i have removed one of them from "below Constructor define".

Code:

---------------------

#include <iostream>

#include <string>

#include <iomanip>

using namespace std;

static unsigned int MONTH_DAY[] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};

static string MONTH_NAME[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};


class DayOfYear

{

private:

unsigned int day;

public:

// Constructor

DayOfYear(unsigned int d = 0)

{
this -> day = d;
}

void print()

{

unsigned int month = 0;
//cout<<"day: "<<day<<endl;
unsigned int daynew = day;

while (MONTH_DAY[month] < day)

{

month ++;

}
if(month!=0)
   daynew= daynew-MONTH_DAY[month-1];

cout<<MONTH_NAME[month]<<" "<<daynew<<"\n\n";

}

};


int main()

{

unsigned int day;

const unsigned int FIRST_DAY = 1;

const unsigned int LAST_DAY = 365;

cout << "Please enter the day: \n";

cin >> day;

while (day < FIRST_DAY || day > LAST_DAY)

{

cout << "Invalid, please re-enter: ";

cin >> day;

}

DayOfYear day_of_year(day);

day_of_year.print();

return 0;

}

--------------------

The problem has been resolved, I hope you will like the solution. If you have any question or query regarding this problem or other, please comment below and give the positive rating.

Thank You!! Happy Learning!! Keep Chegging!!

Add a comment
Know the answer?
Add Answer to:
Assuming that a year has 365 days, write a class named DayOfYear that takes an integer...
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
  • 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...

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

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

  • USE C++. Just want to confirm is this right~? Write a class named RetailItem that holds...

    USE C++. Just want to confirm is this right~? Write a class named RetailItem that holds data about an item in a retail store. The class should have the following member variables: description: A string that holds a brief description of the item. unitsOnHand: An int that holds thw number of units currently in inventory. price: A double that holds that item's retail price. Write the following functions: -appropriate mutator functions -appropriate accessor functions - a default constructor that sets:...

  • This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a fun...

    This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a function and three overloaded operators. You don't need to change anything in the Multiplex.h file or the main.cpp, though if you want to change the names of the movies or concession stands set up in main, that's fine. Project Description: A Multiplex is a complex with multiple movie theater screens and a variety of concession stands. It includes two vectors: screenings holds pointers...

  • C++. here is the problem that kind of the same with the example that i did...

    C++. here is the problem that kind of the same with the example that i did in class (attach picture). however, this require use the loop to do, I get confuse. pls help me! thank you! Problem: Define a class to implement the time of day in a program. To represent time, use three member variables: one to represent the hours, one to represent the minutes, and one to represent the seconds. Also define the following member functions in the...

  • its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task an...

    its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task and Appointment which inherit from the Event class The Event Class This will be an abstract class. It will have four private integer members called year, month, day and hour which designate the time of the event It should also have an integer member called id which should be a...

  • //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards...

    //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards myDeckOfCards; for (int i = 0; myDeckOfCards.moreCards() && i < count; ++i) { std::cout << std::left << std::setw(19) << myDeckOfCards.dealCard().toString(); if (i % 4 == 3) std::cout << std::endl; } } int main() { RunAllTests(); return 0; } //card.hpp #ifndef CARD_HPP_ #define CARD_HPP_ #include <string> class Card { public: static const int totalFaces = 13; static const int totalSuits = 4; Card(int cardFace, int...

  • for the following code I need the source code and output screenshot (includes date/time) in a...

    for the following code I need the source code and output screenshot (includes date/time) in a PDF format. I keep getting an error for the #include "dayType.hpp" dayType.cpp #include"dayType.hpp" string dayType::weekday[7] = {"Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" }; //set the day void dayType::setDay(string day){ cout << "Set day to: " ; cin >> dayType::day; for(int i=0; i<7; i++) { if(dayType::weekday[i]==dayType::day) { dayType::markDay = i; } } } //print the day void dayType::printDay() { cout << "Day = "...

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

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