Skip to main content
Tweeted twitter.com/StackCodeReview/status/688645938647298048
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
added 2 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Of "Of three points supplied, is one a midpoint of the other two?"

Write a method named hasMidpointhasMidpoint 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;
    }

Of three points supplied, is one a midpoint of the other two?

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;
    }

"Of three points supplied, is one a midpoint of the other two?"

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;
    }
Source Link
cody.codes
  • 2k
  • 2
  • 26
  • 45

Of three points supplied, is one a midpoint of the other two?

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;
    }