CoSc 200 Laboratory #11
Sums, Reversals, and Triangles
Objective: To gain experience with recursion.
Download the starter files from this folder or from this zip file.
In order to reverse a string, we can put the first character at the end, put the second character next to the end, put the third character third from the end, and so forth. That is an iterative approach. We can also place the first character at the end of the reversal of the rest of the string. That is a recursive approach.
Your task is to turn the above ideas into code and provide suitable tests for your methods. The file SumAndReverse.java provides a start.
Fortunately, Java's drawing resolution is good enough that if we get it to do this kind of doodling, we can produce things quite a bit more interesting than a big blob. For example, repeatedly dividing triangles into smaller triangles can lead to a picture like that shown above.
The boundary of a triangle doodle is determined by three points (1, 2, and 3 in the figure below). These three points determine three midpoints (A, B, and C in the figure). A triangle doodle (usually called Sierpinski's Gasket) consists of three smaller triangle doodles, each of whose boundaries are determined by one of the original points and the two nearest midpoints (the three smaller doodles in the figure are formed by 1, B, C; 2, A, C; and 3, B, C. Once a triangle doodle is small enough, it consists of the three triangles formed by an original point and the two nearest midpoints (see the green triangle doodle in the figure).
By this definition, the "central" triangle formed by sides from the three "outer" triangles is actually outside of the triangle doodle! Only the area inside of the three "outer" triangles is considered to be inside of the triangle doodle. (In particular, the interior of the triangle formed by A, B, and C is considered to be outside of the triangle doodle). This discussion is important for determining whether a triangle doodle contains the point at which a mouse is pressed.
This demo creates triangle doodles based upon three points provided by the user and allows a user to move a triangle doodle by pressing inside the triangle doodle and dragging the mouse.
In order to replicate the demo version, you will need to use or implement the following classes and interface:
| Value | Feature |
| Syntax Style (1 pt total) | |
| 1 |
Descriptive comments |
| Correctness (19 pts total) | |
| 1 |
Iterative sum |
| 1 |
Recursive sum |
| 1 |
Iterative reverse |
| 1 |
Recursive reverse |
3 |
Tests for the sum and reverse methods |
| 3 |
Triangle doodle creation |
| 3 |
Triangle doodle movement when press is inside |
| 3 |
Triangle doodle non-movement when press is outside |
| 3 |
Triangle doodle removal from canvas |
As with previous lab assignments, you should submit your".java" files through Blackboard. You can either add these files separately before you submit, or you can first zip together these files into a single ".zip" file and submit this single ".zip" file.
Submission Deadline: 3:00pm on Tuesday, April 11. Note that once you submit, it is impossible to submit a revision.
Resubmission and Late Submission Deadline: 9:00am on Tuesday, April 18. A 5 point penalty will be assessed for a resubmission or late submission. These should be submitted to Lab03Late.
Ecstasy is happiness added to ecstacy!