0

I'm taking a java class online, and I need to make a program that calculates grades. However, my output is supposed to have decimal places, but instead it has a single 0. I understand that when you divide a double by an integer it truncates (or removes the decimal values in case I am using that word wrong) it, but I'm not allowed to change the initializations or add any. That would mean I can't change the data type from int to double in the initializations.

I'm not too sure what the right terminology is for this, as my searches on both here and google have turned up with nothing.

Test 3's average calculation is supposed to have the decimal place. the first two come up with 97.0 and 88.0, as they are supposed to. The third one is supposed to come up with 86.333 with the three repeating.

 //local variables
 int numTests = 0;       //counts number of tests
 int testGrade = 0;      //individual test grade
 int totalPoints = 0;    //total points for all tests
 double average = 0.0;   //average grade

    //Test 1 Grade
    testGrade = 97;
    numTests++;     //number of tests increased by one.
    totalPoints = totalPoints + testGrade;     //add the test grade to the total points.
    average = (totalPoints / numTests);     //calculate the average out of all the tests.
    System.out.print("n = " + numTests + "  New Test Grade = " + testGrade + "  Total Points = " + totalPoints);
    System.out.println("   Average Score: " + average);   

    //Test 2 Grade
    testGrade = 79;
    numTests++;
    totalPoints += testGrade;
    average = (totalPoints / numTests);
    System.out.print("n = " + numTests + "  New Test Grade = " + testGrade + "  Total Points = " + totalPoints);
    System.out.println("   Average Score: " + average);

    //Test 3 Grade
    testGrade = 83;
    numTests++;
    totalPoints += testGrade;
    average = (totalPoints / numTests);
    System.out.print("n = " + numTests + "  New Test Grade = " + testGrade + "  Total Points = " + totalPoints);
    System.out.println("   Average Score: " + average);
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
LordValkyrie
  • 75
  • 1
  • 6
  • Have you tried making totalPoints and numTests doubles as well? Or casting them to doubles like average = (double)totalPoints / (double)numTests – Robert Beltran Aug 25 '14 at 18:40
  • 1
    "I understand that when you divide a double by an integer it truncates" This is not true. This only happens when you divide an integer by another integer, which is what you're doing. – Kevin Workman Aug 25 '14 at 18:40

3 Answers3

3

Just type cast the total points to double and then divide it by number of tests.

Like below,

average = ((double) totalPoints / numTests);

or declare totalPoints as double

Loganathan Mohanraj
  • 1,736
  • 1
  • 13
  • 22
1

Try the following

average = (totalPoints / (double)numTests);

This will cast the primitive numTests from an integer into a double primitive data type.

Gary Drocella
  • 337
  • 1
  • 2
  • 11
1

If you can't change the data types of the variables (and it wouldn't make sense to anyway), you instead want to use casting, i.e. converting values to other types.

When you divide two integers, you do indeed get a truncated result. To avoid this without changing the variables to floating point types, you can convert one or both of the integers to floating point values within an expression. Then division takes place using a floating point value (because of the conversion), so no truncating takes place. You can cast one value to another type by enclosing the type in parentheses before the variable in question. In your case, you can cast either totalPoints or numTests to doubles by doing (double) totalPoints and (double) numTests, respectively. The type of both values will be double, after the cast.

You only need to cast one value for the division to take place properly, but some conventions will cast both values for clarity.

jackarms
  • 1,343
  • 7
  • 14