Having problem getting code to even compile. Instructor insists array be in addDays and no adding of extra data members or methods to the class.
"Use an array of strings in addDays. It needs to be local to the method since that is the only place that it is needed. The data member needs to remain a string. The parameter for addDays is an integer which is the number of days to add or to subtract (if it is a negative number) from the current day.Use the modulus operator to make sure that the array index is in range. The idea is to "convert" or associate the string to/with the index. add (or subtract) the number of days to the index and then return the value of the string in the array cell . addDays takes one integer parameter. The array of strings should be in addDays. plusOneDay should call addDays(1) and return the value it gets back. minusOneDay should call addDays(-1) and return the value it gets back. The array of strings has 7 elements. "Sunday" should be the first one, not " ". " quote from instructor's postings.
You will want to compare the data member (day) with the array of strings to find the index of the current day. Then add the parameter to that index, mod it by 7 and then return the contents of the cell in the array that goes with the new index.
INSTRUCTIONS:
The class has from last week
1. one data member of type string called day.
2. one default constructor that assigns a string(a day of the week - pick your favorite) to day
3. an accessor method called getDay which returns a string and takes no parameters.
4. a mutator method called setDay that take a string as a parameter and returns void
5. a method called printDay that takes no parameters, returns void and outputs day (only) and nothing else.
Additional class methods
1. addOneDay – returns a string and takes no parameters, calls addDays with 1 as a parameter
2. minusOneDay – returns a string and takes no parameters, calls addDays with -1 as a parameter
3. addDays – returns a string and takes one integer parameter, adds a number of days (parameter) to the current day and returns the new day as a string – uses a local array of strings
Main
1. instantiates two objects.
2. Test all operations in the class: setDay, getDay, printDay, addOneDay, minusOneDay, addDays.
Do not add extra data members or methods to the class. All extra variables must be local.
This lab writes a program that contains a class that implements the days of the week.
The program is able to perform the following on an object of the class.
Set the day, Print the day, Return the day, Return the next day, Return the previous day, and
Calculate and return the day by adding a certain amount of days to the current day.
Created/Revised by Barbara Williams on September 8, 2010.*/
/* DaysOfTheWeek.h the specification file for the class DayOfTheWeek
Class "DaysOfTheWeek" contains three public member functions
and one private data member variable */
#include <iostream> // Required by cout command
#include <string> // Required by string data type
using namespace std; // Default namespace defining library functions (e.g. cout, endl)
class DaysOfTheWeek
{
public:
void setDay(string wkDay);
/* A mutator method called setDay that take a string as a parameter and returns void.
Function is to set the day.
The day is set according to the parameter.
Postconditions: day wkDay
The function checks whether the value of wkDay is valid.
If value is invalid the default value is assigned. */
string getDay();
/* An accessor method called getDay which returns a string and takes no parameters.
Function is to return the day.
Postconditions: day wkDay */
void printDay();
/* A method called printDay that takes no parameters, returns void and outputs day (only) and nothing else.
Function is to print the day.
Postconditions: The day is printed in the form of a string.*/
void plusOneDay();
/* An accessor method called plusOneDay that returns a string and takes no parameters,
calls addDays with 1 as a parameter, and increments day plus one.
Function is to add one day to the current day.
Postconditions: day++; */
void minusOneDay();
/* An accessor method called minusOneDay returns a string and takes no parameters,
calls addDays with -1 as a parameter.
Function is to subtract one day from the current day.
Postconditions: day--; */
void addDays();
/* A method called addDays returns a string and takes one integer parameter, adds a number of days (parameter)
to the current day and returns the new day as a string – uses a local array of strings.
Function is to add days to the current day.
Postconditions: */
DaysOfTheWeek();
/* Default constructor
Function is to assign a string(a day of the week)to day.
The day is set to "Sunday"
Postconditions: day = "Sunday" */
DaysOfTheWeek(int);
~DaysOfTheWeek();
private:
string day; // Data member (variable) to store the day.
};/* Ch 12 Lab 2
This lab writes a program that contains a class that implements the days of the week.
The program is able to perform the following on an object of the class.
Set the day, Print the day, Return the day, Return the next day, Return the previous day, and
Calculate and return the day by adding a certain amount of days to the current day.
Created/Revised by Barbara Williams on September 8, 2010.*/
#include <iostream> // Required by cout command
#include <string> // Required by string data type
#include "TheDaysOfTheWeek.h" //Required by class
using namespace std; // Default namespace defining library functions (e.g. cout, endl)
/* DayOfTheWeekImp.cpp, the implementation file
The user program that uses DayOfTheWeek class */
//Definitions of the member functions of the class DayOfTheWeek.
DaysOfTheWeek::DaysOfTheWeek()
{
// sets the data member by default to the first day of the week.
day = "Sunday";
} // end default constructor
DaysOfTheWeek::DaysOfTheWeek(int currentDay)
{
day = currentDay;
currentDay = 2;
}
DaysOfTheWeek::~DaysOfTheWeek()
{
} // end destructor
void DaysOfTheWeek::setDay(string wkDay)
{
//sets the data member literally from what is passed as a parameter
day = wkDay;
} // end setDay mutator method
string DaysOfTheWeek::getDay()
{
// returns the value stored in the data member
return day;
} // end getDay accessor method
void DaysOfTheWeek::printDay()
{
cout << day;
} // end printDay method
void DaysOfTheWeek::plusOneDay()
{
} //end plusOneDay method
void DaysOfTheWeek::minusOneDay()
{
} //end minusOneDay method
void DaysOfTheWeek::addDays()
{
// an array of months
string weekDays [] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
string newDay = weekDays[toDay-1]; //subtract 1 to match the array
day = newDay;
} //end addDays method
/* Ch 12 Lab 2
This lab writes a program that contains a class that implements the days of the week.
The program is able to perform the following on an object of the class.
Set the day, Print the day, Return the day, Return the next day, Return the previous day, and
Calculate and return the day by adding a certain amount of days to the current day.
Created/Revised by Barbara Williams on September 8, 2010.*/
#include <iostream> // Required by cout command
#include <string> // Required by string data type
#include "TheDaysOfTheWeek.h" //Required by class
using namespace std; // Default namespace defining library functions (e.g. cout, endl)
int main()
{
cout << "Days of the week" << endl << endl;
// Instantiate two objects of class DaysOfTheWeek
DaysOfTheWeek dayOne;
DaysOfTheWeek dayTwo;
DaysOfTheWeek dayThree;
// Local variables to hold object value
string myDay = " ";
string day = " ";
// Ask the user for a day of the week
cout << "Enter a day of the week such as Monday, Tuesday etc..." << endl;
cin >> myDay;
cout << endl;
// Set firs day for one object using setDay from user entry.
dayOne.setDay(myDay);
// Set second day using the overloaded setDay using an integer
dayTwo.setDay(3);
cout << "The second day, using an integer, is set as " << dayTwo.getDay() << endl;
cout << endl;
// Set third day using a string variable with setDay
dayThree.setDay("Saturday");
cout << "The third day, using a string variable, is set as " << dayThree.getDay() << endl;
cout << endl;
//Displays for Day 1
cout<<"** Day 1 **"<<endl;
cout << "Today is: ";
dayOne.printDay();
// Displaying the value of minusOneDay.
cout << "Yesterday was: ";
DaysOfTheWeek yesterday = dayOne.minusOneDay();
yesterday.printDay();
// Displaying the value of plusOneDay.
cout << "Tomorrow will be: ";
DaysOfTheWeek tomorrow = dayOne.plusOneDay();
tomorrow.printDay();
// Using the addDays function to display value
cout << "Five Days from today will be: ";
DaysOfTheWeek fiveFurther = dayOne.addDays(5);
fiveFurther.printDay();
cout << endl;
//Displays for Day 2
cout << "** Day 2 **" << endl;
cout << "Today is: ";
dayTwo.printDay();
// Displaying the value of minusOneDay.
cout << "Yesterday was: ";
yesterday = dayTwo.minusOneDay();
yesterday.printDay();
// Displaying the value of plusOneDay.
cout << "Tomorrow will be: ";
tomorrow = dayTwo.plusOneDay();
tomorrow.printDay();
// Using the addDays function to display value
cout << "Two Days from today will be: ";
DaysOfTheWeek twoDaysFurther = dayTwo.addDays(2);
twoDaysFurther.printDay();
cout << endl;
//Displays for Day 3
cout << "** Day 3 **" << endl;
cout << "Today is: ";
dayThree.printDay();
// Displaying the value of minusOneDay.
cout << "Yesterday was: ";
yesterday = dayThree.minusOneDay();
yesterday.printDay();
// Displaying the value of plusOneDay.
cout << "Tomorrow will be: ";
tomorrow = dayThree.plusOneDay();
tomorrow.printDay();
// Using the addDays function to display prior day's (minus) value
cout << "Three Days prior from today will be: ";
DaysOfTheWeek priorDays = dayThree.addDays(-3);
priorDays.printDay();
cout << endl;
return 0;
} //end main