Practicing Recursion with JavaFX

Educators and students consider recursion one of the most difficult topics in introductory programming courses. The limited number of recursive examples in the regular textbooks is not sufficient for most students. This text is designed to help students in CS I or CS II courses to practice writing recursive code in JavaFX. It may be helpful for instructors teaching those courses, as well. For them it can serve as a poll of test examples, or as a base to create projects that include variations of existing methods. The book provides collection of over 150 recursive examples on integers, arrays, linked lists, linked trees, and graphical examples. Each example includes a problem specified in English, solution at the algorithm level, and recursive code implemented in JavaFX. Graphical examples have also the picture of the outcome drawn by application in addition to code. Ninety percent of examples were given to students on tests and final exams. The remaining ten percent of the problems are for advanced students who grasped the topic and want to be challenged to do more. We start to practice recursion by writing code for simple examples which can also be done iteratively in slightly more efficient way. Recursion requires several calls before reaching the base case. Each invocation requires putting the method name and parameters on the activation stack, and upon completion, removing it from the stack when the results are passed back. This approach is chosen to help learners to adjust to new way of solving problems. Writing code recursively requires a paradigm shift from being used to specifying the steps explicitly, in the order how they are performed, to specifying them implicitly. Recursive problems have base case part where we specify code to provide the desired result without calling any sub-problems, and in addition there is recursive step part where we specify how to obtain the result by invoking one or more sub-problems of the same type as the original problem, plus we perform some additional code to combine the sub-problems and produce the result. Recursion is so difficult to learn because each problem has its own way of formulating base case and recursive step. Sometimes there is one base case, sometimes there are ten base cases, sometimes we do nothing in base case and just return from the method. Recursive steps range from one to many, they vary how we express sub-problems, and each time the way to combine the solutions for sub-problems in order to produce result for original problem, is different as well. When learners start by learning to write recursive code for simple examples, their only challenge is to figure out how to formulate the solution for the given problem recursively. Examples are ordered from simpler to more difficult ones. Many problems are analogous to those done before. This spiral approach allows learners to master recursion at their individual pace. Some should do most of the examples, others can skip similar problems and spend more time on harder problems. Chapter devoted to recursive graphical examples is done in JavaFX which is the latest version of Java. Graphics provides additional advantage because drawing shapes on graphical surface provides visualization of steps performed by recursion. Learning recursion on integers, arrays, linked lists, and graphics, facilitates a smooth transition to writing code recursively on more advanced data structures such as trees and graphs.

Author: Irena Pevac

Learn more