For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.
atFirst:
public String atFirst(String str) {
if (str.length() == 0) return "@@";
if (str.length() == 1) return str + "@";
return str.substring(0, 2);
}
Let’s talk about style and conciseness for a moment. My solution of “atFirst” is probably a bit shorter than what some coding style guides would suggest. If you look at it, you’ll have no problem with the control flow, and it takes a mere three lines. Furthermore, there is no visual clutter.
On the other hand, some people tend to add if/else structures when they are unnecessary. This is a waste of time and screen space. But let’s just follow some “best practices” and keep the return statements on separate lines. The result is quite unsightly: Six lines instead of three, and two superfluous key words. Three versus six lines might not be a big absolute difference, but imagine you worked on a program that is several thousand lines long. You can easily waste a couple hundred lines like this.
public String atFirst(String str) {
if (str.length() == 0)
return "@@";
else if (str.length() == 1)
return str + "@";
else
return str.substring(0, 2);
}
If this wasn’t bad enough, you can of course add curly braces and end up with code that’s just painful to look at:
public String atFirst(String str) {
if (str.length() == 0) {
return "@@";
} else if (str.length() == 1) {
return str + "@";
} else {
return str.substring(0, 2);
}
}
This isn’t even the worst you could do. For instance, the so-called Allman style dictates that you put braces on separate lines.
lastChars:
public String lastChars(String a, String b) {
if (a.length() == 0 && b.length() != 0)
return "@" + "" + b.charAt(b.length() - 1);
if (b.length() == 0 && a.length() != 0)
return a.charAt(0) + "@";
if (a.length() == 0 && b.length() == 0)
return "@@";
return a.charAt(0) + "" + b.charAt(b.length() - 1);
}
Without the empty string, the return value would be a numerical ASCII value.
conCat:
public String conCat(String a, String b) {
if (a.length() != 0 && b.length() != 0
&& a.charAt(a.length() - 1) == b.charAt(0))
return a + b.substring(1);
return a + b;
}
lastTwo:
public String lastTwo(String str) {
if (str.length() < 2) return str;
return str.substring(0, str.length()-2)
+ str.charAt(str.length()-1) + str.charAt(str.length()-2);
}
[/sourcecode]
<b>seeColor:</b>
[sourcecode language="Java" gutter="false"]
public String seeColor(String str) {
if (str.length() >= 3 && str.substring(0, 3).equals("red"))
return "red";
if (str.length() >= 4 && str.substring(0, 4).equals("blue"))
return "blue";
return "";
}
frontAgain:
public boolean frontAgain(String str) {
int len = str.length();
if (len < 2) return false;
return str.substring(0, 2).equals(str.substring(len - 2));
}
[/sourcecode]
<b>minCat:</b>
[sourcecode language="Java" gutter="false"]
public String minCat(String a, String b) {
int lenA = a.length();
int lenB = b.length();
if (lenA == lenB) return a + b;
if (lenA > lenB) return a.substring(lenA - lenB) + b;
return a + b.substring(lenB - lenA);
}
extraFront:
public String extraFront(String str) {
String front = "";
if (str.length() < 2) front = str;
else front = str.substring(0, 2);
return front + front + front;
}
[/sourcecode]
<b>without2:</b>
[sourcecode language="Java" gutter="false"]
public String without2(String str) {
int len = str.length();
if (len == 0 || len == 2) return "";
if (len == 1) return str;
if (str.substring(len - 2).equals(str.substring(0, 2)))
return str.substring(2);
return str;
}
deFront:
public String deFront(String str) {
if (str.charAt(0) == 'a' && str.charAt(1) != 'b')
return str.charAt(0) + str.substring(2);
if (str.charAt(0) != 'a' && str.charAt(1) == 'b')
return str.substring(1);
if (str.charAt(0) == 'a' && str.charAt(1) == 'b')
return str;
return str.substring(2);
}
For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.