Coding Bat – rotateLeft3

This exercise requires that the program look at an array with 3 integers and shift their positions one index to the left (down) and place first integer (index 0) at the end.

Here is the code that you start with:

public int[] rotateLeft3(int[] nums) {

}

As you can see, we start with an empty integer array named “nums”.

public int[] rotateLeft3(int[] nums) {
  int[] rotatedArray = new int[]{nums[1],nums[2],nums[0]};
  return rotatedArray;
}

 

This solution creates a new integer array named “rotatedArray” and assigns it the values of each index from the original array. (Line 2)

We return the new array on line 3 to solve the problem.

Coding Bat – posNeg

This exercise requires that the program look at two given integers and a boolean. If the Boolean is TRUE AND both integers are negative, then TRUE is returned. If the Boolean is FALSE only 1 integer can be negative for to return TRUE.

Here is the code that you start with:

public boolean posNeg(int a, int b, boolean negative) {

}

As you can see, we start with 2 integer variables named “a” and “b” and a boolean named “negative”.

public boolean posNeg(int a, int b, boolean negative) {
  if (negative){
    if (a < 0 && b < 0){
      return true;
    } else {
      return false;
    } 
  } else if ((a < 0 && b > 0) || (a > 0 && b < 0)) {
    return true;
  } else {
    return false;
  }
}

This solution utilizes a nested if/then structure. Starting on line 2, we say IF the Boolean negative is TRUE, then proceed to line 3. If it is FALSE, we would jump to line 8.

On line 3, we now check to see if both “a” AND “b” are negative (less than 0). If they are, TRUE is returned (line 4). If they are not, FALSE is returned (lines 5-7).

Line 8 is executed if the Boolean on line 3 is FALSE. Line 8 looks to see if “a” is negative and “b” is positive OR “a” is positive and “b” is negative. If either case is TRUE, then TRUE is returned (line 9). If neither statement on line 8 is TRUE, then FALSE is returned (lines 10-12).

Coding Bat presents the following as their solution:

public boolean posNeg(int a, int b, boolean negative) {
  if (negative) {
    return (a < 0 && b < 0);
  }
  else {
    return ((a < 0 && b > 0) || (a > 0 && b < 0));
  }
}

In this solution, line 2 is identical to our line 2. However, their line 3 is shorter version of our lines (3-7). If both “a” AND “b” are not negative, then a FALSE is returned, otherwise a TRUE is returned.

Their lines 5-7 are our lines 8-12 and work the same way.

Coding Bat – Arrays Cluster 1

Today, while I was attending meetings, I assigned my students to complete 4 Coding Bat logic exercises.

CODING BAT – COMMONEND

This exercise requires that the program return TRUE if the first integers or last integers of two given arrays are the same, otherwise, FALSE will be returned.

Here is the code that you start with:

public boolean commonEnd(int[] a, int[] b) {

}

We start with two integer arrays named “a” and “b”.

public boolean commonEnd(int[] a, int[] b) {
  int arrayALength = a.length;
  int arrayBLength = b.length;
  int lastA = a[arrayALength - 1];
  int lastB = b[arrayBLength - 1];
  int firstA = a[0];
  int firstB = b[0];
  if(firstA == firstB || lastA == lastB){
    return true;
  }
  return false;
}

The solution above starts by calculating the length of each array (lines 2 and 3). Remember, whenever you are asked to do anything with the “end” or “last” items, you will almost always need to calculate the length.

On lines 4 & 5, we are establishing variables to hold the last index of each array and lines 6 & 7 are holding the first index of each array.

Finally, lines 8 through 10 are a conditional statement comparing the first indices of each array and the last indices of each array. If either of them are equal, a TRUE is returned. However, if neither is TRUE, then FALSE is returned.

CODING BAT – MAKELAST

This exercise requires that the program calculate the length of a given array and create a new array that is double that length populated with zeros with the exception of the last index. The last index is to populated with the last index of the first array.

Here is the code that you start with:

public int[] makeLast(int[] nums) {

}

We start with a single integer variable named “nums”.

public int[] makeLast(int[] nums) {
 int arrayLength = nums.length;
 int arrayFinalLength = arrayLength * 2;
 int arrayLast = nums[arrayLength - 1];
 int[] num = new int[arrayFinalLength];
 num[arrayLength - 1] = arrayLast;
 return num;
}

As always, since we are asked to do some operation with the “last” or “end”, we must calculate the length of the array. This is done on line 2.

On line 3, we created a variable to hold the length of the new array, which is double the length of the original array.

Line 4 is where we capture the last index of the original array.

On line 5, we create a new integer array named “num” which has a size assigned by the variable that was created on line 3.

On line 6, we replace the last index of the new array with the value of the variable created on line 4.

Finally, on line 7, we return the new array.

CODING BAT – MAKEPI

This exercise requires that the program return the first 3 digits of PI in an array.

Here is the code that you start with:

public int[] makePi() {

}

We start with just the basic class declaration and no variables.

public int[] makePi() {
  int pi[] = {3,1,4};
  return pi;
}

The solution above starts by creating a new integer array named “pi” which is assigned the integers of 3, 1, and 4 on line 2.

On line 3, the array is returned.

