**For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.**

In the Logic-1 section of CodingBat the pace picks up a bit. None of the exercises should make you break a sweat, but it’s quite easy to write unnecessarily convoluted if/else statements. Many of my solutions make use of the ternary operator to save some vertical space and increase readability.

All 24 solutions were successfully tested on 4 February 2013.

**cigarParty:**

public boolean cigarParty(int cigars, boolean isWeekend) { if (isWeekend) return (cigars >= 40); return cigars >= 40 && cigars <= 60; } [/sourcecode] <b>dateFashion:</b> [sourcecode language="Java" gutter="false"] public int dateFashion(int you, int date) { if (you <= 2 || date <= 2) return 0; return (you >= 8 && date >= 2 || date >= 8 && you >= 2) ? 2 : 1; }

**squirrelPlay:**

public boolean squirrelPlay(int temp, boolean isSummer) { return (isSummer) ? (temp >= 60 && temp <= 100) : (temp >= 60 && temp <= 90); } [/sourcecode] <b>caughtSpeeding:</b> [sourcecode language="Java" gutter="false"] public int caughtSpeeding(int speed, boolean isBirthday) { if (isBirthday) speed -= 5; if (speed <= 60) return 0; return (speed > 60 && speed <= 80) ? 1 : 2; } [/sourcecode] <b>sortaSum:</b> [sourcecode language="Java" gutter="false"] public int sortaSum(int a, int b) { return (a + b >= 10 && a + b <= 19) ? 20 : a + b; } [/sourcecode] <b>alarmClock:</b> [sourcecode language="Java" gutter="false"] if (vacation) return (day >= 1 && day <= 5) ? "10:00" : "off"; return (day >= 1 && day <= 5) ? "7:00" : "10:00"; [/sourcecode] If you think this is too concise, then compare it with a solution that avoids the ternary operator: [sourcecode language="Java" gutter="false"] public String alarmClock(int day, boolean vacation) { if (vacation) { if (day >= 1 && day <= 5) return "10:00"; return "off"; } if (day >= 1 && day <= 5) return "7:00"; return "10:00"; } [/sourcecode] Ugly, isn't it? <b>love6:</b> [sourcecode language="Java" gutter="false"] public boolean love6(int a, int b) { return a == 6 || b == 6 || a + b == 6 || Math.abs(a - b) == 6; }

**in1To10:**

public boolean in1To10(int n, boolean outsideMode) {

return (outsideMode) ? n <= 1 || n >= 10 : n >= 1 && n <= 10;
}
[/sourcecode]**nearTen:**

public boolean nearTen(int num) {

return num % 10 >= 8 || num % 10 <= 2;
}
[/sourcecode]**teenSum:**

public int teenSum(int a, int b) {

return (a >= 13 && a <= 19 || b >= 13 && b <= 19) ? 19 : a + b;
}
[/sourcecode]

**For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.**

John DoeIn in1To10 could you explain your return?

return (outsideMode) ? n = 10 : n >= 1 && n <= 10;

Mostly the "?" and ":" those are new to me?

Gregor UlmPost authorThat’s the so-called ternary operator, which is a shorthand for if-then-else. I’m personally divided on its usage, since you trade succinctness for readability.

AdamHere was my solution to the in1To10 problem

public boolean in1To10(int n, boolean outsideMode) {

return (outsideMode ? outside(n) : inside(n));

}

public boolean inside(int a){

return (a >= 1 && a <= 10 ? true : false);

}

public boolean outside(int a){

return (a = 10 ? true : false);

}

asdnearTen:

return (num+2)%10<5;

Gregor UlmPost authorAs you can see, supposedly clever solutions are not always the most readable.

RonnieHi, here’s my code using no shorthands. Please excuse bad spacing.

public boolean in1To10(int n, boolean outsideMode) {

if (outsideMode){

if (n = 10) return true;

else return false;

} else {

if (n >=1 && n <= 10) return true;

else return false;

}

}

RonnieWell for some reason my post submission cut off some of my code. Here’s a pastebin link.

http://pastebin.com/TjEDSSSD

Ankit ShahWithout if two liner solution in java for Logic-1 > caughtSpeeding

public int caughtSpeeding(int speed, boolean isBirthday) {

int add = isBirthday ? 5 : 0;

return speed = 81 + add ? 2 : 1;

}

Jafar Alifound an easier solution to dateFashion:

public int dateFashion(int you, int date) {

if(you <= 2 || date = 8 || date >= 8) return 2;

else return 1;

}

Jafar Alipublic int dateFashion(int you, int date) {

if(you <= 2 || date = 8 || date >= 8) return 2;

else return 1;

}

it messed it up idk why

WOJTEKno logic – thats why

solution

public int dateFashion(int you, int date) {

if(you<3 || date7 || date>7) return 2;

return 1;

}

AnuReally learned a lot from your samples, Gregor Ulm. Thanks for your solutions.

I am able to solve codingbat however your solutions are much simpler.

public boolean specialEleven(int n) {

return (n%11==0 || n%11==1);

}