Results 1 to 2 of 2
  1. #1
    vika is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Question 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;
    }

    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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.

Similar Threads

  1. average
    By swrta in forum New To Java
    Replies: 3
    Last Post: 03-16-2011, 09:52 AM
  2. 2 dimensional array, average student
    By aborgeld in forum Advanced Java
    Replies: 5
    Last Post: 03-05-2011, 03:12 PM
  3. Can't get correct average using array
    By nevets93 in forum New To Java
    Replies: 2
    Last Post: 02-11-2011, 10:33 AM
  4. average
    By anjigadu in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 09:52 PM
  5. Need help getting average
    By soccer_kid_6 in forum New To Java
    Replies: 15
    Last Post: 09-12-2010, 11:59 PM

Posting Permissions

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