• 03-28-2011, 09:47 PM
vika
Have to write a method as a part of the program that calculates average age in each Bmi group('O','U','N') The class where ages are stored is displayed below. Totally stuck...

//a class to keep data of an individual member

public class Account
{ //the attributes
private String accountName;
public double height;
public double weight;
public int age;
public double Bmiindex;
public char Bmi;
public char[] Bmicat = {'O','U','N'};

public Account(String nameIn, double heightIn, double weightIn,int ageIn)//constructor
{
accountName = nameIn;
height = heightIn;//in meters(eg. 1.60)
weight = weightIn;//in kilograms
age = ageIn;
Bmiindex = weight/(height*height);
double pp=Bmiindex;
double pt=Math.round(pp*100);
System.out.println("Your BMI is : "+pt/100);
//assigning BMI index to the appropriate category
if(Bmiindex>=25)
{
Bmi=Bmicat[0];
}
else if (Bmiindex<=20)
{
Bmi=Bmicat[1];
}
else if(Bmiindex<25&&Bmiindex>20)
{
Bmi=Bmicat[2];
}
}
//methods to read the atributes
public String getaccountName()
{
return accountName;
}
public double getHeight()
{
return height;
}
public double getWeight()
{
return weight;
}

public int getAge()
{
return age;
}
public char getBmi()
{
return Bmi;
}
public double Bmiindex()
{
return Bmiindex;
}
public char[] getBmicat()
{
return Bmicat;
}

}
• 03-28-2011, 10:09 PM
ozzyman
use the same method but pass the different BMI groups as a parameter. then you can run the same method 3 times after to get the different results you need.

Code:

```public double getAverageAge(String BMIgroup) { double totalAge = 0; double groupCount = 0;     for (int i=0; i<BMIlist.size(); i++) {         if (BMIlist.get(i).group == BMIgroup) {             //we have a match             totalAge += BMIlist.get(i).age;             //count number of matches             groupCount++;         }     } double averageAge = totalAge/groupCount; return averageAge; }```
the above code assumes you've compiled your BMI data into a java.util.List<BMIObjects> to simplify your process

then you would need to call the method for each group:

Code:

```System.out.printf("The average age for BMI group O is %.2f %n",getAverageAge("O")); System.out.printf("The average age for BMI group U is %.2f %n",getAverageAge("U")); System.out.printf("The average age for BMI group N is %.2f %n",getAverageAge("N"));```