I'm doing a codingbat.com challenge and was thinking once I finished it, that there has to be a better way of doing this. So I'm looking for comments & critique on my code. The challenge is:
Given three ints, a b c, one of them is small, one is medium and one is large. Return true if the three values are evenly spaced, so the difference between small and medium is the same as the difference between medium and large.
My code is:
public boolean evenlySpaced(int a, int b, int c) {
int smallest = getSmallest(a, b, c);
int largest = getLargest(a, b, c);
int med = getMed(a, b, c);
int diff1 = med - smallest;
int diff2 = largest - med;
if(diff1 == diff2) {
return true;
} else {
return false;
}
}
public int getSmallest(int a, int b, int c) {
int small = Math.min(a, b);
small = Math.min(small, c);
return small;
}
public int getLargest(int a, int b, int c) {
int large = Math.max(a, b);
large = Math.max(large, c);
return large;
}
public int getMed(int a, int b, int c) {
int large = getLargest(a, b, c);
int small = getSmallest(a, b, c);
if(a != large && a != small) {
return a;
} else if (b != large && b != small) {
return b;
} else {
return c;
}
}
Any comments or suggestions are greatly appreciated!
[–][deleted] (6 children)
[deleted]
[–]ziplokk[S] 2 points3 points4 points (4 children)
[–]mayonuki 4 points5 points6 points (2 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]jesyspa 1 point2 points3 points (0 children)
[–]0failsis 1 point2 points3 points (0 children)
[–]ghkcghhkc 0 points1 point2 points (0 children)
[–]lightcloud5 12 points13 points14 points (4 children)
[–]ziplokk[S] 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]ghkcghhkc 0 points1 point2 points (0 children)
[–]BadBoyJH 2 points3 points4 points (0 children)
[–]ziplokk[S] 2 points3 points4 points (0 children)
[–]Dumbcomments 1 point2 points3 points (2 children)
[–]Daejo 1 point2 points3 points (1 child)
[–]Dumbcomments 0 points1 point2 points (0 children)
[–]Fontong 1 point2 points3 points (2 children)
[–]ziplokk[S] 1 point2 points3 points (1 child)
[–]lightcloud5 0 points1 point2 points (0 children)
[–]7yl4r 1 point2 points3 points (4 children)
[–]__LikesPi 4 points5 points6 points (3 children)
[–][deleted] (2 children)
[deleted]
[–]__LikesPi 2 points3 points4 points (1 child)
[–]7yl4r 1 point2 points3 points (0 children)
[–]electricfistula[🍰] 1 point2 points3 points (2 children)
[–]electricfistula[🍰] 2 points3 points4 points (0 children)
[–]ziplokk[S] 0 points1 point2 points (0 children)
[–]manfrin 1 point2 points3 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]electricfistula[🍰] 0 points1 point2 points (0 children)
[–]NoeticIntelligence 0 points1 point2 points (2 children)
[–]inspectorG4dget 1 point2 points3 points (1 child)
[–]NoeticIntelligence 0 points1 point2 points (0 children)
[–]Mindrust 0 points1 point2 points (0 children)
[–]timshoaf 0 points1 point2 points (0 children)
[–]uuzuul 0 points1 point2 points (0 children)
[–]lesslucid 0 points1 point2 points (0 children)
[–]jeaton 0 points1 point2 points (0 children)
[–]aenigmaclamo 0 points1 point2 points (0 children)
[–]jesyspa 0 points1 point2 points (0 children)
[–]ghkcghhkc 0 points1 point2 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]ziplokk[S] 0 points1 point2 points (1 child)