CSC171 Lab 14 - Adam Purtee PDF

Title CSC171 Lab 14 - Adam Purtee
Course Science Of Programming - Lab
Institution University of Rochester
Pages 2
File Size 31.6 KB
File Type PDF
Total Downloads 9
Total Views 133

Summary

Adam Purtee...


Description

CSC171 — Lab 14 Recursion The goal of this activity is to give you experience with recursion and writing recursive methods (functions). Be sure that you are comfortable tracing the execution of these functions: what values are passed, what happens to them, how the parameters and local variables work, what is returned (if any), and how the return value gets used. These are not new concepts, but recursive methods test that you really understand it all.

Questions 1. Write a recursive function to find the minimum element of an integer array. Your function should take two parameters – an integer array to be searched, and an integer specifying the start position to begin searching. The first time you call the method, you should always begin by searching from position 0 – i.e., the beginning of the array. For convenience, you can add an overloaded helper method to start the search from position zero as in the following outline: public static int findMin(int[] array) { return findMin(array, 0); } public static int findMin(int[] array, int startPos) { // your code here. } As with all recursive methods, you should think carefully about what is the base case and what is the recursive case. Hint: the minimum value in an array of length one is always easy to find! 2. Write a recursive method to reverse a string. As you know, Strings in Java are immutable types, so you will actual write a method to return a new String which is equal to the reverse of the input. You will need the substring(int begin) and charAt(int index) methods in order to take apart the pieces of the input string. 3. Challenge problem. A “numerical palindrome” is a number whose decimal digits are the same when read left-to-right or right-to-left. Write a recursive method that checks whether a given number is a numerical palindrome. DO NOT convert the number to a string. Your main method should prompt the user for a number, test it, and print the result. Hint: Getting the last digit of a number is easy. How would you get the first digit? Do it using some math and some code, not by having Java convert the number to a string.

Congratulations, you now have experience writing recursive programs! You know the drill – when you’re finished, polish and shine your code until it’s clear and then demonstrate your solution to your TA.

2...


Similar Free PDFs