CS 275 - Programming Assignment 8

This assignment is based on Section 3.5 (pages 134-148) of our textbook. In this assignment you will write a number of short, recursive methods. Create a file Recursion.java that contains static methods that define solutions to the following problems. Your Recursion.java program should contain these methods in the order that they are described below (so that I can find them easily in your code). Your methods should have the names and signatures given below (so that I can test them).

Write a method int max(int[] a) that returns the maximum value from the input array. This method should call a recursive helper method int maxRecursive(int[] a, int i, int j) that solves the same problem for the sub array starting at index i and going to index j.

Write a method int countOccurrences(int[] a, int b) that returns the number of times that the integer b occurs in the array a. This method should call a recursive helper method
int countOccurrencesRecursive(int[] a, int b, int i, int j) that solves the same problem for the sub array starting at index i and going to index j.

Write a method int hasDouble(int[] a) that returns the first index i for which a[i]==a[i+1] is true and returns -1 if no such index exits. This method should call a recursive helper method
int hasDoubleRecursive(int[] a, int i, int j) that solves the same problem for the sub array starting at index i and going to index j.

Write a method boolean moreEvens(int[] a) that returns true if the input array contains more even integers than odd integers or the same number of even and odd integers. This method should call a recursive helper method int moreEvensRecursive(int[] a, int i, int j) that returns, for the sub array starting at index i and going to index j, how many more even than odd integers there are in the sub array (so this method returns a positive number if the sub array has more evens than odds, and it returns a negative number if there are more odds than evens).

Write a recursive method int string2int(String s) that converts a string of digits, s, into the integer it represents in base 10 (you do not need to worry about the string containing non digit characters).

Write another program TestRecursion.java that tests your methods. Be sure to come up with enough test cases to be pretty sure that you have correct solutions. Test your methods in the same order in which they are described above.

Turn in a zip file called CS275Hw8Surname.zip containing your Java source file, Recursion.java, your test program TestRecursion.java, and also this file, hw8.html. This assignment is due Monday, November 26.


Return to the CS 275 home page.


compliments and criticisms