DEPTH-FIRST SEARCHING USING RECURSION: PROGRAMMING EXERCISES 1. Create a NumDigits application that prompts the user for an integer and then outputs the number of digits in that number. So, for example, the number 542 would return 3 because there are three digits in the number 542. The number 72345 would return 5 because there are five digits in the number 72345. You will need to include a recursive method called numDigits() that returns the number of digits in any given number. HINT: The number of digits in a number (n) is always one more than the number of digits in n/10. Your program output should look something like this: Please enter an integer: 5234 The number 5234 contains 4 digits! Save the project as NumDigits in your UNIT 4 folder. 2. Create an AntColonies application so that it reads slides that are digitized to report colour. For example, a slide file could look like this: 6 8 00550000 00050000 00005500 01200000 01111000 00000030 The digits 1 through 9 represent various colours. The digit 0 represents no colony. When the program launches, the user will need to click the OPEN button to open a text file that contains the data. Recursion Programming Exercises Page 1 of 7 Once the file has been selected, the data from the file should be outputted in the JTextArea on the right. Recursion Programming Exercises Page 2 of 7 When the user clicks REPORT, the program should use a depth-first searching algorithm to determine and display a listing of the size, location, and colour value of each ant colony on the slide. An ant colony is defined as a connected (horizontally or vertically) sequence of cells holding the same colour value. For the above slide, the application should output the following report: Save the project as Ant Colonies in your UNIT 4 folder. 3. Create a MazeRunner application that finds the starting point of a maze (labeled S) and finds a path to the goal (labeled G). The maze must be read from a text file and will be defined as follows: ‘X’ ‘ ’ ‘S’ ‘G’ represents represents represents represents Recursion Programming Exercises walls paths the starting point the goal Page 3 of 7 So the text file would look something like this: 8 10 XXXXXXXXXX XS X XX XXX XXX XX X X XXXX X X X X X XXX X XXXX GX XXXXXXXXXX The first line of the text files indicates how many rows, the second line indicates how many columns and the rest of the file is the maze itself. When the program starts the user will need to click the OPEN button to select a text file using a JFileChooser object: Recursion Programming Exercises Page 4 of 7 Once the file has been read, the maze should be outputted to the JTextArea on the left: When the user clicks the SOLVE button, the program must trace the correct path to get to the goal using a depth-first searching algorithm. You will need to trace the path taken by inserting a ‘.’ Once the maze is solved and the goal is reached, you will need to output the solution to the JTextArea on the right. So the solution to the above maze would look something like this: Recursion Programming Exercises Page 5 of 7 The pseudocode for the algorithm would look something like this: public boolean solveMaze(int row, int col) { If the space above, below, to the left and to the right of your current position equals ‘G’ Insert a ‘.’ at the current position Output the path taken Return true If your current position does not equal ‘S’ Insert a ‘.’ at the current position If the position to the right of your current position is ‘ ’ and the maze is not solved Call the solveMaze method and pass it the position to the right of your current position If the position below your current position is ‘ ’ and the maze is not solved Call the solveMaze method and pass it the position below your current position If the position to the left of your current position is ‘ ’ and the maze is not solved Call the solveMaze method and pass it the position to the left of your current position If the position above your current position is ‘’ and the maze is not solved Call the solveMaze method and pass it the position above your current position If solved equals false Insert a ‘ ’ at your current position Recursion Programming Exercises Page 6 of 7 } Save the project as Maze Runner in your UNIT 4 folder. Recursion Programming Exercises Page 7 of 7