Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Design and implement a class dayType that implements the day of the week in a program. The class dayType should store the day, such as

Design and implement a class dayType that implements the day of the week in a program. The class dayType should store the day, such as Sunday for Sunday. The program should be able to perform the following operations on an object of type dayType:

Step a: Set the day.

Step b: Print the day.

Step c: Return the day.

Step d: Return the next day.

Step e: Return the previous day.

Step f: Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add 4 days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.

Step g: Add the appropriate constructors.

Write the definitions of the functions to implement the operations for the class dayType.

** The only problem I'm having with is Step F. I am using an online program so the program wants us to use a void function, but I have it as a string function, which works but the program is failing me simply because it is not a void function. Please help! Following are my codes, plus some hints the program provided, and I am still lost after that. Thank you!

//dayType.cpp

// dayType.cpp

#include

#include

#include "dayType.h"

using namespace std;

// array for days of the week

string weekDays[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

// part b

void dayType::print() const

{

cout << weekDay;

}

// part d

string dayType::nextDay() const

{

string nextDay;

if (weekDay == "Monday")

nextDay = "Tuesday";

if (weekDay == "Tuesday")

nextDay = "Wednesday";

if (weekDay == "Wednesday")

nextDay = "Thursday";

if (weekDay == "Thursday")

nextDay = "Friday";

if (weekDay == "Friday")

nextDay = "Saturday";

if (weekDay == "Saturday")

nextDay = "Sunday";

if (weekDay == "Sunday")

nextDay = "Monday";

return nextDay;

}

// part e

string dayType::prevDay() const

{

string prevDay;

if (weekDay == "Monday")

prevDay = "Sunday";

if (weekDay == "Tuesday")

prevDay = "Monday";

if (weekDay == "Wednesday")

prevDay = "Tuesday";

if (weekDay == "Thursday")

prevDay = "Wednesday";

if (weekDay == "Friday")

prevDay = "Thursday";

if (weekDay == "Saturday")

prevDay = "Friday";

if (weekDay == "Sunday")

prevDay = "Saturday";

return prevDay;

}

// part f

string dayType::addDay(int nDays)

{

string weekDays[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

int index;

for(int i = 0; i < 7; i++)

{

if (weekDay == weekDays[i])

{

index = i;

break;

}

}

return weekDays[ (index + nDays) % 7 ];

}

// part a

void dayType::setDay(string d)

{

string weekDays[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

if (d == "Monday")

weekDay = weekDays[1];

if (d == "Tuesday")

weekDay = weekDays[2];

if (d == "Wednesday")

weekDay = weekDays[3];

if (d == "Thursday")

weekDay = weekDays[4];

if (d == "Friday")

weekDay = weekDays[5];

if (d == "Saturday")

weekDay = weekDays[6];

if (d == "Sunday")

weekDay = weekDays[0];

}

// part c

string dayType::getDay() const

{

return weekDay;

}

// part g

dayType::dayType()

{

weekDay = "Sunday";

}

// part g

dayType::dayType(string d)

{

setDay(d);

}

//dayType.h

#ifndef H_dayType #define H_dayType #include

using namespace std;

class dayType { public: static string weekDays[7]; void print() const; string nextDay() const; string prevDay() const; string addDay(int nDays); void setDay(string d); string getDay() const;

dayType(); dayType(string d);

private: string weekDay; };

#endif

//main.cpp

#include #include #include #include "dayType.h" using namespace std;

int main() { dayType myDay("Monday"); dayType temp("Sunday"); dayType myDay2("Friday");

myDay.print(); cout << endl;

cout << myDay.prevDay() << endl;

cout << myDay.nextDay() << endl; cout << endl; temp.print(); cout << endl;

cout << temp.prevDay() << endl;

cout << temp.nextDay() << endl; cout << endl; cout << myDay2.addDay(3) << endl; cout << myDay.addDay(4) << endl; return 0; }

Here's what the program wants, but I'm lost on where to implement it into my code: (it says its the program code/logic)

TEST(Day, 1) { dayType myDay("Friday"); myDay.addDay(3); ASSERT_TRUE(myDay.getDay() == "Monday"); } 
TEST(Day, 1) { dayType myDay("Monday"); myDay.addDay(4); ASSERT_TRUE(myDay.getDay() == "Friday"); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

More Books

Students also viewed these Databases questions