COMP503/ENSE502/ENSE602 Week 4 - Exercises
Exercise 07: Simple Date
Create the Java class called SimpleDate and complete the class with attributes int day; int month; int year; using the private accessor modifier. Write get methods for each attribute and write set methods for the instance variables such that:
- day can only be set to a value between 1 and 31
- month can only be set to values 1 to 12
- year is a value between 2000 and the current year. If these conditions are not met by the set method's input parameters, choose an appropriate default value to set the instance variable to instead.
Write a method called setDate(int day, int month, int year) that sets the instance variables using the set methods you've written.
Use the setDate method to write a constructor with the method signature SimpleDate(int day, int month, int year). Choose some appropriate initial values for the SimpleDate attributes and use these values to write the default constructor.
Write a toString method returning a string of the form "DD/MM/YYYY". To construct this string, use String concatenation (+) to form the required output.
Create a program called SimpleDateApplication.java with a main method that tests the functionality of each SimpleDate method. For example, consider the following code snippet:
SimpleDate d1 = new SimpleDate();
d1.setDay(22);
d1.setMonth(3);
d1.setYear(2016);
System.out.println(d1);
SimpleDate d2 = new SimpleDate(14, 03, 2016);
System.out.println(d2);