CodingBat: Java. Logic-1, Part II


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


answerCell:

public boolean answerCell(boolean isMorning, boolean isMom, 
		boolean isAsleep) {
	if (isAsleep) return false;
	if (isMorning) return isMom;
	return true;
}

The sample solution on the website is less clean, though:

public boolean answerCell(boolean isMorning, boolean isMom, 
		boolean isAsleep) {
	if (isAsleep) {
		return false;
	}
	if (isMorning && !isMom) {
		return false;
	}
	return true;
}

teaParty:

public int teaParty(int tea, int candy) {
	if (candy < 5 || tea < 5)
		return 0;
	if (candy >= 5 && tea >= 5 && (candy >= 2 * tea || tea >= 2 * candy))
		return 2;
	return 1;
}

twoAsOne:

public boolean twoAsOne(int a, int b, int c) {
		return a + b == c || a + c == b || b + c == a;
}

inOrder:

public boolean inOrder(int a, int b, int c, boolean bOk) {
	if (bOk) return b < c;
	return a < b && b < c;
}
&#91;/sourcecode&#93;

<b>inOrderEqual:</b>
[sourcecode language="Java" gutter="false"]
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {
	if (equalOk) return a <= b && b <= c;
	return a < b && b < c;
}
&#91;/sourcecode&#93;






<b>lastDigit:</b>
[sourcecode language="Java" gutter="false"]
public boolean lastDigit(int a, int b, int c) {
	return a % 10 == b % 10 || a % 10 == c % 10 || b % 10 == c % 10;
}

lessBy10:

public boolean lessBy10(int a, int b, int c) {
	return Math.abs(a - c) >= 10 || Math.abs(a - b) >= 10
			|| Math.abs(b - c) >= 10;
}

withoutDoubles:

public int withoutDoubles(int die1, int die2, boolean noDoubles) {
	if (!noDoubles) return die1 + die2;
	if (die1 == die2) return (die1 != 6) ? die1 + die2 + 1 : die1 + 1;
	return die1 + die2;
}

maxMod5:

public int maxMod5(int a, int b) {
	if (a == b) return 0;
	if (a % 5 == b % 5) return (a < b) ? a : b;
	return (a < b) ? b : a;
}
&#91;/sourcecode&#93;

<b>redTicket:</b>
[sourcecode language="Java" gutter="false"]
public int redTicket(int a, int b, int c) {
	if (a == b && b == c) return (c == 2) ? 10 : 5;
	if (a != b && a != c) return 1;
	return 0;
}

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


6 thoughts on “CodingBat: Java. Logic-1, Part II

  1. Nana

    public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
    if ( !isMorning && !isAsleep && !isMom)
    return true;
    if ( isMorning && isMom && isAsleep)
    return false;
    if ( isMorning && !isMom && isAsleep)
    return false;
    if ( isMorning && isMom && !isAsleep)
    return true;
    if ( !isMorning && isMom && !isAsleep)
    return true;
    return false;

    }

    Reply
    1. Gregor Ulm Post author

      This solution is certainly a lot less appealing than any of the two examples given above in the post.

      Reply
      1. Gregor Ulm Post author

        Adam,
        nesting the ternary operator leads to rather unreadable code. You can try this yourself. Just wait a few weeks, and then try to figure out what the code you wrote was supposed to do, without looking at the exercise description at CodingBat.

        Reply
      2. A Helpful Person

        I was able to use the ternary operator in this way to shorten it:

        answerCell:
        public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
        return isMorning ? isMom && !isAsleep : !isAsleep;
        }

        *I did “!isAsleep” because I want it to return true if isAsleep == false and vise versa.

        Reply
  2. Frsco

    Are there any disadvantages of reducing the inOrder code into a ternary operator, such as this?
    public boolean inOrder(int a, int b, int c, boolean bOk) {
    return (bOk) ? (c > b) : (a < b && b < c);
    }

    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.