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); } [/sourcecode] <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; } [/sourcecode] <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; } [/sourcecode] <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"); } [/sourcecode] <b>icyHot:</b> [sourcecode language="Java" gutter="false"] public boolean icyHot(int temp1, int temp2) { return (temp1 < 0 && temp2 > 100 || temp1 > 100 && temp2 < 0); } [/sourcecode] <b>in1020:</b> [sourcecode language="Java" gutter="false"] public boolean in1020(int a, int b) { return (a >= 10 && a <= 20 || b >= 10 && b <= 20); } [/sourcecode] <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); } [/sourcecode] <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; } [/sourcecode] 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: [sourcecode language="Java" gutter="false"] 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. } [/sourcecode] The XOR operator (^) wasn’t mentioned on the CodingBat website. If you use it, the solution shrinks down to just one line: [sourcecode language="Java" gutter="false"] public boolean loneTeen(int a, int b) { return (a >= 13 & a <= 19) ^ (b >= 13 && b <= 19); } [/sourcecode] <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.