Problem:
Given three ints, a b c, return true if one of them is 10 or more less than one of the others.
lessBy10(1, 7, 11) → true
lessBy10(1, 7, 10) → false
lessBy10(11, 1, 7) → true
Solution:
public boolean lessBy10(int a, int b, int c) { int high = Math.max(a,b); high = Math.max(high, c); if (high - a >= 10 || high - b >= 10 || high - c >=10) return true; else return false; }
public boolean lessBy10(int a, int b, int c) {
ReplyDeleteint max = Math.max(a,Math.max(b,c));
int min = Math.min(a,Math.min(b,c));
int dif = max - min;
if (dif >= 10) return true;
else return false;
}
if (high - a >= 10 || high - b >= 10 || high - c >=10)
ReplyDeletereturn true;
else
return false;
can be shortened to:
return (high - a >= 10 || high - b >= 10 || high - c >=10)
return Math.abs(a - b) >= 10 || Math.abs(b - c) >= 10 || Math.abs(a - c) >= 10;
ReplyDeleteWhat does ...abs mean..in math.abs
DeleteReturn the absolte value
Deletereturn Math.abs(a - b) >= 10 ? true
ReplyDelete: Math.abs(b - c) >= 10 ? true
: Math.abs(a - c) >= 10 ? true
: false;
return (Math.abs(a-b) >=10) || (Math.abs(b-c) >=10) || (Math.abs(c-a) >=10);
ReplyDeleteCan we do this program without using math.abs
ReplyDeleteyes we can.
Deletepublic boolean lessBy10(int a, int b, int c) {
if((a-b>=10) || (b-c>=10) || (c-a>=10) || (b-a>=10) || (c-b>=10) || (a-c>=10))
{
return true;
}
else
{
return false;
}
}
return Math.abs(a-b)>=10||Math.abs(a-c)>=10||Math.abs(c-b)>=10;
ReplyDelete"Yes we can do program without using Math.abs"
ReplyDeleteif(a-b>=10 || b-c>=10 || a-c>=10 || c-b>=10 || c-a>=10)
{
return true;
}
else
{
return false;
}
saikumar the scintist.
Deletereturn true ? (Math.abs(a-b) >= 10 || Math.abs(a-c) >= 10 || Math.abs(b-c) >= 10) : false;
ReplyDeletepublic boolean lessBy10(int a, int b, int c) {
ReplyDeletereturn ((a-b>=10) || (b-a>=10) || (b-c>=10) || (c-b>=10) || (a-c>=10) || (c-a>=10));
}
return ((a-b>=10) || (b-a>=10) || (b-c>=10) || (c-b>=10) || (a-c>=10) || (c-a>=10));
ReplyDelete