CodingBat: Java. Warmup-1, Part I


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


The section Warmup-1 contains 30 short exercises to help you getting familiar with basic operations in Java, covering simple boolean operations and string manipulations. I’ve checked my solutions on CodingBat again on 2 January 2013, and they passed all tests.

This post contains solutions to the first ten exercises.

sleepIn:

	public boolean sleepIn(boolean weekday, boolean vacation) {
		return (!weekday || vacation);
	}

monkeyTrouble:

	public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
		return ((aSmile && bSmile) || (!aSmile && !bSmile));
	}

sumDouble:

	public int sumDouble(int a, int b) {
		if (a == b) return 2 * (a + b);
		return a + b;
	}

A note on coding style: Compared to more modern languages, Java is quite verbose, which is one of the reasons why I prefer to write more compact code. One consequence is that I omit superfluous curly braces, like in sumDouble. According to the official Java style guide you are not supposed to do that, though. Further, I am not overly fond of excessive use of variables.

The above method could also have been written as:

	public int sumDouble(int a, int b) {
		int sum = a + b;
		if (a == b) {
			sum = sum * 2;
		} 
		return sum;
	}

I consider this alternative to be inferior because it is less succinct. You’ll spend more time writing it, and you waste the time of everyone who’s ever going to read your code. Stressing this point in this example may sound silly, but there are sound practical reasons. Once you write larger programs, you’ll probably find it easier to organize your work if you wrote in a less verbose style.

diff21:

	public int diff21(int n) {
		if (n > 21)
			return 2 * Math.abs(n - 21);
		return Math.abs(n - 21);
	}

Like in the previous example, you could have used the ternary operator, which allows you to condense this method into just one line:

	public int diff21(int n) {
		return (n > 21) ? 2 * Math.abs(n - 21) : Math.abs(n - 21);
	}

The CodingBat website does not refer to the ternary operator, which is why I will not use it in my solutions. Some programmers find the ternary operator confusing. Yet, you should at least know that it exists.

parrotTrouble:

	public boolean parrotTrouble(boolean talking, int hour) {
		return ((talking && hour < 7) || (talking && hour > 20));
	}

A slightly more succinct solution is:

	public boolean parrotTrouble(boolean talking, int hour) {
		return talking && (hour < 7 || hour > 20);
	}

makes10:

	public boolean makes10(int a, int b) {
		return ((a == 10) || (b == 10) || (a + b == 10));
	}

nearHundred:

	public boolean nearHundred(int n) {
		return (Math.abs(100 - n) <= 10 || Math.abs(200 - n) <= 10);
	}
&#91;/sourcecode&#93;

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

<b>notString:</b>
[sourcecode language="Java" gutter="false"]
	public String notString(String str) {
		if (str.length() >= 3 && str.substring(0, 3).equals("not"))
			return str;
		return "not " + str;

	}

Please note that you are not supposed to use str.startsWith().

missingChar:

	public String missingChar(String str, int n) {
		return str.substring(0, n) + str.substring(n + 1);
	}

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


8 thoughts on “CodingBat: Java. Warmup-1, Part I

  1. Ryan Memmott

    For warmup-1: monkeyTrouble.
    return aSmile==bSmile;

    posNeg
    return negative?a<0&&b<0:a<0^b<0;
    or for non ternary solution.
    if (negative) return a < 0 && b < 0;
    return a < 0 ^ b < 0;

    Reply
  2. Clover Ernest

    for Warmup – 1:mokeyTrouble.
    why are we in trouble if both monkeys are smiling or both are not smiling?
    i think the monkeys are plotting something BIG. we need to get ready, because this is about to become Dawn Of The Planet Of The Apes.

    Reply
  3. Chris

    parrotTrouble:
    public boolean parrotTrouble(boolean talking, int hour) {
    return talking && !(7 <= hour && hour <= 20);
    }

    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.