[homework] Method to calculate age given code that creates date objects
This is all relating to Java.
My teacher provided us with the code posted below and the assignment "Write a method to calculate a display persons age if he or she is celebrating a birthday." I've already attempted a solution but want to verify if it is correct or not, and what I can do to improve.
I apologize for the first few lines of code being outside of the gist, this is my first time using it so I am unsure what went wrong.
public class FirstDate {
private String month;
private int day;
private int year;
public FirstDate() { //Constructor
month = "valid month";
day = 1;
year = 1900;
}
public void setDate(int month, int day, int year) //Sets date to what is inputted
{
this.month = monthName(month);
this.day = day;
this.year = year;
}
public String monthName (int month) { //Returns the name of the month
switch (month) {
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
System.out.println("Fatal Error");
System.exit(0);
return "Error";
}
}
public String getMonth() { //Getter
return this.month;
}
public int getDay() { //Getter
return this.day;
}
public int getYear() { //Getter
return this.year;
}
public boolean equals (FirstDate birthday) {
return ((month.equals(birthday.month)) && (day == birthday.day));
}
public void displayOutput()
{
System.out.println(getMonth() + " " + getDay() + ", " + getYear());
//System.out.println(month + " " + day + " " + year);
}
public int age (FirstDate user) {
if (equals(user) == true)
{
return this.year - user.year;
}
else {
System.out.println("It's not their birthday; age cannot be calculated.");
return 0;
}
}
public static void main(String[] args) {
}
}
The "age" method is my own attempt; everything else is the code provided by my teacher.
Posted Oct 6, 2016, 9:06 PM UTC · edited
For the formatting, adding four spaces to the beginning of each line allows code to be formatted correctly:
As for the assignment, you've done what it's asked of you. If the user is celebrating a birthday the age is returned, else a message is printed out.
As for improvements:
can be replaced with
and - for clarity sake - I think the
userargument would make more sense being calledbirthdayto be consistent with theequals()method.Besides that, I think adding a few test-cases in the
main()method is all that's left to do.When you say four spaces at the beginning of each line, do I have to manually go in and hit spacebar four times for every single line?
In terms of the code, thanks for the help. Those clarification suggestions definitely improve documentation and also eliminate the one redundancy.
Thanks a bunch, it means a lot to a new guy like me.
I personally use a site like this one to prefix each line with the spaces.
You're welcome and good luck!