Rich Kid
Rich Kid

Reputation: 23

how do i calculate the mean for my code

The file has various team names and the other file has attendances for the matches. I am trying to write a piece of code that finds the average attendance for each team.

i would like it to look similar to this

public static double mean(double[] m) {
    double sum = 0;
    for (int i = 0; i < m.length; i++) {
        sum += m[i];
    }
    return sum / m.length;
}

I tried to work it out myself and here is what i came up with. I am trying to combine two list together.

public static String getAverageAttendance(Team team)
{
    ArrayList<Match> ourList = new ArrayList(results);



    ArrayList<Match> teamsAttendance = new ArrayList<Match>();
    for (Match att : ourList)
    {
        if (att != null && att.getTeamName().equals(team.getName()))
        {
            teamsAttendance.add(att);
        }
    }



    float crowd = 0;
    for (Match att : teamsAttendance)
    {
     float multiplier = (att.getAttendance()/391);
     crowd = crowd + multiplier

    }
}

I understand that the code probably needs redoing and i dont think i used float correctly. As you can see i am an novice and any help would be highly appreciated.

Upvotes: 1

Views: 74

Answers (2)

Marc Baumbach
Marc Baumbach

Reputation: 10473

I'm going off the assumption that results is a static List<Match>. With that assumption, I believe you could create your function this way:

public static String getAverageAttendance(Team team) {
    double totalCrowds = 0.0;
    int totalMatches = 0;
    for (Match match : results) {
        if (match.getTeamName().equals(team.getName())) {
            totalCrowds += match.getAttendance();
            totalMatches++;
        }
    }
    double averageAttendance = totalMatches > 0 ? totalCrowds / totalMatches : 0.0;
    return String.valueOf(averageAttendance);
}

This removes the need to create additional lists and only iterates over the matches once. I chose double, but you could just as easily use float if you prefer that type.

If you're using Java 8 and want to use streams:

public static String getAverageAttendance(Team team) {
    return String.valueOf(results
            .stream()
            .filter(m -> m.getTeamName().equals(team.getName()))
            .collect(Collectors.averagingDouble(Match::getAttendance)));
}

You might need to change averagingDouble to averagingInt or whatever appropriate function depending on your numeric return type from Match::getAttendance.

Note that both of these functions are adhering to your original method signature and are returning a String. If you have control over this method, I'd suggest returning the numeric double or float type and allowing the caller convert it to String if necessary. I would also recommend passing in the List<Match> object as a parameter to the method instead of depending on a static variable since this would make the method more re-useable, but I don't know all of your use cases.

Upvotes: 1

Paul J Abernathy
Paul J Abernathy

Reputation: 1003

You could do something like this:

public static String getAverageAttendance(Team team) {

    double[] attendences = new doble[result.size()];
    for (int i = 0; i < result.size(); i++) {
        Match att = results.get(i);
        if (att != null && att.getTeamName().equals(team.getName())){
            attendences[i] = getAdjustedAttendence(att);
        }
    }

    return mean(attendences);
}

public double getAdjustedAttendence(Match match) {
    //whatever you do to adjust the numbers
}

Don't forget to do error checking in mean(), because passing in a null array would cause it to throw an Exception.

Upvotes: 0

Related Questions