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

```public boolean commonEnd(int[] a, int[] 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.

```public int[] makeLast(int[] 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.

```public int[] makePi() {

}```

```public int[] makePi() {