Archibald Haddock (123456789)
COMP-202A, Section 0 (Fall 2010)
Instructor: Cuthbert Calculus
Assignment 1, Question 3 (b)
1. Line: 17 (bug manifests itself on line 27)
Problem: If the value of variable x is 0, then the program crashes.
Type: Run-time
Fix: Tell the user that the value entered for his/her age in 1989
must not be zero.
New line: System.out.print("How old were you in 1989 (value must be " +
"positive)? ");
2. Line: 24
Problem: Only the last term in the sum is divided by 3 due to operator
precedence rules; however, calculating the average of 3 values
involves dividing their sum by 3, not just the last term in the
sum.
Type: Logical
Fix: Move the close parenthesis ')' so that it is before the '/'
operator.
New line: q = (x + y + z) / 3;
3. Line: 24
Problem: Because x, y, and z are all of type int, their sum will be of
type int as well. However, 3 is also of type int, and division
with two ints results in a value of type int (with the
fractional part being truncated), regardless of the type of the
variable in which the quotient is stored.
Type: Logical
Fix: Divide the sum of x, y, and z by 3.0 (which is of type double)
instead of 3 (which is of type int). This will result in the
sum of x, y, and z being promoted to type double before the
division takes place.
New line: q = (x + y + z) / 3.0;
4. Line: 27
Problem: An operator (presumably the multiplication operator, '*'), is
missing between 4 and (z/x)
Type: Syntactic
Fix: Add a multiplication operator '*' between 4 and (z/x)
New line: int meaninglessexpression = (((q*12-(4(z/x)+100)*y-z)%5)/2+283);
5. Line: 27
Problem: Because variable q of type double is used in the meaningless
expression, the latter evaluates to type double. However, the
value of the expression is assigned to a variable of type int;
this is a narrowing assignment conversion, which is not allowed
Type: Syntactic
Fix: a) Cast the expression to int
b) Change the type of variable meaninglessexpression to double
New line: a) int meaninglessexpression =
(int)(((q*12-(4(z/x)+100)*y-z)%5)/2+283);
b) double meaninglessexpression =
(((q*12-(4(z/x)+100)*y-z)%5)/2+283);