Question: The following method attempts to return the median (middle) of three integer values, but it contains logic errors. In what cases does the method return

The following method attempts to return the median (middle) of three integer values, but it contains logic errors. In what cases does the method return an incorrect result? How can the code be fixed?

public static int medianof3 (int n1, int n2, int n3) { if (nl < n2) { if (n2 < n3) { return n2; } else { return n3; } else { if (nl < n3) { return n1; } else { return n3;

public static int medianof3 (int n1, int n2, int n3) { if (nl < n2) { if (n2 < n3) { return n2; } else { return n3; } else { if (nl < n3) { return n1; } else { return n3;

Step by Step Solution

3.33 Rating (159 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The medianOf3 code fails when n3 is the smallest of the three numbers for example when the par... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Building Java Programs A Back to Basics Approach Questions!