Computer Program Reading Comprehension Project
U of T Computer Program Reading Comprehension Project
CalcAverage
This exercise focuses on conversion between integers and floating point numbers.

Download files: CalcAverage.java
Areas: Primitive types.


CalcAverage.java
1int mark1 = 10;
2int mark2 = 9;
3int mark3 = 9;
4int ave = (mark1 + mark2 + mark3) / 3;


Question 1
What is the programmer trying to do?
Solution

Question 2
Assuming the programmer is trying to calculate the exact average of each student's marks, will he get the desired result?
Hints Solution

Question 3
What will the value of ave be?
Solution

Question 4
After several complaints from unhappy students, the programmer decided to change the last line of the code to
int ave = (mark1 + mark2 + mark3) / 3.0;
Is the problem solved?
Hints Solution

Question 5
Realizing programming is hard work and requires considerable amount of persistency, the programmer did not give up. After hours of trial and error, he changed the code to
double ave = (mark1 + mark2 + mark3) / 3;
The compiler error disappeared; all seemed to be good. However, the programmer continues to hear the same complaints from students. Now he is completely confused. Do you know where the problem is? How would you make the code work?
Hints Solution