-1

I'm working on this project for my computer science class that tells you what grade you need to get on the final to get an A in the class. I'm working on the method that finds the grade you need to get, but it keeps printing out 15.

Does anyone know what I'm doing wrong? The issue is not due to int division.

public int gradeNeeded() {
    int sumSum = 0;
    double newOverall = 0;
    int i = 0;
    for (int j = 0; j < sumGrades.length; j++) {
        sumSum += sumGrades[j];
    }
    while (newOverall < 90) {
        sumSum += i;
        double sumAverage = ((double) sumSum / (sumGrades.length + 1));
        newOverall = (this.getFormAverage() * .25) + (sumAverage * .75);
        i++;
    }
    return i;
}
  • 3
    What do you expect it to print out instead? Give us some samples of input. – Makoto Apr 27 '15 at 00:14
  • 2
    The problem is in `sumAverage = sumSum / (sumGrades.length + 1)`, which uses integer division. See the question I've linked to for more info on that. (The tl;dr is: you want `sumAverage = ((double) sumSum / (sumGrades.length + 1)`) – yshavit Apr 27 '15 at 00:16
  • What I have been using to test it so far is 80 for formAverage and 2 90s in the sumGrades array and with those inputs it should return 100 – Michael Cunningham Apr 27 '15 at 00:17
  • yshavit That didn't fix it. It still returns 15. – Michael Cunningham Apr 27 '15 at 00:23
  • Oops sorry, my parens were wrong. Should be `((double) sumSum) / ...` – yshavit Apr 27 '15 at 02:45

1 Answers1

0

The problem is that you keep increasing sumSum in the loop. You should also increase i at the beginning of the loop, so you are not off by one.

   int i = -1;

   while (newOverall < 90) {
        i++;
        int nSum = sumSum+i;
        double sumAverage = ((double) nSum / (sumGrades.length + 1));
        newOverall = (this.getFormAverage() * .25) + (sumAverage * .75);
    }
Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102