I am doing some basic Java programming where i have to create classes which have constructors linked to other class constructors. For example, refer to my following code…
public class Friend {
private String name;
private Date dateOfBirth;
private String phoneNum;
public Friend(String name){
this.name=name;
this.dateOfBirth = null;
}
public Date setDOB(Date input){
return dateOfBirth;
}
}
public class Date {
final int MINDAYSINMONTH=1;
final int MAXDAYSINMONTH=30;
final int MINMONTHSINYEAR=1;
final int MAXMONTHSINYEAR=12;
private int day;
private int month;
private int year;
//constructor
public Date(int day, int month, int year){
this.day=day;
this.month=month;
this.year=year;
}
}
I am trying to create a new Friend, then alter the dateOfBirth value that is within the Friend class, like so…
Friend trial = new Friend(input);
trial.setDOB(new Date(2, 15, 1991));
But my output suggests that i have created a new Friend but the dateOfBirth didn’t change to the values that I supplied above. Could someone please help me understand what I’m doing wrong.
Your
setDOBmethod is wrong – it isn’t actually setting anything at all.Try changing this…
You should probably also have a getter method so you can check the value after it has been set…