In: Computer Science
Objective: The goal of this lab is to practice (ragged) 2D arrays and simple recursion (in two separate parts). You are not allowed to use any of the built-in classes for this lab. If you find yourself needing to import anything at the top of your class, you are doing it wrong.
Part A
a) Create a class method called printArray2D that accepts a 2D integer array and prints out the values formatted such that each value occupies 4 spaces (using System.out.printf), and each row appears on its own line, beginning and ending with a vertical “pipe” | character (Shift + key above Enter key). (See output below for example.)
b) Create a class method called fillArray2D that accepts an integer array (1D) and "fills in the gaps" between adjacent cells in the array as rows of a 2D array. If the first slot in the array is a 2 and the second slot in the array is a 6, then the first row of your 2D array will be 2, 3, 4 and 5, and the next row will start with 6. You may assume that the numbers passed in are in ascending order and there are no duplicates.
This method is best explained with an example.
In main: // Create array and then print the filled array int[] arr = {2,4,8,14,19,20,24,25}; print2DArray(fillArray2D(arr));
Output:
| 2 3|
| 4 5 6 7|
| 8 9 10 11 12 13|
| 14 15 16 17 18|
| 19|
| 20 21 22 23|
| 24|
| 25|
The returned 2D array must be full. In other words, do not have extra slots at the end that are considered "empty." The original array passed in as the argument must remain unchanged.
Part B
Write a method reverseRec that accepts a String parameter as input and returns the reverse of that String. The method must be recursive (i.e., it must call itself). As a hint, consider the factorial example from class, as well as the method in the quiz question at the end of the slides. Remember, you’ll need a base case(s) and a recursive case. Provide several test cases calling your recursive method from main.
For example, System.out.println(reverseRec("theseareafewofmyfavoritestrings"));
Output: sgnirtsetirovafymfowefaeraeseht
public class Solution {
public static void main(String[]
args){
int[] arr = {2,
4, 8, 14, 19, 20, 24, 25};
printArray2D(fillArray2D(arr));
System.out.println(reverseRec("racecar"));
System.out.println(reverseRec("reverseMe"));
System.out.println(reverseRec("theseAreFewOfMyFavouriteStrings"));
}
static void printArray2D(int[][]
input){
for (int i = 0 ;
i < input.length ; i++){
//print pipe at starting of each line
System.out.printf("|");
for (int j = 0 ; j < input[i].length ;
j++){
//print each element of array
occupying space of 4
System.out.printf("%4d",
input[i][j]);
}
System.out.printf("|\n");
}
}
static int[][] fillArray2D(int input[]){
int
output[][] = new int[input.length][];
if (input.length
== 0){
return output;
}
for (int i = 1 ; i < output.length ; i++){
// fill (i-1)th row of output array with values
from input[i-1] to input[i]
output[i-1] = new int[input[i] -
input[i-1]];
for (int j = 0 ; j < output[i-1].length ;
j++){
output[i-1][j] = input[i-1] +
j;
}
}
//fill last
row of output array with last element of input array
output[output.length - 1] = new int[1];
output[output.length - 1][0] = input[input.length - 1];
return
output;
}
static String reverseRec(String
input){
//base case - if
input is empty string return empty string as reverse of input
if
(input.equals("")){
return "";
}
// recursive
case
return
input.charAt(input.length() - 1) + reverseRec(input.substring(0,
input.length() - 1));
}
}