Mastering Recursion in Java: Tools, Techniques, and Problem-Solving Strategies
This course dives into the fundamental concepts of recursion and its applications in Java programming. Learn about essential data structures like lists and maps, techniques for Big O analysis, and the intricacies of classes and inheritance. You’ll explore the process of breaking down complex problems into simpler components, significantly enhancing your coding skills. Engage with practical exercises, classwork, and quizzes on Sakai. By the end of this course, you will have a strong grasp of recursive functions and their role in solving problems efficiently.
Mastering Recursion in Java: Tools, Techniques, and Problem-Solving Strategies
E N D
Presentation Transcript
Recursion Snarf the code for today’s class.
Where We Are: Tools and Techniques • Data Structures, like Lists and Maps • Techniques, like Big O analysis • Classes and Inheritance • The essential process of taking a problem and solving it with code All of it happens to be within Java in this course…but these ideas transcend Java
Recursion • When a function calls a “clone” of itself • Also, when a Data structure contains a “clone” of itself (but we will save this one for later)
What to do • Snarf the code for today’s class if you haven’t already • Go to the quiz section of Sakai • There’s classwork for today there • Go through and try to answer the questions (feel free to take you time, think, and ask a friend) • If you get stuck, run the code and see what’s going on • If you finish it all, start on the BONUS section
Two Parts To Every Recursive Function • Base Case if(n == 1) return 1; • Recursive Case return n*recursiveFunction(n-1); The basic idea is to get help solving a problem from coworkers (clones) who work and act like you do. • Ask clone to solve a similar, but smaller/simpler problem • Use the result • Don’t forget step 2!
How you divide the problem is key Say you want to count the number of “a”sin a string. How could you solve that problem recursively? Hint: how many “a”s does a string of length 0 have?
How you divide the problem is key isPalindrome(String s) abaXaba -> true abba -> true abab -> false