[homework] Method to calculate age given code that creates date objects

Homework

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

29
Rascal_Two•

For the formatting, adding four spaces to the beginning of each line allows code to be formatted correctly:

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) {
		
	}

}

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:

if (equals(user) == true)

can be replaced with

if (equals(user))

and - for clarity sake - I think the user argument would make more sense being called birthday to be consistent with the equals() method.

Besides that, I think adding a few test-cases in the main() method is all that's left to do.

3
LankeiOP•

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.

2
Rascal_Two•

I personally use a site like this one to prefix each line with the spaces.

You're welcome and good luck!

2
BernoulliRandom•

Like Rascal_Two said, it looks like you completed the assignment to satisfaction. But, here are some thing that you could improve.

  1. Stylistically, your if/else looks weird; your if and else follow two separate coding styles. To make your code look "prettier" you might want to format it like one of the two code blocks below. It looks like the rest of your code could also be "beautified."

     if (condition) {
         // do stuff
     }
     else {
         // do stuff
     }
    
     if (condition) 
     {
         // do stuff
     }
     else 
     {
         // do stuff
     }
    
  2. Some people discourage early termination. Don't ask me why, but I get bitched out for doing it in code reviews. Consider doing something like:

     public int age (FirstDate user) {
    
         int retval = this.year - user.year;
         if (!equals(user)) {
             System.out.println("It's not their birthday; age cannot be calculated.");
             retval = 0
         }
    
         return retval;
     }
    

Edit:

Also, what happens when this.year = user.year (e.g. someone was just born)? How will you differentiate between someone who is a new born vs someone who has no birthday?

3
throwaway57201•

I worked for a company that absolutly hated the if statements going like: if(*){ enter }

instead of if() enter { }*

Personally I like the latter. Easier for me to debug bracing. Although that should be something so trivial to miss, that it shouldn't.

2
BernoulliRandom•

I prefer the second way too. For me, it adds consistency to white space, it also makes it easier for me to read because I can line up braces.

2
LankeiOP•

Thanks for the help, I truly appreciate it. I'll go adjust the curly braces for stylistic purposes.

Additionally, I changed the return 0; to a return -1; because like you said, theoretically a newborn could be valid, and -1 helps to indicate that something went wrong with processing.

When you say early termination, do you mean the fact that if the if statement's condition is met the code will end before the else is considered? As in, the return statement occurs within the if statement rather than after it?

1
BernoulliRandom•

Correct. It's pretty trivial.

2
LankeiOP•

Given my teacher's scrupulous nature and their heavy experience in the CS world it may be that they care about it after all. Plus it's always useful to learn about industry practice.

1