Write a method named
hasMidpoint
that accepts three integers as parameters and returns true if one of the integers is the midpoint between the other two integers; that is, if one integer is exactly halfway between them. Your method should return false if no such midpoint relationship exists.
Here are two different versions of code that I wrote at two different occasions to accomplish the same thing. How can I make my code better, and is there a better way to accomplish this task?
public static boolean hasMidpoint(int a, int b, int c) {
if ( a == b && b == c) {
return true;
} else if ((a + b) / 2.0 == c || (b + c) / 2.0 == a || (a + c) / 2.0 == b) {
return true;
} else {
return false;
}
}
public static boolean hasMidpoint(int p1, int p2, int p3) {
if((double)(p1 + p2) / 2 == p3 || (double) (p2 + p3) / 2 == p1 || (double) (p1 + p3) / 2 == p2){
return true;
}
return false;
}