-2

I am fairly new to Java coding.I have an exercise that demands to calculate an average of marks according to registration for modules.I checked the code so many times,but I can't find where Is the error.This Is what I do:

public class Student {

    double mark1, mark2, mark3, mark4, mark5, mark6;
    boolean regModule1, regModule2, regModule3, regModule4, regModule5, regModule6;
    boolean tabModule[] = {regModule1, regModule2, regModule3, regModule4, regModule5, regModule6};
    double tabmark[] = {mark1, mark2, mark3, mark4, mark5, mark6};

    public void average() {
        double sum = 0;
        int count = 0;
        double av;

        for(int i=0; i<=5; i++) {
          if(tabModule[i] == true) {
              count++;
              sum += this.tabmark[i];
          }
        }
        av = sum/count;
        System.out.println("the average is: " + av);
    }
}

That's the code in the main:

      e1.regModule1 = true;
      e1.regModule4 = true;
      e1.mark1 = 18.5;
      e1.mark4 = 13.25;
      e1.average();

When I run the project, I receive this:

the average is: NaN(not a number)

what I'm messing here?

Yassel
  • 77
  • 1
  • 1
  • 7
  • 3
    Hint: print out `count`. What do you expect it to be, and what is it? – Jon Skeet Jul 13 '20 at 17:31
  • 1
    Changing the variables `regModule1` etc. doesn't change the values in `tabModule` - it is initialized in advance. – RealSkeptic Jul 13 '20 at 17:32
  • 1
    `if(tabModule[i]==true) ` - What value do you expect `tabModule` elements to have and why? `sum+=this.tabmark[i];` - What value do you expect `tabmark` elements to have and why? – David Jul 13 '20 at 17:32
  • 1
    primitive type are not passed by reference, so you just put 0 in your arrays, changing `regModule` doesn't change the values in the arrays – azro Jul 13 '20 at 17:33
  • @jonSkeet count calculate the number of modules who a student registered in them – Yassel Jul 13 '20 at 18:29
  • 1
    @YassineElAissati: But what did you expect the value to be just before you did the division? And what was the value when you printed it out? – Jon Skeet Jul 13 '20 at 18:33
  • @RealSkeptich yes it will always be false ,what i should to do to avoid this problem ? – Yassel Jul 13 '20 at 18:35
  • @David first i was excepting that tabModule will be true after changing the variables but now i know that it is initialized in advance and will be always false , i except that tabmark will have value of mark who is appropriate with the module (if a student is registered in module1 it will have the value of mark1) – Yassel Jul 13 '20 at 18:42
  • @azro yes it will be always false – Yassel Jul 13 '20 at 18:43
  • @JonSkeet after the division in this exemple i was excepte 2 because the Student e1 have registered in 2 modules (Module1 and Module4) ,the value of count will still always 0,because { if(tabModule[i]==true) }always will be false} – Yassel Jul 13 '20 at 18:47
  • 1
    Exactly - my original hint was to encourage you to find out that `count` was 0, and then look into why. – Jon Skeet Jul 13 '20 at 18:52

1 Answers1

1

Look, the problem is that your Boolean variables have been initialized with false by default, so if(tabModule[i] == true) always will be false and not true, so count will be zero and division for zero is not allowed, but in this case Java promotes the integer value to double, so you have double sum = 0 and count = 0.0 after promotion to double, so the result is NaN.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jordy
  • 109
  • 5