CSE2305 - Object-Oriented Software Engineering
exercises

CSE2305
Object-Oriented Software Engineering

Exercise 2: The Date Class

This exercise is designed to reinforce the concepts discussed in the lectures. There are no marks for completing this exercise, however your attendance at tutorials and successful completion is officially recorded and may be used as a positive adjunct to assessment in special circumstances.

You are strongly advised to compete the exercise as it will help you with the concepts discussed in lectures and assist you in successfully completing the practical work. Exercises often mysteriously reappear as exam questions.


Topic: Develop a simple Date class in C++

Purpose: a chance to develop understanding of basic object-oriented programming using C++.

How to Proceed:

  1. Programmers often develop a "kit bag" of useful classes that they find they need for almost every project. Here is your chance to begin your own collection and learn C++.
  2. For this exercise, you are to design and implement a simple class whose purpose is to represent a date.
  3. Dates are expressed in a number of different formats, e.g. 27/3/02, 27 March 2005, Wednesday, March 27, 2005. Different countries and cultures use different formats and ordering for dates (e.g. the North American format is MM/DD/YY or MM/DD/YYYY). A date entered in one format should work for another (i.e. the Date class should encapsulate the date without relying on a specific representation).
  4. Clearly we need to distinguish between internal representation and formats we can display the date in.
  5. Remember Y2K? What can we learn from this regarding dates – how far into the future will your Date class work? How far back in the past?
  6. Please come to the tutorial with a written design for the methods and data members of your class. Priority for help will be given to students who have completed this before the tute begins.
  7. Make sure your Date class has the following abilities:

Hints:

  1. You may be interested in the UNIX library calls gettimeofday(2), time(3) and ctime(3).
  2. Keep your internal representation private and use accessor methods to get and set components of the date.
  3. Sophisticated date formats use metacharacters to prototype the format, e.g. myDate.setFormat("%D/%M/%Y") so that when the date is printed it follows the specified format (see the format options for the UNIX date(1) command as an example). You could try this or alternatively, have a series of preset formats that are set by index or name, (e.g. myDate.setFormat(aus_date_format) )
  4. A nice way to handle things global to a class is to use static class methods and data members.


This material is part of the CSE2305 – Object-Oriented Software Engineering course.
Copyright © Jon McCormack, 2005.  All rights reserved.

Last modified: August 5, 2005