0

I'm trying to get the average score from objects but can't think of a good way to do it.

I have 10 objects and they all have their own score associated with it.

I created setter and getter class where it would get the average, but the problem I run into is that I would create something like this so that even if I do put object's score into this method the holder will go back to 0.

public double getAverage() {
    return average;
}

public void setAverage(double studentScore) {
    double holder = 0;

    average = holder + studentScore;
    holder = average;

    this.average = studentScore;
}

I was also thinking of just creating setter and getter methods for every single score, but that takes up lots of space and I figured there has to be a better way to do it.

Here is a snippet of the code I'm using in the main method

public static void main(String[] args) {
  final String STUDENT_INFO = "HW1_Students.txt";

  List<GradeInfo> list = new ArrayList<GradeInfo>();

  Scanner inputFile = null;
  GradeInfo person1 = new GradeInfo();
  GradeInfo person2 = new GradeInfo();
  GradeInfo person3 = new GradeInfo();
  GradeInfo person4 = new GradeInfo();
  GradeInfo person5 = new GradeInfo();
  GradeInfo person6 = new GradeInfo();
  GradeInfo person7 = new GradeInfo();
  GradeInfo person8 = new GradeInfo();
  GradeInfo person9 = new GradeInfo();
  GradeInfo person10 = new GradeInfo();

  list.add(person1);
  list.add(person2);
  list.add(person3);
  list.add(person4);
  list.add(person5);
  list.add(person6);
  list.add(person7);
  list.add(person8);
  list.add(person9);
  list.add(person10);


  try {
   inputFile = new Scanner(new File(STUDENT_INFO));
  }

  catch (FileNotFoundException ex) {
   System.out.println("\n *** Exception occured while opening "
     + ex.getMessage() + " ***");
   System.exit(-1);
  }

  readData(inputFile, STUDENT_INFO, person1, person2, person3, person4,
    person5, person6, person7, person8, person9, person10);

      for (int i = 0; i < list.size(); i++) {
          System.out.println(list.get(i));
      }
}

Any advice would be greatly appreciated, thanks.

2
  • What do you need to keep the value of holder for? I am confused.
    – Austin
    Sep 15, 2015 at 20:54
  • 1
    I have no idea what you're trying to achieve here. What is setAverage() supposed to do? Based on its name, this method should take an average, and store it in a field. If you want to compute an average, you need to sum all the scores, and divide the result by the number of students. A loop is needed.
    – JB Nizet
    Sep 15, 2015 at 21:10

3 Answers 3

1
public void setAverage(ArrayList<GradeInfo> grades) {
double average = 0;
for(int i = 0; i < grades.size(); i++){
    average += grades.get(i);
}
average = average / grades.size();
this.average = average;
}

I hope this is what you meant to do....... This method will take your Arraylist, and set the correct average score in your wired "setter and getter class".

1

I dont really understand what you are trying to do here:

 public void setAverage(double studentScore) {
        double holder = 0;

        average = holder + studentScore;
        holder = average;

        this.average = studentScore;
    }

But its logical that holder will be reset to 0 because you initialize it and set it to 0 in the beginnig of your function, or do i misunderstand this? Just create holder outside of your setter.

And if you just want to have the average of a bunch of values just add all the values together and divide the result by the number/quantity of your values.

For example:

public double getAverage(List <GradeInfo> l){
       int totalAmount;

       for(i = 0; i < l.size(); i++){
           totalAmount += l.get(i);
       }

       double average = totalAmount / l.size();
       return average;
}
1
  • Thank you, I wasn't thinking straight but the holder should have just been average = average + studentScore; Thanks for the help
    – MKatana
    Sep 15, 2015 at 21:46
0

This is probably not applicable to your program insofar as you probably have at least one positive value going into your calculation. But I could see a professor taking off points for it if he is testing all possible cases.

If you do follow the advice of Sand, be sure to check that the size of the arraylist is greater than zero before you use the size in the average calculation. You cannot divide by zero in Java. Instead, do this:

public double getAverage(List <GradeInfo> l){
       int totalAmount;

       for(i = 0; i < l.size(); i++){
       totalAmount += l.get(i);
       }
       if (l.size() > 0) {
       double average = totalAmount / l.size();
       return average;
       else {
            double average = 0;
            }
            return average;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.