0% found this document useful (0 votes)
16 views

Week 01 Lab 03 Date

The document describes a Date class with fields for year, month, and day. It includes a constructor, ToString method, and Add methods to modify the date. The Add methods call a Normalize method to keep the fields within valid ranges by adjusting months and years. The document provides examples of creating a Date object, calling the Add methods, and potential enhancements like handling different month lengths.

Uploaded by

ulfany.guimel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Week 01 Lab 03 Date

The document describes a Date class with fields for year, month, and day. It includes a constructor, ToString method, and Add methods to modify the date. The Add methods call a Normalize method to keep the fields within valid ranges by adjusting months and years. The document provides examples of creating a Date object, calling the Add methods, and potential enhancements like handling different month lengths.

Uploaded by

ulfany.guimel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Programming II Date: methods

Creating a Date class

We will assume that there are 12 months in a year and each month is 30 days long. In this course,
sometimes it is more practical to ignore certain details, that would otherwise overly complicate your
model.

Date
Class
Fields
- year : int
- month : int
- day : int
Methods
+ «constructor» Date(year : int, month : int, day : int)
+ ToString() : string
+ Add(days : int) : void
+ Add(month : int, days : int) : void
+ Add(other : Date) : void
- Normalize() : void

Description of field members:


1. year: this private member represents the year value of this object
2. month: this private member represents the month
value of this object. You may interpret the months Alternately you may choose to have 0 =
January, 1 = February etc.. This would
as you normally do i.e. 1 = January, 2 = February
be my personal preference.
3. day: this private member represents the day value
of this object

Description of constructor:
1. public Date(int year, int month, int day): this is the constructor of this class. It
takes three integer arguments and assigns them to the appropriate fields. You will assume that
all the arguments will be in the correct range.

Description of action members:


1. public override string ToString(): this method overrides the same method in the
base class. It does not take any argument but it returns a string representing this object.

Page 1 of 2
Programming II Date: methods

2. public void Add(int days): this method takes a single integer argument representing the
amount to increase the day field by and adds it to the appropriate field.
3. public void Add(int months, int days): this method takes two integer arguments
representing the amount to increase the month field and the day field. It adds the argument to
the appropriate fields.
4. public void Add(Date other): this method takes a single integer argument representing
a date. The is actually three values packed into a single date object. You will have to separate
each packed value and increase the year, month and day fields by the appropriate amount.
5. private void Normalize(): this method does not take any argument nor does it return a
value. It does the following:
a. If the days field is more than the number of days in a month, then it subtracts the number
of days in the month from the day field and increases the month field by one.
b. If the month field is more than 12 then, 12 is subtracted from the month field and the year
field is increased by 1.

This method should be called at the end of each Add method.

In your main method write the code to do the following:


1. Create a Date object. You decide on the arguments.
2. Display the above object reference on the console.
3. Call the Add() method of the object reference to verify that the method works correctly. You
will need to invoke this method three time will different number and types of argument because
this method is overloaded three times. (How would you display the modified object?).
4. What would you add to the class to make the display more sensible? i.e. the day and month
value should be in range

Enhancements:
1. Display the text for the month i.e. Jan or January for the month 1.
2. Handle the different month length properly
"30 days has September, April, June, and November. All the rest have 31. Save
February, with 28 days clear, and 29 each leap year."
3. Would it be better to have a single int field that represents the number of days passed since the
year has started? Now all the operations are easier to implement excepting the ToString
method.

Page 2 of 2

You might also like