1. Member Join Date
Mar 2011
Posts
1
Rep Power
0

## Average of an array. Please help

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);
//assigning BMI index to the appropriate category
if(Bmiindex>=25)
{
Bmi=Bmicat;
}
else if (Bmiindex<=20)
{
Bmi=Bmicat;
}
else if(Bmiindex<25&&Bmiindex>20)
{
Bmi=Bmicat;
}
}
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;
}

}  Reply With Quote

2. ## 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.

Java 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:

Java 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"));```
Last edited by ozzyman; 03-28-2011 at 09:11 PM.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•