We have seen in lecture and readings how recursion can be useful. When there is a big problem that needs to be solved, splitting it up into smaller versions of that same problem until it is at it's simplest case can be a lot quicker and more efficient.
In the first part of this assignment, you will be determining
whether or not a string of characters is a palindrome. A palindrome
is a word, phrase,
number or other sequence of units that can be read the same way in
either direction. Here are some
examples of interesting palindromes. For the purpose of this
assignment, you will not be required to take into account white space
or punctuation, so if you have a phrase that is a palindrome you
should test it without white space or punctuation. For example, if you
wanted to use "A man, a plan, a canal, Panama" you should send your
program "amanaplanacanalpanama". If you want to take on the challenge
and modify your code to accept white space and punctuation, there is
extra credit available if you do it correctly.
You will then write the same function, but this time using
loops. Both code should perform exactly the same. This is so that you
can see for yourself how a problem solvable with a recursive solution can
also be solved with an iterative solution.
The last part of the assignment will be a little more appealing to the eye and entertaining. It will also involve recursion, but you will be creating fractals. The graphics have already been written for you so that you can play with your code and see direct results, but you will have to write the recursion code that actually draws the fractals.
This homework requires skills with recursion, 2-D arrays, and loops.
You must put a comment at the top of your code that specifies your:
real name, login name, email address, and PID. You must also include a
two-to-four sentence description of the program. Use a multi-line
comment (/* */). YOU MUST DO THIS FOR EVERY JAVA
FILE!
Comments Comments in the style detailed in the book ARE required. Your code must be properly commented to receive full credit.
Comments and good style are worth (2pt).USAGE: java Palindromes (String) (Loop|Recursion)If there are more than 2 command-line parameters given, the extras will be ignored and the program continues like normal. Depending on what choice the user makes (looping or recursion), this method should call the appropriate method.
The output should look like the following:
> java Palindromes "racecar" RecursionNOTE: It should not matter whether the characters are capitalized. For example, Racecar is a palindrome. Keep in mind though, R != r, so you might have to modify the input. (2pts)
> This is a palindrome!
> java Palindromes "racecar" Loop
> This is a palindrome!
> java Palindromes "monkey" Recursion
> This is not a palindrome!
> java Palindromes "monkey" Loop
> This is not a palindrome!
The technique you should use in this method is:
recursivePalindromes("racecar")
if(r==r)
recursivePalindromes("aceca")
else
false
etc
java Dictionary input.txt
Hint: Look at your method header for main -- the first element in that array is the filename to open.
String [][] dict = new String [1][1]; ... (later) dict = new String [something][somethingElse];
What word would you like me to look up?Until they enter the word DONE in all capital letters
That word is not in my dictionary!, Try again?
The definitions of cloud are a visible collection of particles of water overshadow (in Web 2.0) a weighted list of words often with a graphical representation representing weight
The figures below show what your DragonFractal class should draw for recursion depths 0-3.
| Depth 0: | Depth 1: |
|
|
| Depth 2: | Depth 3: |
|
|
The figures below show what your ColoredDragonFractal class (EXTRA CREDIT) should draw for recursion depths 9-12. (We are starting with depth 9 because when you draw the ColoredDragonFractal with a depth of 8 or less, only one color is used.)
Each colored section is a dragon fractal with depth 8. Note how the different colored sections fit together like tiles, and also note how the number of colored sections increases as the recursion depth increases. Can you see a pattern?
| Depth 9: | Depth 10: |
|
|
| Depth 11: | Depth 12: |
|
|
The Animator class has two methods:
sleep(int milliseconds) -- Pauses for the amount
specified. Used so that the process of drawing lines can be seen.
drawLine(Point p1, Point p2) -- Draws a solid
line from point p1 to point p2.
NOTE: You do not need to write the Animator class, but you will need the source code for it in order to compile your fractals. The source code for Animator.java can be found here.
The Point
class has 3 important parts that will be used in this
assignment:
Class Field - x -- This is the x
coordinate where the point is located in the pane.
Class Field - y -- This is the y coordinate where the
point is located in the pane.
Point(int x, int y)
(constructor) -- This is the constructor that creates a new point at
the x and y coordinates specified.
First, calculate the x-value and y-value of a new point called newP.
Then make two recursive calls to draw, passing in the decremented value of depth and the following parameters:
First, calculate the x-value and y-value of a new point called newP.
Then make two recursive calls to draw, passing in the decremented value of depth, the alternate value of direction, and the following parameters:
See the above images for a reference to what should be drawn.
More information about the DragonFractal is here.
If you want to design your own Colored Dragon Fractal that changes colors in an interesting way, we encourage you to do so. Or you can use the provided instructions to draw the example Colored Dragon Fractal.
If you design your own Colored Dragon Fractal, please include a brief description of how it works and why it is interesting in your comments at the top of the ColoredDragonFractal class (1/2pt for description). The required fields and methods include animator, the ColoredDragonFractal constructor (1/2pt for animator and constructor), and the draw method (see specifications in instructions for option 2 below) (1pt for draw). You can create any other fields and methods that you want to implement the coloring (1pt for coloring). (3pts)
You should initialize this array to contain 8 colors of your choosing. The colors you choose should be easy to tell apart at a glance.
Use the static fields in Color
Example: Color.yellow
-OR-
Create colors using one of the Color constructors
Example: Color(int r, int g, int b)
When the recursion depth in draw is a multiple of 8, change the color used to draw lines. (Note that when you draw the fractal with a depth of 8 or less, the color won't change.) Call your nextColor method to choose what color to use next.
We will have a testing program for both Part A and Part B of this assignment.
The testing code for Part A can be copied from here. But please be sure to try any other
test cases that you can think of.
The tester for Part 2 will allow you to write your fractals one at a
time, and you can test them individully without having to write them
all at once. You can run the testing program for Part B by typing the
following commands on ieng6:
testPSA4B {none|DragonFractal.class} {none|ColoredDragonFractal.class}We've provided some example fractals, the Sierpinski Triangle Fractal and the Koch Snowflake Fractal, in the tester. You can try out these examples to see how the testing program behaves. So for example, if you have not completed any of your fractal classes yet, you can run the tester to view the examples we've provided:
testPSA4B none noneHere's another example. If you have only completed your DragonFractal and not the extra credit ColoredDragonFractal, then you could test it by typing:
testPSA4B DragonFractal.class noneThe tester will load a GUI application that will allow you to run your fractal at different iteration depths. You should begin testing your fractal with small iteration depths (i.e. 0, 1, 2) and then once those are successful, you can work your way up to larger ones. The larger the iteration depth you give, the longer it will take for your fractal to run. This time will grow exponentially as you increase the iteration depth. For the DragonFractal and ColoredDragonFractal, iteration depths beyond 10 will take a long time.
On ieng6 somewhere under your home directory you need to make a folder called PSA4. All of your files for this assignment must go in this folder.
Here's some help with the geometry of the Dragon fractal. We will walk though a "right turn". Consider the
diagram below:
The inputs to this call to the Dragon fractal would be the points
p1 and p2.
The first step is to compute the mid point between p1 and p2, call it M.
The next useful values to have are the difference in the x-values of M and p2 (call it dx), and the difference in the y-values between M and p2 (call it dy). The values of dx and dy are drawn on the diagram in blue.
The last point we need to calculate is T, which we can find using the values of M, dx, and dy. Notice from the diagram that the difference in x-values between T and M is equal to dy. Also, the difference in y-values between T and M is dx.
Now that we have these points, we can make the recursive calls. The recursive calls you should make are:
(HINT: something you should keep in mind is, will dx be positive or negative? What about dy? Does it matter? Will your code work for every case? Hmm...)