I'm going to try my best to word my question clearly, I apologize if I do not make sense in my question, I am still very new to java (several weeks in to my first Java class).
Part of my assignment is: "Modify the constructor in the Invoice class, so the day is not greater than 31, 30, or 28, depending on the month. Also if the month is invalid, and thus forced to 0, also force the day to 0."
So in my original program I have this as my month and day methods:
public void setMonth(int month) {
if(month < 1 || month > 12) //if month is less than 1 and greater than 12 force number to 0
this.month = 0;
else
this.month = month;
}
public void setDay(int day) {
if(day < 1 || day > 31) //if day is less than 1 and greater than 31 force to 0
this.day = 0;
else
this.day = day;
}
So I guess where my confusion is, is should I use an 'else if' to make sure the day is not greater than 31, 30, or 28 depending on the month? (also I am not sure if 'else if' is supported in Java? C++ is what I know the most about)
Like:
else if(month = 2 && day > 28)
this.day = 0;
(I know this code is incorrect, it had an error to it when I was typing it). What do you think would be the best way for me to go about making sure the day is not greater than 31, 30, or 28 depending on the month?