Ben South
Ben South

Reputation: 13

How do I find the average and the top grade in an ArrayList in Java?

In this piece of code I would like to find out the top mark that a student has got, and also the average of all the marks. The marks are put into an ArrayList by user input. I have half done the double but do not know how to finish it, and I would like to know how to find the top mark.

Thanks.

import java.util.*;
import java.lang.*;
class Course
{
    private ArrayList<Student> people = new ArrayList<Student>();

    public void add( Student s )
    {
        people.add( s );
    }
    //Return the number of students who passed (mark>= 40)
    public int pass()
    {
        int count = 0;
        for ( int i=0; i < people.size(); i++ )

        {
            int mark = people.get(i).getMark();
            if(mark < 40){
                count = count +1;
            }
        }

        return count;
    }

    public int fail()
    {
        int count = 0;
        for ( int i=0; i < people.size(); i++ )

        {
            int mark = people.get(i).getMark();
            if(mark < 40){
                count = count +1;
            }
        }

        return count;
    }

   public String top()
   {

   }

    public double average()
    {
        int sum=0;

        for (int i=0; i < people.size(); i++ )
        {
            double average = sum / (double) i;
        }

        return sum;
    }

}

Upvotes: 0

Views: 4904

Answers (5)

user2624794
user2624794

Reputation: 1

Check this. It may help you.

Java program to find average of all numbers except top three

Upvotes: 0

John Ericksen
John Ericksen

Reputation: 11113

You can accomplish this by gathering statistics in the add(Student) method:

public class Course {
    private ArrayList<Student> people = new ArrayList<Student>();
    private int passing = 0;
    private int failing = 0;
    private int top = Integer.MIN_VALUE;
    private int sum = 0;

    public void add( Student s ) {
        people.add( s );
        if(s.getMark() >= 40){
            passing++;
        }
        else {
            failing++;
        }
        sum += s.getMark();
        if(s.getMark() > top) {
            top = s.getMark();
        }
    }

    public int pass() {
        return passing;
    }

    public int fail() {
        return failing;
    }

    public int top() {
        return top;
    }

    public double average() {
        return sum / people.size();
    }
}

To answer your questions directly, top is simply found my comparing each mark to the maximum found mark. Average is found by dividing the total sum by the number of marks.

Upvotes: 0

Daniel Garc&#237;a Baena
Daniel Garc&#237;a Baena

Reputation: 1201

You can implement Comparable interface (http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html) with your Student class and then to use Collections (http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html) class to use max and min functions to get the max and the min grade values. To get the average grade value you just have to iterate the ArrayList making the sum of the grade values and finally dividing the sum by the ArrayList.size().

Upvotes: 0

Reimeus
Reimeus

Reputation: 159854

Use Collections.max(people, yourComparator) where yourComparator uses getMark as the comparison field: You could do:

Student maxStudent = Collections.max(people, new Comparator<Student>() {
    @Override
    public int compare(Student first, Student second) {
        if (first.getMark() > second.getMark())
            return 1;
        else if (first.getMark() < second.getMark())
            return -1;
        return 0;
    }
});

Upvotes: 1

ghdalum
ghdalum

Reputation: 891

For getting the top mark you create a variable which you replace each time a higher mark is found. If you are already looping you can find the average by summing all marks in a loop and divide it by total people. Here I have done both in the same loop:

int totalMark = 0;
int topMark = 0;
for (int i=0; i< people.size(); i++) {
    int mark = people.get(i).getMark(); 
    if (mark > topMark) {
        topMark = mark;
    }
    totalMark += mark;
}
int averageMark = totalMark / people.size();

Upvotes: 0

Related Questions