CodingBat: Java. Logic-1, Part I


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;
}
&#91;/sourcecode&#93;


<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);
}
&#91;/sourcecode&#93;


<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;
}
&#91;/sourcecode&#93;

<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;
}
&#91;/sourcecode&#93;


<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";
&#91;/sourcecode&#93;

If you think this is too concise, then compare it with a solution that avoids the ternary operator:

&#91;sourcecode language="Java" gutter="false"&#93;
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";
}
&#91;/sourcecode&#93;

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.


12 thoughts on “CodingBat: Java. Logic-1, Part I

  1. John Doe

    In in1To10 could you explain your return?

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

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

    Reply
    1. Gregor Ulm Post author

      That’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.

      Reply
  2. Adam

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

    Reply
  3. Ronnie

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

    Reply
  4. Ankit Shah

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

    Reply
  5. Jafar Ali

    found an easier solution to dateFashion:

    public int dateFashion(int you, int date) {
    if(you <= 2 || date = 8 || date >= 8) return 2;
    else return 1;
    }

    Reply
    1. Jafar Ali

      public int dateFashion(int you, int date) {
      if(you <= 2 || date = 8 || date >= 8) return 2;
      else return 1;
      }
      it messed it up idk why

      Reply
      1. WOJTEK

        no logic – thats why

        solution

        public int dateFashion(int you, int date) {
        if(you<3 || date7 || date>7) return 2;
        return 1;
        }

        Reply
  6. Anu

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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Spammer prevention; the answer is an integer: * Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.