Classes and Data Abstraction Assignment

I need help with my assignment for class. Please note I am not asking anyone to do the work for me, just asking for hints. Here is the assignment:

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.

I need help with the addDay function. I am using an online learning tool my professor uses called MindTap. MindTap is very picky, and even though the code works, it still says the test has failed to add 3 days to Friday and 4 days to Monday. MindTap wants me to make the addDay function a void function. Any suggestions to how to make the function work as a void function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//dayType.cpp
#include <iostream>
#include <string>

#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 - I NEED HELP WITH THIS FUNCTION
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] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
if (d == "Monday")
    weekDay = weekDays[0];
if (d == "Tuesday")
    weekDay = weekDays[1];
if (d == "Wednesday")
    weekDay = weekDays[2];
if (d == "Thursday")
    weekDay = weekDays[3];
if (d == "Friday")
    weekDay = weekDays[4];
if (d == "Saturday")
    weekDay = weekDays[5];
if (d == "Sunday")
    weekDay = weekDays[6];
}

// 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 <string>  

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 <iostream>
#include <string>
  
#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;
}
a void adder might look like

void addto(type &in, type &value)
{
in += value;
}

or

void addtwo(type &result, type &value1, type &value2)
{
result = value1+value2;
}

...
as for adding up days, this begs using a modulus function and an enum
enum days{su,mo,tu,wd,th,fr,sa,max_days};

someday = (someday+ numdays) % max_days;
becomes, for fr + 4
fr = 5.
5 + 4 = 9.
max days is 7.
9 % 7 is 2.
2 is tu.
fr, sat(+1), su(+2), mo(+3), tu(+4) right?

something like that?


Topic archived. No new replies allowed.