Many students struggle to make this problem more challenging than it needs to be. Some try to take the Math.PI constant and populate the array one index at a time. However, this is not necessary and is not outlined as a requirement in the problem.

 

Coding Bat – firstLast6

This exercise requires that the program analyze an array of integers and return TRUE if either the first or last integer is a 6. The array is guaranteed to have a minimum length of 1.

Here is the code that you start with:

public boolean firstLast6(int[] nums) {

}

As you can see, we have a single integer array named nums.

public boolean firstLast6(int[] nums) {
 int first = nums[0];
 int last = nums[nums.length-1];
 if (first == 6 || last == 6){
   return true;
 }
 return false;
}

This solution utilizes a conditional if with the Boolean OR to solve the problem.

We start my declaring an integer variable named “first” to retrieve the value of index 0 (line 2).

We then declare an integer variable named “last” to retrieve the value of the last letter (Line 3)

As was the case when working with strings, to retrieve the last index of the array, we must first calculate its length and then subtract 1 from the total length. This will give us the value of the last index of the array.

Lines 4 through 6 are the conditional Boolean OR looking to see if either the first or last number are 6. If the Boolean is TRUE, then TRUE is returned.

Coding Bat – sameFirstLast

This exercise requires that the program analyze an array of integers and return TRUE if the array has a length greater than 1 and the first index is the same as the last index.

Here is the code that you start with:

public boolean sameFirstLast(int[] nums) {

}

As you can see, we have a single integer array named nums.

public boolean sameFirstLast(int[] nums) {
 if (nums.length >= 1){
   int first = nums[0];
   int last = nums[nums.length-1];
   if (first == last){
     return true;
   }
   return false;
   }
 return false;
}

We start on line 2 checking to see if the array has a minimum length of 1. If is does not, we skip to line 10 and return FALSE. If the Boolean on line 2 is true, we proceed to line 3.

We then declare an integer variable named “first” to retrieve the value of index 0 (line 3).

We then declare an integer variable named “last” to retrieve the value of the last letter (Line 4)

As was the case when working with strings, to retrieve the last index of the array, we must first calculate its length and then subtract 1 from the total length. This will give us the value of the last index of the array.

Lines 5 through 7 are the conditional Boolean looking to see if the first and last index are the same value. If the Boolean is TRUE, then TRUE is returned.

Coding Bat – delDel

This exercise requires that the program analyze a string to see if it contains the word “del” starting at index position 1. If it does, it is to remove it and display the word with the “del” removed.

Here is the code that you start with:

public String delDel(String str) {

}

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

public String delDel(String str) {
 if(str.length()>=4 && str.substring(1,4).equals("del")){
 return str.substring(0, 1) + str.substring(4);
 }
 return str;
}

This solution utilizes a conditional if with the Boolean AND to solve the problem.

We start by looking to see if the string is 4 or more characters in length AND if the string starting at index 1 to 4 is “del”.

If this Boolean is TRUE, then we return the string from index 0 to 1 concatenated with the string from index 4 to the end.

If this Boolean is FALSE, then we simply return the string unmodified.

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.

Here is the code that you start with:

public String firstTwo(String str) {
 
}

We start with a single string variable named 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.

Here is the code that you start with:

public String extraEnd(String str) {
 
}

We start with a single string variable named 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.

Here is the code that you start with:

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

We start with two string variables named “a” and “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).

Here is the code that you start with:

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).

Coding Bat – or35

This exercise requires that the program return the value of true if the given number is evenly divisible by either 3 or 5. The instructions encourage us to utilize modulus division to solve this problem.

As a reminder, modulus division is the “remainder” after you solve a division problem.

Here is the code that you start with:

public boolean or35(int n) {
 
}

As you can see, we have a single integer variable named “n”.

public boolean or35(int n) {
  return (n % 3 == 0) || (n % 5 == 0);
}

The provided solution does not utilize variables beyond the single provided n.

public boolean or35(int n) {
 double modFive = n % 5;
 double modThree = n % 3;
 if (modFive == 0 || modThree == 0){
 return true;
 }
 return false;
}

An alternative solution could use variables for the division and inside the conditional statements.

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.

Coding Bat – in3050

This exercise requires that the program return the value of true if both of two numbers entered are between 30 and 40 (inclusive) or between 40 and 50 inclusive.

Here is the code that you start with:

public boolean in3050(int a, int b) {
  
}

As you can see, we have two integer variables named “a” and “b”.

public boolean in3050(int a, int b) {
  if (a >= 30 && a <= 40 && b >= 30 && b <= 40) {
    return true;
  }
  if (a >= 40 && a <= 50 && b >= 40 && b <= 50) {
    return true;
  }
  return false;
}

In the solution provided, we have a Boolean that returns true if A is any of the following values:

30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40

AND if B is any of the following values:

30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40

We have a second conditional statement that returns true if A is any of the following values:

40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50

AND if B is any of the following values:

40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50

Otherwise, a false is returned.

An alternate solution could also be:

public boolean in3050(int a, int b) {
  if ((a >= 30 && a <= 40 && b >= 30 && b <= 40) || (a >= 40 && a <= 50 && b >= 40 && b <= 50)) {
    return true;
  }
  return false;
}

Here, we have combined the else if portion of the original solution into a BOOLEAN or statement.