473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Code and test a class for the date: Please help

I'm having a little trouble will my coding, I'm suppose to set the methods with three parameters that returns a bool. Next assign the parameter to the properties and return a true if all three parameters are valid. Then assign zeros to the porperties and return a false if any/all of the parameters are invalid.

1st parameter represents the month number (1-12)
2nd parameter represents the day of month, day must be valid based on the month.
3rd parameter represents the years:
( if the year is between 0 and 99, its valid;
if the year is between 1800 and 2030, it is valid;
if the year is between 0 and 29, store it as 2000-2029;
if the year is between 30 and 99, store it as 1930-1999)

Here's my coding so far:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class MyDate
  5. {
  6. private:
  7.     int Month;
  8.     int Day;
  9.     int Year;
  10. public:
  11.     int getMonth()
  12.     { 
  13.         return Month;
  14.     }
  15.     int getDay()
  16.     { 
  17.         return Day;
  18.     }
  19.     int getYear()
  20.     { 
  21.         return Year;
  22.     }
  23.     void set(int Month_param, int Day_param, int Year_param)
  24.     {
  25.         Month = Month_param;
  26.         Day = Day_param;
  27.         Year = Year_param;
  28.  
  29.  
  30.     }
  31. };
  32.  
  33.  
  34.     int main()
  35. {
  36. return 0;
  37. }
Oct 2 '07 #1
5 1552
If I understood the directions correctly you have to make the return type of your "set" function be a bool.

Inside the set function you have to check if each the parameters are valid. Right now, you don't have any checks in there.
Oct 2 '07 #2
If I understood the directions correctly you have to make the return type of your "set" function be a bool.

Inside the set function you have to check if each the parameters are valid. Right now, you don't have any checks in there.
How do I go about checking to see if the parameters are valid, do I have to do if statements to find that out or what?
Oct 2 '07 #3
How do I go about checking to see if the parameters are valid, do I have to do if statements to find that out or what?
Correct

So you might have something like this

Expand|Select|Wrap|Line Numbers
  1. bool set(int Month_param, int Day_param, int Year_param) 
  2. {
  3. <spoonfeed code removed}
  4.  
Does that make sense?

And also, don't forget to do this:
Then assign zeros to the porperties and return a false if any/all of the parameters are invalid.
Oct 2 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Consider your class design. A valid date must have the month between 1 and 12 and the day between 1 to 31 (or 30 or 29 or 28) and a year valid for the Gregorian calendar. Most classes like this establish an arbitrary minimum year.

For the date object to be consistent, you cannot have a method that just accepts a month because that can affect the day that is already in the object (and if it's not already in the object, then you have other problems).

So, your set method should have three arguments and it shoujld return a bool to indicate whether the three arguments constitute a valid date.

This will apply to your constructors. You will need one with three arguments.

If you have a default constructor (no arguments) then that constructor will have to establish a valid default value for the month, day and year (and zeros or -1 are the wrong answer).

The get methods that return the month or the day or the year are OK for starters.
Oct 3 '07 #5
Correct

So you might have something like this

Expand|Select|Wrap|Line Numbers
  1. bool set(int Month_param, int Day_param, int Year_param) 
  2. {
  3. <spoonfeed code removed}
  4.  
Does that make sense?

And also, don't forget to do this:
If you have any more advice or examples to give on the topic please do I need all the help there is.
Oct 5 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: James | last post by:
Please help - getting very desperate! Sun, 12 October 2003 05:39 I have PHPDEV 4.2.3 from Firepages.com.au as the upgrade to 4.3.0 did not work. I also had an abortive download from PHP.NET as...
0
by: Dafella | last post by:
The following is code from Xeoport. It is suppose to access my pop3 account and load email into Mysql database. It's not inserting and there is no log or anything to tell me why. Here is the...
8
by: Clifton M. Bean | last post by:
First, I defined three classes (listed below): =========== // 1st class =========== class PointCl { public: PointCl & operator= (const PointCl & rgh ); //define as usual assingment operator
7
by: sck10 | last post by:
Hello, I have the following sub in a class in my "App_Code" directory. The script is for setting focus on a particular control, but I get the error, "Name ClientScript Not declared". Also, I am...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
9
by: Jamiil | last post by:
This is not a question where the source code is important, it is not important at all. So don't expect to find answers to my questions in the code; the answers will be in how the class interacts...
17
by: Liam.M | last post by:
Hey guys, Forgive me if my question my be alittle silly, but I would very much appreciate and assistance that could be given! My situation is as follows: I have created a Button, and set...
2
by: Netkiller | last post by:
#!/usr/bin/python # -*- coding: utf-8 -*- """ Project: Network News Transport Protocol Server Program Description: 基于数据库的新闻组,实现BBS前端使用NNTP协议来访问贴子...
2
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.