Coding Bat – Cluster 1

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 – helloName

This exercise requires that the program return an entered name with the word Hello appended to the front and an exclamation point following the name.

Here is the code that you start with:

public String helloName(String name) {
  
}

We start with a string variable named name.

public String helloName(String name) {
  return "Hello " + name + "!";
}

The solution above simply uses the concatenate function to append “Hello ” in front of the name variable and the “!” following it.

Coding Bat – makeAbba

This exercise requires that the program take two input strings of text and place them into the A-B-B-A order.

For example, let’s say that string A is “cat” and string B is “dog”. The output would be catdogdogcat.

Here is the code that you start with:

public String makeAbba(String a, String b) {
  
}

We start with two string variables named a and b.

public String makeAbba(String a, String b) {
  return a + b + b + a;
}

The solution above simply uses the concatenate function to append variable a to variable b to variable b to variable a.

Coding Bat – makeTags

This exercise requires that the program take two entered strings of which the first is an HTML tag and create a formatted HTML instruction with open and closing tags enclosing the second entered string.

Here is the code that you start with:

public String makeTags(String tag, String word) {
  
}

We start with two string variables named tag and word.

public String makeTags(String tag, String word) {
  return "<" + tag + ">" + word + "</" + tag + ">";
}

The solution above simply uses the concatenate function to append the correct text and variables in an HTML syntax structure.

Coding Bat – withoutX

This exercise requires that the program take an entered string that if it start with or ends with “x” the “x” is removed. If there is an “x” in the middle of the letter, it remains.

Here is the code that you start with:

public String withoutX(String str) {
 
}

As you can see, we have a string variable named str.

public String withoutX(String str) {
 int count = str.length();
 if (count == 0){
 return str;
 }
 int secondToLastLetter = count - 1;
 String firstLetter = str.substring(0, 1);
 String lastLetter = str.substring(secondToLastLetter, count);
 if (str.equals("x")){
 str = "";
 }
 else if (firstLetter.equals("x") && (lastLetter.equals("x"))){
 str = str.substring(1,secondToLastLetter);
 }
 else if (firstLetter.equals("x")){
 str = str.substring(1,count);
 }
 else if (lastLetter.equals("x")){
 str = str.substring(0,secondToLastLetter);
 }
 return str;
}

You can see that this problem was little bit more involved because the testing data set included an empty string and a string that was just a single letter x.

Lines 3 – 5 address the situation of a null or empty string.

Lines 6 – 8 create local variables to be used in the other cases presented.

Lines 9 – 11 address the situation of only the letter x.

Lines 12 – 14 address the situation of an x at the front AND an x at the end.

Lines 15 – 17 address the situation of an x only at the front.

Lines 18 – 20 address the situation of an x only at the end.

Line 21 returns what the string will look like with the necessary letter x’s removed.

Leave a Reply

Your email address will not be published. Required fields are marked *