This project has two parts: The first part gives you practice writing recursive methods. The second part is to practice writing methods for lists. These two parts are unrelated; I've put them together into one project for scheduling purposes.
Make a directory for this assignment, and change into it.
cd cs235 mkdir proj6 cd proj6
Copy the following file from the public directory for this class:
cp /homes/tvandrun/Public/cs235/proj6/* .
Open the file RecursiveString.java
.
This file contains the stubs for three methods for you to write,
as well as a simple main method to test these methods out
using information from the args
array.
In each section below, you will be instructed to fill-in the body for
one of these methods; you also should uncomment the code in the main
method which tests it.
We've already seen at least one algorithm for determining whether or not a String is a palindrome. Now you need to figure out a recursive method to do it. Figure the algorithm on your own, but here's a recursive definition of a palindrome to get you started:
A palindrome is
(Remember, "prepend" means "concatenate to the front" and "append" means "concatenate to the back.")
Write the isPalindrome()
method and test.
You need concern yourself only with single words, not phrases (so you
can assume that the string has only letters, not spaces, punctuation,
etc).
Notice that the main method tests your methods on strings from the command line
using the args
array.
Therefore, to test this, you should give a string to test on the command line.
For example
java RecursiveString racecar
Write a method which will reverse a String. First write out a recursive definition of what the reverse of String is; then transform that definition into a recursive algorithm for computing the reverse of a String.
Write a method startsWith()
which takes two strings and determines whether or not one
is a prefix of the other.
The goal of this lab is to implement basic sorting algorithms.
We saw the general problem of sorting back in Project 3. Some sorting algorithms are recursive. The most intuitive recursive sort if mergesort. It works this way
Suppose you have an unsorted pile of cards in your hand. First you split that pile up into two smaller piles. Then you sort each pile. Finally, you merge the two smaller (now sorted) piles into a full, sorted pile. This raises the question, how do we sort the smaller piles? The answers is-- the same way we sort the big one: we split those up each into two even smaller piles, sort those smaller piles, and merge them into a sorted small pile. We repeat the process (recursively) until we get to a pile of size 1, which is trivially sorted.
Like other sorting algorithms, mergesort is usually described in terms of how it is implemented on an array. We, however, will be implementing it on a linked list.
You have the following files given to you in this part of the project:
Node.java
. This is the node class exactly
as we have used it in the non-recursive versions in class.
You will not need to change this class.
List.java
. This is a linked list class
similar to what we've seen in class and lab.
You will need to complete two methods in it.
SortList.java
. This is a driver program, similar
to the Sort
program from Project 3.
To run it on a random list of integers, run java SortList
ListMerge
.
ListMerge.java
. This contains the static
sort()
method which executes mergesort.
You will need to complete a helper method in this class.
As in the lab (on 11/4), the driver and a big part of the list class is
already provided for you.
Also, the basic merge sort algorithm is given for you in
the method sort()
in class ListMerge
.
Your task is to write the following methods that sort()
needs:
half()
, a method in the List
class.
This method does two things: It remove the last half of nodes from the list
on which it is called and it returns those removed nodes as their
own list.
For example, if it were called on the list
List
object)
addToBack()
, a method in the List
class.
This takes an int
, makes a new node for it, and hooks the
new node into the back of the list.
merge()
, a static method in ListMerge
.
Notice that this is not called on a list, and since it is not part of the
List
class, it does not have access to the private members
(i.e., head
) of any list.
Consequently, merge()
will almost certainly need
removeHead()
and addToBack()
.
You can implement these exactly as you did for lab 12.
It is possible that you will write merge
in such a way
that you wish List
to have some extra operations;
feel free to add more operations to List
as you see fit.
Do the normal turn-in procedure: Script file, cat the files you changed, compile, run a few times.
a2ps (the name of the script file)
Then turn in the hard copy by 5:00, Thurs, Nov 13.