You're ahead of schedule this week, which means today's a chance to build real reps instead of busywork. Same CodingBat you'll use all year — today we're just being deliberate about one specific skill: methods that return true or false.
This only takes 30 seconds and you only ever do it once. CodingBat only shows me your progress if you share it with me directly — I can't see it otherwise.
Only do this once — it stays linked for every problem you ever do on CodingBat, not just today's.
Read through how boolean methods actually work, plus a quick note on the % operator.
Run the quick predict-the-output check below to make sure the pattern actually clicked.
Go to CodingBat and clear the 13 boolean problems checked off below. One green check, move on.
A method declared boolean has exactly one job: hand back true or false. Nothing else compiles. There are two ways to do that — one is safer to reason about, the other is what you should be reaching for by the end of today.
public boolean isPositive(int n) { if (n > 0) { return true; } else { return false; } }
public boolean isPositive(int n) { return n > 0; }
The trick: n > 0 is already a boolean expression — it evaluates to true or false all on its own. You don't need an if/else to "convert" it into one. Just hand it back directly.
% operatorA few of today's problems use % ("mod"). For now, all you need is this: n % 2 == 0 means n is even, and n % 2 == 1 means n is odd. You'll see it used with other numbers too, like %3 or %10 — same idea. We'll dig into why it actually works later.
Five short ones. Click what you think each one returns before you head to CodingBat.
Every problem below returns boolean and only uses numbers or booleans — no strings, since we haven't covered those yet. Check them off as you go; it's saved automatically if you close the tab.