The goal of this lab is to practice writing and working with recursive methods.
As in previous assignments, move into your cs235
directory.
Make a directory for this assignment, and change into it.
cd cs235 mkdir lab6 cd lab6
Copy the following file from the public directory for this class:
cp /homes/tvandrun/Public/cs235/lab6/RecursiveString.java .
This file contains the stubs for six 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 will do the following:
main()
method.
In these problems, you will never need to declare a local variable. You will also never write a loop. Some problems have specific restrictions; pay careful attention to those.
You've already programmed 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.
In your method, you may use charAt()
, length()
,
and substring()
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 recursive version of the startsWith()
method you wrote for the quiz.
Write a recursive method which will determine if one String is a substring of another.
Write a recursive method that will compute a String's
length.
You may use only substring()
and equals()
.
Write a recursive method that will compute whether or not
two Strings are equal to each other in their contents.
You may use only length()
, charAt()
,
and substring()
.
Create the script file as before. Run the program using a variety of words. Print it out and hand in a hard copy.
a2ps (the name of the script file)
One advantage to your being able to write methods is that now you can write pieces of code that can be plugged into larger programs. In this exercise you will write a method that defines some behavior within a program.
Copy the following files.
cp /homes/tvandrun/Public/cs235/lab6_ec/* .
You'll notice that this includes many class
files, things
that are already compiled for you.
If you run the program KnightGame
, it will
open a window containing a chess-like board of a given size.
For example,
java KnightGame 8 &
Will produce a window containing a normal 8x8 chessboard.
The problem given to you is, given a position on a chessboard and a number of moves, what positions can a knight reach from the given position within the given number of moves.
Open the file KnightSearch.java
.
It contains the stub of method performing this
search, which you must complete.
The program is set up in such a way that when the user clicks the
"go" button, your method will be invoked, and the results will appear
on the method.
Your method will be given a value of type ChessBoard
.
This type has the following operations (assume a variable of this type
called board
, just like the parameter to search()
:
board.mark(2, 4, 1)
this will "mark" board position (2, 4)
with the number 1, indicating that the knight can get to that position
with one move remaining.
board.isMarked(2, 4)
this will return true or false,
depending on whether this position has been marked
board.getMark(2, 4)
this will return the (int) mark
on that position (the number of moves left after arriving at
that position on the best route found so far).
board.length()
this will return the (int) length of one dimension of
the board.
The obvious strategy for solving this is
search()
recursively on all of those, with one fewer move
available
The details are a little difficult. See what you can do.