Results 1 to 2 of 2
Thread: Average of an array. Please help
- 03-28-2011, 08:47 PM #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);
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;
}
}
-
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.
the above code assumes you've compiled your BMI data into a java.util.List<BMIObjects> to simplify your processJava 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; }
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.
Similar Threads
-
average
By swrta in forum New To JavaReplies: 3Last Post: 03-16-2011, 09:52 AM -
2 dimensional array, average student
By aborgeld in forum Advanced JavaReplies: 5Last Post: 03-05-2011, 03:12 PM -
Can't get correct average using array
By nevets93 in forum New To JavaReplies: 2Last Post: 02-11-2011, 10:33 AM -
average
By anjigadu in forum New To JavaReplies: 4Last Post: 09-19-2010, 09:52 PM -
Need help getting average
By soccer_kid_6 in forum New To JavaReplies: 15Last Post: 09-12-2010, 11:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks