Hello guys, long time no see!
i've put together a very small simple console base application that takes a date and a few strings and print them off.
Let's look at the code first.
Date.java
/*Date.java
this class represents dates
*/
public class Date{
private int month;
private int day;
private int year;
//constructor
public Date(int dayDate, int monthDate, int yearDate){
day = checkDay(dayDate);
month = checkMonth(monthDate);
year = yearDate;//no validation
System.out.printf(
"Date is %d-%d-%d\n",getDay(),getMonth(),getYear()
);
}
public int getDay(){//getter
return day;
}
public int getMonth(){
return month;
}
public int getYear(){
return year;
}
private int checkDay(int testDay){//test day
if((testDay <= 31) || (testDay > 0)){//if between 1 and 31 it's ok
return testDay;
}
else{//if not
throw new IllegalArgumentException("day must be between 1 and 31");//throw exception
}
}
private int checkMonth(int testMonth){
if((testMonth <= 12) || testMonth > 0){
return testMonth;
}
else{
throw new IllegalArgumentException("month must be between 1 and 12");//throw exception
}
}
public String toString(){
return String.format(
"Date is %d-%d-%d\n",getDay(),getMonth(),getYear()
);
}
}//end of Date
then Patient.java
/*Patient.java
this class represents a patient
*/
public class Patient{
private String name;
private String surname;
private Date dateOfBirth;
public Patient(String theName, String theSurname, Date birthday){
name = theName;
surname = theSurname;
dateOfBirth = birthday;
}
public String getName(){
return name;
}
public String getSurname(){
return surname;
}
/* public String getBirth(){
return dateOfBirth;
} */
public String toString(){
return String.format("Name: %s, Surname: %s, Date of birth: %s ",
getName(), getSurname(), dateOfBirth
);
}
}//end of class
and the testing class, PatientTest.java
/*PatientTest.java
testing the Date and Patient classes
*/
public class PatientTest{
public static void main(String[] args){
Date birth = new Date(10, 02, 1912);//create a date object with that date
Patient patient = new Patient("John", "Dye", birth);
System.out.println(patient);
}
}//end of PatientTest
OK, so here are the questions:
in PatientTest.java I have System.out.println(patient);
This implies that the toString() of the the patient object is called implicitly from what I remember. Class Patient has a toString() method there, so that's fine, but when in class Date I remove the toString() (even though I have this
System.out.printf(
"Date is %d-%d-%d\n",getDay(),getMonth(),getYear()
);
) I don't get a properly formatted date but rubbish values:
antobbo@antobbo-linux-Dell-System-XPS-L702X:/media/KINGSTON/JAVA/health_basic$ java PatientTest
Date is 10-2-1912
Name: John, Surname: Dye, Date of birth: Date@65c0035b
Is ther eason for this that, since Date is "part" of the patient object created int he test class, the Date class must also have a toString() method? I'm not entirely clear on this.
Second question: in Patient.java I have some getters methods to return the instance variables of type String. I wanted to have a getter to return an instance variable (or is that an object?) of type Date, so I included this
/* public String getBirth(){
return dateOfBirth;
} */
trouble is that if I then call it from, say, the toString() method like so:
public String toString(){
return String.format("Name: %s, Surname: %s, Date of birth: %s ",
getName(), getSurname(), getBirth()
);
}
I get the following error:
antobbo@antobbo-linux-Dell-System-XPS-L702X:/media/KINGSTON/JAVA/health_basic$ javac *.java
Patient.java:23: incompatible types
found : Date
required: java.lang.String
return dateOfBirth;
^
1 error
Does it mean that I can't use a getter with reference types?
thanks