This exercise extends the Person class to calculate the age of a person and compare ages to find who is older.
My Code
Main
public class Main {
public static void main(String[] args) {
Person pekka = new Person("Pekka", new MyDate(15, 2, 1983));
Person steve = new Person("Steve");
System.out.println( pekka );
System.out.println( steve );
}
}
MyDate
public class MyDate {
private int day;
private int month;
private int year;
public MyDate(int pv, int kk, int vv) {
this.day = pv;
this.month = kk;
this.year = vv;
}
public String toString() {
return this.day + "." + this.month + "." + this.year;
}
public boolean earlier(MyDate compared) {
if (this.year < compared.year) {
return true;
}
if (this.year == compared.year && this.month < compared.month) {
return true;
}
if (this.year == compared.year && this.month == compared.month
&& this.day < compared.day) {
return true;
}
return false;
}
/*
* In assignment 92 method differneceInYears was added to MyDate
* Copy the method here since it eases this assignment considerably.
*/
public int differenceInYears(MyDate comparedDate){
int days1 = this.day;
days1 += this.month * 30;
days1 += this.year * 365;
int days2 = comparedDate.day;
days2 += comparedDate.month * 30;
days2 += comparedDate.year * 365;
return Math.abs((days1 - days2) / 365);
}
}
Person
import java.util.Calendar;
public class Person {
private String name;
private MyDate birthday;
private MyDate currentDate;
;
public Person(String name, int pp, int kk, int vv) {
this.name = name;
this.birthday = new MyDate(pp, kk, vv);
}
public Person(String name, MyDate birthday) {
this.name = name;
this.birthday = birthday;
}
public Person(String name) {
this.name = name;
this.birthday = new MyDate(Calendar.getInstance().get(Calendar.DATE),
Calendar.getInstance().get(Calendar.MONTH) + 1,
Calendar.getInstance().get(Calendar.YEAR));
}
public int age() {
// calculate the age based on the birthday and the current day
// you get the current day as follows:
MyDate currentDate = new MyDate(Calendar.getInstance().get(Calendar.DATE),
Calendar.getInstance().get(Calendar.MONTH) + 1,
Calendar.getInstance().get(Calendar.YEAR));
return this.birthday.differenceInYears(currentDate);
}
public boolean olderThan(Person compared) {
// compare the ages based on birthdays
if (this.birthday.earlier(compared.birthday)) {
return true;
}
return false;
}
public String getName() {
return this.name;
}
public String toString() {
return this.name + ", born " + this.birthday;
}
}
Model Code
Main
public class Main {
public static void main(String[] args) {
Person pekka = new Person("Pekka", new MyDate(15, 2, 1983));
Person steve = new Person("Steve");
System.out.println( pekka );
System.out.println( steve );
}
}
MyDate
public class MyDate {
private int day;
private int month;
private int year;
public MyDate(int pv, int kk, int vv) {
this.day = pv;
this.month = kk;
this.year = vv;
}
public String toString() {
return this.day + "." + this.month + "." + this.year;
}
public boolean earlier(MyDate compared) {
if (this.year < compared.year) {
return true;
}
if (this.year == compared.year && this.month < compared.month) {
return true;
}
if (this.year == compared.year && this.month == compared.month
&& this.day < compared.day) {
return true;
}
return false;
}
/*
* In assignment 92 method differneceInYears was added to MyDate
* Copy the method here since it eases this assignment considerably.
*/
public int differneceInYears(MyDate compared) {
if (earlier(compared)) {
return compared.differneceInYears(this);
}
return calculateDifference(compared);
}
private int calculateDifference(MyDate verrattava) {
int vuosiPois = 0;
if (this.month < verrattava.month) {
vuosiPois = 1;
} else if (this.month == verrattava.month && this.day < verrattava.day) {
vuosiPois = 1;
}
return this.year - verrattava.year - vuosiPois;
}
}public class MyDate {
private int day;
private int month;
private int year;
public MyDate(int pv, int kk, int vv) {
this.day = pv;
this.month = kk;
this.year = vv;
}
public String toString() {
return this.day + "." + this.month + "." + this.year;
}
public boolean earlier(MyDate compared) {
if (this.year < compared.year) {
return true;
}
if (this.year == compared.year && this.month < compared.month) {
return true;
}
if (this.year == compared.year && this.month == compared.month
&& this.day < compared.day) {
return true;
}
return false;
}
/*
* In assignment 92 method differneceInYears was added to MyDate
* Copy the method here since it eases this assignment considerably.
*/
public int differneceInYears(MyDate compared) {
if (earlier(compared)) {
return compared.differneceInYears(this);
}
return calculateDifference(compared);
}
private int calculateDifference(MyDate verrattava) {
int vuosiPois = 0;
if (this.month < verrattava.month) {
vuosiPois = 1;
} else if (this.month == verrattava.month && this.day < verrattava.day) {
vuosiPois = 1;
}
return this.year - verrattava.year - vuosiPois;
}
}
Person
import java.util.Calendar;
public class Person {
private String name;
private MyDate birthday;
public Person(String name, int pp, int kk, int vv) {
this.name = name;
this.birthday = new MyDate(pp, kk, vv);
}
public Person(String name, MyDate birthday) {
this.name = name;
this.birthday = birthday;
}
public Person(String name) {
this.name = name;
Calendar nyt = Calendar.getInstance();
int year = nyt.get(Calendar.YEAR);
int month = nyt.get(Calendar.MONTH) + 1;
int day = nyt.get(Calendar.DATE);
this.birthday = new MyDate(day, month, year);
}
public int age() {
// calculate the age based on the birthday and the current day
// you get the current day as follows:
// Calendar.getInstance().get(Calendar.DATE);
// Calendar.getInstance().get(Calendar.MONTH) + 1; // January is 0 so we add one
// Calendar.getInstance().get(Calendar.YEAR);
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DATE);
MyDate tamaPaiva = new MyDate(day, month, year);
return birthday.differneceInYears(tamaPaiva);
}
public boolean olderThan(Person compared) {
// compare the ages based on birthdays
return this.birthday.earlier(compared.birthday);
}
public String getName() {
return this.name;
}
public String toString() {
return this.name + ", born " + this.birthday;
}
}
Comments
I don’t like how the current date is worked out in two places but when i went to use it as a global variable Netbeans wanted me to initialise it in the constructor.