Question

In: Computer Science

Objective: The goal of this lab is to practice (ragged) 2D arrays and simple recursion (in...

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

Solutions

Expert Solution

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));
       }
   }


Related Solutions

Question Objective: The objective of this lab exercise is to give you practice in programming with...
Question Objective: The objective of this lab exercise is to give you practice in programming with one of Python’s most widely used “container” data types -- the List (commonly called an “Array” in most other programming languages). More specifically you will demonstrate how to: Declare list objects Access a list for storing (i.e., writing) into a cell (a.k.a., element or component) and retrieving (i.e., reading) a value from a list cell/element/component Iterate through a list looking for specific values using...
The objective of this lab is to practice your Verilog coding and the design of a...
The objective of this lab is to practice your Verilog coding and the design of a Finite State Machine. Lab Goal: For this lab, you will code a Verilog module to implement the FSM described in this document. This lab will also require that you use the Seven - Segment Display on the DE0 - CV FPGA board. Design Specifications for the FSM Implem ent the following simple state machine on the DE0 - CV FPGA board. This FSM will...
The goal of this lab is to write a simple, but functional, web server that is...
The goal of this lab is to write a simple, but functional, web server that is capable of sending files to a web browser on request. The web server must create a listening socket and accept connections. It must implement just enough of the HTTP/1.1 protocol to enable it to read requests for files and to send the requested files. It should also be able to send error responses to the client when appropriate. It would be useful to see...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please have a screenshot of output) In competitive diving, each diver makes dives of varying degrees of difficulty. Nine judges score each dive from 0 through 10 in steps of 0.5. The difficulty is a floating-point value between 1.0 and 3.0 that represents how complex the dive is to perform. The total score is obtained by discarding the lowest and highest of the judges’ scores,...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
What is the objective, goal, abstract, and purpose of Inertial lab in physics report. And what...
What is the objective, goal, abstract, and purpose of Inertial lab in physics report. And what would be the conclusion.
In this lab, you will practice the use of array by building an simple maze game,...
In this lab, you will practice the use of array by building an simple maze game, where your task is to control the prince to defeat the monster and save Snow White. You have to work based on the given skeleton code. Game Description The Snow White is detained in the maze by the monster, who needs the prince's help to rescure her out from the exit. The pre-set maze is shown below, with width 11 and height 8. The...
In this lab, you will practice the use of array by building an simple maze game,...
In this lab, you will practice the use of array by building an simple maze game, where your task is to control the prince to defeat the monster and save Snow White. You have to work based on the given skeleton code. Game Description The Snow White is detained in the maze by the monster, who needs the prince's help to rescure her out from the exit. The pre-set maze is shown below, with width 11 and height 8. The...
Objective: To provide practice (or a refresher) with writing simple MATLAB codes that employ common tools...
Objective: To provide practice (or a refresher) with writing simple MATLAB codes that employ common tools such as loops, functions, arrays, and MATLAB plotting tools. P T 1: Functions and loops The goal of this exercise is to plot the function r(theta) = theta where theta is a phase angle and r(theta) is the radius at that angle, over the range - 3pi/2 =< theta =< 3pi/2 . It is often useful to define a function to handle calculations that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT