Superclass source code
public class Date {
private int month;
private int day;
private int year;
public Date() {
setMonth(1);
**day = 1;**
setYear(1900);
}
public Date(int month, int day, int year) {
this.setMonth(month);
this.**day** = day;
this.setYear(year);
}
Month and Year works fine because I can use the setMonth and setYear in my subclass. However, when I try to use day it says the var is not visible because its private. There is no setter for day in the superclass but there is a getter. What should the setter look like? Furthermore, what should my subclass contructor look like?
Subclass contructor
public EDate(int month, int day, int year)
{
this.setMonth(month);
day = getDay();
this.setYear(year);
}
Subclass Day setter
public void setDay(int newInt) {
if (isGooddDate(getMonth(), newInt, getYear())==true)
{
newInt = this.getDay();
}
Any help is much appreciated!
I dont think there is a need to subclass your Date class in the first place. The reason is because the Date functionality will remain same, no matter what happens. So no need for the subclass constructor.
And as far as date setter goes, you are going in the right direction:
public void setDate(int dateValue) {
if(isDateValid(dateValue)) {
date = dateValue;
} else {
throw new Exception("Invalid date");
}
}
If your class features time support you could actually write a better solution. Convert the date value in time, no matter what the date is. While storing, convert the time into an appropriate date. This is how built-in DateTime class works.
for example: if you store 2006-16-80 it will be stored as 2007-06-19 rather than throwing an exception. Just a thought!