My question cud be very daft but i am new to programming so please help.. need to complete assignment for my uni
i am trying to find average age of a Squad (n no.of players) but the code gives me wring answer there are two classes 1. Players and 2. Club
In Player Class I have written this bit of code for gettingage of individual player:
public String getAge() {
Calendar now = Calendar.getInstance(); int year=now.get(Calendar.YEAR); int mon = now.get(Calendar.MONTH); int day = now.get(Calendar.DAY_OF_MONTH);
now.set(year,mon,day);
long nowinday = now.getTimeInMillis()/86400000;
Calendar DOB = Calendar.getInstance();
DOB.set(yob,dob,mob); long dobinday = DOB.getTimeInMillis()/86400000;
long spanofday = nowinday - dobinday;
int years = (int)(spanofday / 365); int rem = (int)(spanofday % 365);
int month = (rem / 30); rem = (rem % 30);
String age = "The age is " + years + " years " + month + " months and " + rem + " days"; return age; }
And in Club Class I have written this bit of code for calculating averageage. but it does not work...please help
//** method for calculating average Age of Squad
public String getAverageAge() { Calendar cal1 = Calendar.getInstance(); long days = 0; long months = 0; String ret; int year = cal1.get(cal1.YEAR); int month = cal1.get(cal1.MONTH) + 1; int day = cal1.get(cal1.DATE); cal1.set(year, month, day); for (Players p:Squad) { Calendar cal2 = p.getDateOfBirth(); cal2.set(cal2.get(cal2.YEAR), cal2.get(cal2.MONTH), cal2.get(cal2.DATE)); long mills = 86400000; days = day + ((cal1.getTimeInMillis() - cal2.getTimeInMillis())/mills); } days = days/Squad.size(); long years = (days / 365); long rem = (days % 365); if (rem > 30) { months = (rem / 30); rem = (rem % 30); } ret = year + " years " + months + " months " + days + " days"; return ret; }