Department of Computer
Science
CMSC132:
Fall 2009
Take-home Quiz: Recursion
Due Date: Wednesday October 14, 6:00 pm
Assignment Type: Closed (See
Policy)
For this homework you will implement several static methods that will perform simple tasks on generic lists. We will provide you with four public tests (one for each of the routine problems)
This project is designed to help you practice implementing recursive algorithms. The solutions to these problems should be quite short.
The homework is worth 100 points.
Any clarifications or corrections associated with this project will be available at Clarifications.
The project's code distribution is available by checking out the project named RecursionExercises. The code distribution provides you with the following:
When you look in the code distribution you'll see method prototypes that look like this:
public static <T> void duplicateFirst(List<T> list)
Between the keyword "static" and the return type "void" you see <T>. This is how static methods can be parameterized for use with generic types. "T" represents a type that will be determined later when the method is invoked.
To successfully implement your methods, you will find it quite helpful to be able to generate a sublist from a List. Java makes this very easy! The List interface includes a method with the following prototype:
Important: Note that the parameter "toIndex" is not inclusive! For example, suppose x is the list: <a, b, c, d, e>. Then x.subList(2, 4) would give you the sublist: <c, d>,not the sublist <c, d, e> as you might have expected.(This may remind you of the subString method of the String class.)
Methods for you to Implement
You will implement the following methods:
This method will replace all of the elements in the list with copies of the first element of the list. For example, if the original list is <a, b, c, d, e>, then after the method is over the list will look like: <a, a, a, a, a>.
This method will replace all of the elements in the list with copies of the last element of the list. For example, if the original list is <a, b, c, d, e>, then after the method is over the list will look like: <e, e, e, e, e>.
This method will return false if all of the elements of the list are distinct, and true if there are any duplicates.
This method will return the largest element of the list. (Note that the elements in the list must be Comparable to one another).