Java Reference
In-Depth Information
Let's look at these sample runs. Clearly, the output in Sample Run 1 is correct. In Sample
Run 2, the input is 3.8 and the output indicates that this GPA is below the graduation
requirement. However, a student with a GPA of 3.8 would graduate with some type of
honor, so the output in Sample Run 2 is incorrect. In Sample Run 3, the input is 1.95
and the output does not show any warning message. Therefore, the output in Sample
Run 3 is also incorrect. It means that the if ... else statement in Lines 11 to 15 is
incorrect. Let us look at these statements:
if (gpa >= 2.0)
//Line 11
if (gpa >= 3.9)
//Line 12
System.out.println("Dean\'s Honor List.");
//Line 13
else
//Line 14
System.out.println("The GPA is below the "
+ " graduation requirement. \nSee your "
+ "academic advisor."); //Line 15
Following the rule of pairing an else with an if , the else in Line 14 is paired with the
if in Line 12. In other words, using the correct indentation, the code is:
if (gpa >= 2.0)
//Line 11
if (gpa >= 3.9)
//Line 12
System.out.println("Dean\'s Honor List.");
//Line 13
else
//Line 14
System.out.println("The GPA is below the "
+ " graduation requirement. \nSee your "
+ "academic advisor."); //Line 15
Now we can see that the if statement in Line 11 is a one-way selection. Therefore, if the
input number is less than 2.0 , no action will take place, that is, no warning message will
be printed. Now suppose the input is 3.8 . Then the expression in Line 11 evaluates to
true , so the expression in Line 12 is evaluated, which evaluate to false . This means the
output statement in Line 13 executes, resulting in an unsatisfactory result.
In fact, the program should print the warning message only if the GPA is less than 2.0 , and
the message:
Dean's Honor List.
if the GPA is greater than or equal to 3.9 .
To achieve that result, the else in Line 14 needs to be paired with the if in Line 11. To
pair the else in Line 14 with the if in Line 11, you need to use a compound statement
as follows:
if (gpa >= 2.0)
//Line 11
{
//Line 12
if (gpa >= 3.9) //Line 13
System.out.println("Dean\'s Honor List."); //Line 14
}
//Line 15
else
//Line 16
System.out.println("The GPA is below the "
+ " graduation requirement. \nSee your
"
+ "academic advisor.");
//Line 17
Search WWH ::




Custom Search