Java

OOP GUIDE / WORK

 

DATE CLASS

 

Topics

  • Advanced class design
  • Constructors
  • Instance methods
  • Encapsulation

 

 

TASK – PART 1 – DATE CLASS: INSTANCE VARIABLES


You will create our own Date class.

 

It has three private instance variables:

 

  • year
  • month
  • day

 

Note: All three instance variables should probably be integers for convenience.

 

 

TASK – PART 2 – DATE CLASS: CONSTRUCTOR


The class has one constructor only.  It receives a value for each of the instance variables. 

 

Pre-condition: You can assume that the variables provided as parameters are a legal date.

 

 

TASK – PART 3 – DATE CLASS: GET METHODS

 

Include the following get methods:

 

  • getDay() – returns the day of the month

 

  • getMonth() – return the month’s number (ie: returns 6 for June)

 

  • getYear() – returns the year

 

  • getMonthName() – returns the month’s name (ie: “June”)

 

  • toString() – returns a String of the date in YYYY-MM-DD format

 

 

TASK – PART 4 – DATE CLASS: MORE DATE FORMATS

 

Also include a way to get the Date in the six common formats below.  You can create six different methods or one method that gets a parameter indicating which format is desired.

 

International (Numeric)

2022-10-15

International (Worded)

2022 October 15

British (Numeric)

15-10-2022

British (Worded)

15 October 2022

American (Numeric)

10-15-2022

American (Worded)

October 15, 2022

 

Note: In Canada, when using numeric formats, we are encouraged to use the international format.  This format is also alpha numerically sortable which is nice if used in filenames.

 

 

TASK – PART 5 – DATE CLASS: ANOTHER INSTANCE VARIABLE

 

Add another instance variable of type String called separator.  The constructor should set it to the hyphen “-“ by default.

 

Provide a get method for the separator instance variable.

 

Provide a setSeparator(String s) method to change the separator.

 

Change your getMethods in the previous parts to use the separator specified in the instance variable instead of only using the hyphen (“-“).

 

 

TASK – PART 6 – TESTING

 

In a Tester class, try creating a few Date objects.  Test each method.  Output to screen all of the different formats.