CodingBat: Java. Warmup-1, Part II


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


This post continues where the last one left off. It contains solutions to another ten exercises, most of which are straightforward.

frontBack:

	public String frontBack(String str) {
		int length = str.length();
		if (length <= 1) return str;
		return str.charAt(length - 1) + str.substring(1, length - 1)
				+ str.charAt(0);
	}
&#91;/sourcecode&#93;

<b>front3:</b>
[sourcecode language="Java" gutter="false"]
	public String front3(String str) {
		int length = str.length();
		if (length <= 3) return str + str + str;
		String front = str.substring(0, 3);
		return front + front + front;
	}
&#91;/sourcecode&#93;

<b>backAround:</b>
[sourcecode language="Java" gutter="false"]
	public String backAround(String str) {
		char add = str.charAt(str.length() - 1);
		return add + str + add;
	}

front22:

	public String front22(String str) {
		if (str.length() < 2)
			return str + str + str;
		String firstTwo = str.substring(0, 2);
		return firstTwo + str + firstTwo;
	}
&#91;/sourcecode&#93;

<b>startHi:</b>
[sourcecode language="Java" gutter="false"]
	public boolean startHi(String str) {
		if (str.length() < 2)
			return false;
		return str.substring(0, 2).equals("hi");
	}
&#91;/sourcecode&#93;

<b>icyHot:</b>
[sourcecode language="Java" gutter="false"]
	public boolean icyHot(int temp1, int temp2) {
		return (temp1 < 0 && temp2 > 100 || temp1 > 100 && temp2 < 0);
	}
&#91;/sourcecode&#93;

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

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

<b>loneTeen:</b>
[sourcecode language="Java" gutter="false"]
	public boolean loneTeen(int a, int b) {
		int count = 0;
		if (a >= 13 && a <= 19) count++;
		if (b >= 13 && b <= 19) count++;
		return count == 1;
	}
&#91;/sourcecode&#93;

This solution might strike you as odd at first. My goal was to avoid a cumbersome boolean expression. Thus, I maintain a counter that increases by one if one of the integers is in the specified range. If the value of count is "1", then the boolean XOR condition is fulfilled. You’ll probably agree that this approach is slightly more elegant than the solution on the website:

&#91;sourcecode language="Java" gutter="false"&#93;
	public boolean loneTeen(int a, int b) {
		boolean aTeen = (a >= 13 && a <= 19);
		boolean bTeen = (b >= 13 && b <= 19);
		return (aTeen && !bTeen) || (!aTeen && bTeen);
		// Translation: one or the other, but not both.
		// Alternately could use the Java XOR operator, but it's obscure.
	}
&#91;/sourcecode&#93;

The XOR operator (^) wasn’t mentioned on the CodingBat website. If you use it, the solution shrinks down to just one line:
&#91;sourcecode language="Java" gutter="false"&#93;
	public boolean loneTeen(int a, int b) {
		return (a >= 13 & a <= 19) ^ (b >= 13 && b <= 19);
	}
&#91;/sourcecode&#93;

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

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


5 thoughts on “CodingBat: Java. Warmup-1, Part II

  1. Ryan Memmott

    For front3 I would not store str.length () in a variable since it is only called once. In addition since we only care about the first 3 existing place of the string, we can recycle the existing variable str to only contain the part of the string we need. This also saves a line of code.
    if (str.length() >= 3) str=str.substring(0, 3);
    return str + str + str;

    Reply
  2. John Doe

    Could someone explain me the loneTeen problem?

    I mean if the teen number is in the variable c instead of the other ones how does it get checked if it’s in there?

    Reply

Leave a Reply to Ryan Memmott Cancel 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.