Coding Bat – Cluster 2

Today, while I was working with the FTC 11242 (ERROR 404) robotics team at Ferris Intermediate School for our community outreach efforts, my Computer Science class was assigned to complete 4 different Coding Bat assignments.

CODING BAT – firstTwo

This exercise requires that the program return only the first two characters of a provided string.

```public String firstTwo(String str) {

}```

```public String firstTwo(String str) {
if (str.length() < 2){
return str;
}
String firstTwo = str.substring(0,2);
return firstTwo;
}```

The solution above first uses the length method of the String class to determine if the string is less than 2 characters long. If it is, then the entered string is returned as the solution (lines 2 – 4).

Lines 5 – 6 are executed if the string is 2 or more characters in length and uses the substring method of the String class to capture the characters from index 0 to index 2 (first 2 letters of the string) and assigns them to the variable named firstTwo and then returns the value of that variable.

CODING BAT – extraEnd

This exercise requires that the program take the last two letters of the provided string and create a new string that repeats them 3 times.

```public String extraEnd(String str) {

}```

```public String extraEnd(String str) {
int strLength = str.length();
String lastTwo = str.substring(strLength - 2, strLength);
String extraEnd = lastTwo + lastTwo + lastTwo;
return extraEnd;
}```

Whenever we are doing something involving the end of a string, we must first calculate the length of the string (line 2).

Once we know the length of the string, we can come back 2 characters (line 3).

We then use concatenate to repeat the last 2 characters 3 times (line 4) and output the final solution (line 5).

CODING BAT – conCat

This exercise requires that the program take two entered strings and concatenate the first with the last. However, if the last character of the first string and the first character of the last string are the same, they are NOT to be repeated and one is to be removed.

```public String conCat(String a, String b) {

}```

```public String conCat(String a, String b) {
int aLength = a.length();
int bLength = b.length();
if (aLength == 0 || bLength == 0){
return a + b;
}
String aLastLetter = a.substring(aLength - 1, aLength);
String bFirstLetter = b.substring(0,1);
if (aLastLetter.equals(bFirstLetter)){
return a + b.substring(1, bLength);
}
return a + b;
}```

The solution above first determines the length of the two strings (lines 2 – – 3). We then look to see if either of the strings is empty (lines 4 – 6) and return the strings as they are.

If both of the strings have text, we then look to see what the last letter of string a is (line 7) and the first letter of string b (line 8). We then compare these two letters to see if they are the same (line 9). If they are, we return string a concatenated with string b with the first letter of string b removed.

If line 9 is false, then we simply return string a concatenated with string b.

CODING BAT – theEnd

This exercise requires that the program display either the first letter of the string (if the Boolean front is true) or the last letter of the string (if the Boolean front is false).

```public String theEnd(String str, boolean front) {

}```

As you can see, we have a string variable named str and a Boolean with the value of front.

```public String theEnd(String str, boolean front) {
int strLength = str.length();
String strFirstLetter = str.substring (0,1);
String strLastLetter = str.substring (strLength - 1, strLength);
if (front == true){
return strFirstLetter;
}
return strLastLetter;
}```

As always, when we’re asked to do anything with the end of a string, we must first calculate the length of the string (line 2).

We then create variables for the first letter and last letter of the string using the substring method of the String class (lines 3 – 4).

Line 5 looks to see if the Boolean is true. If it is, we return the value of the variable for the first letter of the string (line 6). Otherwise, we return the value of the variable for the last letter of the string (line 8).