user761497
user761497

Reputation: 67

average ( working with arraylist ) java

i'm trying to display the average mark of students, but it's display the wrong number, this is what i've tried. Any help would be greatly appreciated.

import java.util.ArrayList;

class Course
{

  private ArrayList<Student> people = new ArrayList<Student>();  

// return the average mark in the course

  public double average()
  {
        double average = 0.0;
        int i = 0;

        for (i = 1; i < people.size(); i++)
        {
            int tmark = people.get(i).getMark() ; 
            {
                average = tmark / i;
            }       
        }
        return average;
  }

} 

Upvotes: 0

Views: 8329

Answers (6)

Shlomi
Shlomi

Reputation: 4748

sure, you are not calculating average.. you have to divide by the size after summing all the values:

public double average()
{
     double average = 0.0;
     for (int i = 0; i < people.size(); i++)  {
         average += people.get(i).getMark() ; 
     }
     return average/people.size();
 }

Upvotes: 3

Dunes
Dunes

Reputation: 40703

You need to work through your code on paper. You are doing something very peculiar. Get a bit of paper, write down an example array of Students (or just marks), and then work through your loop one iteration at a time. And answer these questions

  • What is the value of each variable at the beginning of the loop?
  • What is the value of each variable at the end of each loop?
  • How would you calculate the average on your own with a pen and paper?
  • How can the above point be translated into a for loop? Does all the work need to be done in the for loop?

One last point. It should be noted that the first element in an ArrayList or array is at index 0 and that the last is at index (size-1).

eg.

index:   0    1    2
array: ['a', 'b', 'c']
size or length: 3

Upvotes: 2

dotoree
dotoree

Reputation: 2993

Try to change this :

public double average()
  {
        double average = 0.0;


        for (int i = 0; i < people.size(); i++)
        {
            int tmark = people.get(i).getMark() ; 
            {
                average += tmark;
            }       
        }
        average /= people.size();

        return average;
  }

} 

Upvotes: 0

Tom
Tom

Reputation: 4180

try:

double total = 0d;
for(Student student : people) {
    total += student.getMark();
}
double average = total / (double)people.size();

Upvotes: 0

amit
amit

Reputation: 178431

As others mentioned, the index of ArrayList in java starts with 0.

However, you might want to use a for each loop in here instead. Replace:

for (i = 1; i < people.size(); i++)

with

for (Student s : people)

to sum the total grades, and then divide with people.size() as suggested by @alf.

Upvotes: 0

alf
alf

Reputation: 8513

First of all, get() is 0-based, not 1-based.

Second, you need to divide the whole sum by people.size() rather than i, or else first student becomes way more important than 100th one.

Update: and third, the current code only considers the very last student, don't you think?

Upvotes: 4

Related Questions