Computing.Net > Forums > Programming > Help creating class day

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help creating class day

Reply to Message Icon

Name: bluesmiley
Date: April 12, 2005 at 22:12:33 Pacific
OS: Windows XP Professional
CPU/Ram: 56mbps
Comment:

I am trying to make this program that does the following:
implements the class Day that implements the day of the week in a program. The class day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of the type Day:
a. Set the day
b. Print the day
c. Return the day
d. Return the next day
e. Return the previous day
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.
g. Add the appropriate constructors.
h. Write the definition of the methods to implement the operations for the class Day as defined in a-g.
i. Write a program to test various operations on the class Day.
Here is what i have so far, i can't get any further i need help.
public class Day
{
private String sun, mon, tue, wed, thur, fri, sat;

public void Day(String s, String m, String t, String w, String x, String f, String a)
{
sun = s;
mon = m;
tue = t;
wed = w;
thur = x;
fri = f;
sat = a;
}

public void Day()
{
String s, m, t, w, x, f, a;
s = "sunday";
m = "monday";
t = "tuesday";
w = "wednesday";
x = "thursday";
f = "friday";
a = "saturday";
}

public String printDay(String s, String m, String t, String w, String x, String f, String a)
{
print.Day();
}

public String ToDay(String day)
{
String day;
day = Day();
return day;
}

public String nextDay(String day)
{
return day++;
}
public String previousDay(String day)
{
return day--;
}
}

"you can't implement me, i'm outside your scope!"




Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: April 13, 2005 at 09:40:43 Pacific
Reply:

well that's all kinds of messed up, isn't it?

you cannot return from a void.

let me help you out.

#include <string>
using namespace std;

class Day
{
private:
string Days[7];
int TodayIndex;
public:
Day();
Day(string WeekDays[]);
~Day();
void SetDay(string WeekDay);
void SetDay(int DayOfWeek);
string Today(void);
string Yesterday(void);
string Tomorrow(void);
string operator +(int Number);
string operator -(int Number);
} ;

Day::Day()
{
TodayIndex = 0;
}
Day::Day(string WeekDays[])
{
TodayIndex = 0;
for(int d=0;d<WeekDays->length();d++)
Days[d] = WeekDays[d];
}
Day::~Day()
{
}
void Day::SetDay(string WeekDay)
{
for(int d=0;d<7;d++)
{
if(Days[d] == WeekDay)
TodayIndex = d;
}
}
void Day::SetDay(int DayOfWeek)
{
if(DayOfWeek > -1 || DayOfWeek < 7)
TodayIndex = DayOfWeek;
}
string Day::Today(void)
{
return Days[TodayIndex];
}

string Day::Yesterday(void)
{
int y = (TodayIndex-1)%7;
return Days[y];
}

string Day::Tomorrow(void)
{
int t = (TodayIndex+1)%7;
return Days[t];
}
string Day::operator+ (int Number)
{
TodayIndex += Number%7;
return Days[TodayIndex];

}
string Day::operator- (int Number)
{
TodayIndex -= Number%7;
return Days[TodayIndex];

}

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


Hope this helps some,
Chi

They mostly come at night...mostly


0

Response Number 2
Name: Chi Happens
Date: April 13, 2005 at 09:56:37 Pacific
Reply:

ok mine has problems too. i guess i should have tested first. i forgot a few things:

try this:

-------------------
#include <string>
#include <iostream>

using namespace std;

class Day
{
private:
string Days[7];
int TodayIndex;
public:
Day();
Day(string WeekDays[]);
~Day();
void SetDay(string WeekDay);
void SetDay(int DayOfWeek);
string Today(void);
string Yesterday(void);
string Tomorrow(void);
Day operator +(int Number);
Day operator -(int Number);
} ;

Day::Day()
{
Days[0] = "MON";
Days[1] = "TUE";
Days[2] = "WED";
Days[3] = "THU";
Days[4] = "FRI";
Days[5] = "SAT";
Days[6] = "SUN";
TodayIndex = 0;
}
Day::Day(string WeekDays[])
{
TodayIndex = 0;
for(int d=0;d<WeekDays->length();d++)
Days[d] = WeekDays[d];
}
Day::~Day()
{
}
void Day::SetDay(string WeekDay)
{
for(int d=0;d<7;d++)
{
if(Days[d] == WeekDay)
TodayIndex = d;
}
}
void Day::SetDay(int DayOfWeek)
{
if(DayOfWeek > -1 || DayOfWeek < 7)
TodayIndex = DayOfWeek;
}
string Day::Today(void)
{
return Days[TodayIndex];
}

string Day::Yesterday(void)
{
int y = (TodayIndex-1)%7;
return Days[y];
}

string Day::Tomorrow(void)
{
int t = (TodayIndex+1)%7;
return Days[t];
}
Day Day::operator+ (int Number)
{
Day temp;
temp.SetDay(TodayIndex);
temp.TodayIndex += Number%7;
return temp;

}
Day Day::operator- (int Number)
{
Day temp;
temp.SetDay(TodayIndex);
temp.TodayIndex -= Number%7;
return temp;

}

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

#pragma argsused
int main(int argc, char* argv[])
{
Day mDay;
cout << mDay.Today() << endl;
mDay = mDay + 1;
cout << mDay.Today() << endl;
char dummy;
cin >> dummy;
return 0;
}
-------------------


Chi

They mostly come at night...mostly


0

Response Number 3
Name: SteveWalsh
Date: April 13, 2005 at 12:04:04 Pacific
Reply:

homework?


0

Response Number 4
Name: bluesmiley
Date: April 13, 2005 at 20:37:06 Pacific
Reply:

haha yeah i know it was messed up. thanks but i dont want to use arrays i just want to use maybe an if else loop or for loops. i know you can't return in a void method, i wrote that about a month ago and havent modified it just thought i would get some input. maybe you can take my program and just add and change a few things? thank you so much.

"you can't implement me, i'm outside your scope!"


0

Response Number 5
Name: Chi Happens
Date: April 15, 2005 at 08:08:38 Pacific
Reply:

"maybe you can take my program and just add and change a few things?"

nah, thanks for the offer, but mine works well.

Chi

They mostly come at night...mostly


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

java ide jsp front end creation



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help creating class day

help with classes in c++ www.computing.net/answers/programming/help-with-classes-in-c/11749.html

VBScript help creating a log file www.computing.net/answers/programming/vbscript-help-creating-a-log-file-/14414.html

Need help about Classes interface www.computing.net/answers/programming/need-help-about-classes-interface/6016.html