Download presentation
Presentation is loading. Please wait.
1
Review of Previous Lesson
Thursday, 16/08/2018 Review of Previous Lesson State as many Vocabulary words and Learning Objectives that you remember from the last lesson as you can. Remember to grade yourself from
2
Object Orientated Programming Paradigm (OOP)
Thursday, 16/08/2018 Object Orientated Programming Paradigm (OOP) Recursion
3
Program Implementation
Thursday, 16/08/2018 Program Implementation Part of the problem-solving process is the statement of solutions in a precise form that invites review and analysis. The implementation of solutions in the Java programming language reinforces concepts, allows potential solutions to be tested, and encourages discussion of solutions and alternatives. Control Recursion
4
Thursday, 16/08/2018 Recursion When a method calls itself.
5
Thursday, 16/08/2018 Loop Recursion int i = 0; while(i < 6){ System.out.println(i); i++; } public static void recurse(int i){ System.out.println(i); recurse(i+1); } Would go on forever like an infinite loop. We need to add in a stopping condition (in recursion this is known as the base case) to tell the code to stop – see next slide. ? Output: Output: 1 2 3 4 5 1 2 3 4 5 … ?
6
? Loop Recursion Thursday, 16/08/2018
int i = 0; while(i < 6){ System.out.println(i); i++; } public static void recurse(int i){ if(i==6) { //do nothing } else { System.out.println(i); recurse(i+1); Stopping Condition Base Case Stop Note that there must be a base case otherwise infinite recursion occurs. ? Output: Output: 1 2 3 4 5 1 2 3 4 5
7
Thursday, 16/08/2018 What do you think would happen if we swap the call & println? Recursion public static void recurse(int i){ if(i==6) { //do nothing } else { recurse(i+1); System.out.println(i); Stop ? Output: 5 4 3 2 1
8
How we recursively calculate the number of digits in an integer?
Thursday, 16/08/2018 How we recursively calculate the number of digits in an integer? To code this recursively we will have to start at the number (e.g. 4326) and recursively divide by 10 and call until < 10 (the final digit), so that the recursion will unwind back from the last digit. Base Case is when num < 10 and return 1. Otherwise return 1 + the previous call’s return. ? ?
9
How we calculate the number of digits in an integer?
Thursday, 16/08/2018 public class NoDigitsInAnIntegerExample { public static void main(String args[]){ int num=4326; System.out.println("No of Digits in "+ num + " is: " + findNoDigitsInAnInteger(num)); } // return no of digits in num. public static int findNoDigitsInAnInteger(int num){ if(num < 10) return 1; e.g. 4326 result = 4 return 4 num = 4326 1 + 3 return 3 num = 432 1 + 2 return 2 num = 43 1 + 1 return 1 + findNoDigitsInAnInteger(num / 10); return 1 num = 4
10
How we calculate the number of digits in an integer?
Thursday, 16/08/2018 public class NoDigitsInAnIntegerExample { public static void main(String args[]){ int num=4326; System.out.println ("No of Digits in "+ num + " is: " + findNoDigitsInAnInteger(num)); } // return no of digits in num. public static int findNoDigitsInAnInteger (int num){ if(num < 10) return 1; Repeat of previous slide but with automatic animation. e.g. 4326 result = 4 return 4 num = 4326 1 + 3 return 3 num = 432 1 + 2 return 2 num = 43 1 + 1 return 1 + findNoDigitsInAnInteger(num / 10); return 1 num = 4
11
How we calculate a factorial iteratively?
Thursday, 16/08/2018 How we calculate a factorial iteratively? e.g. 4 4 * 3 * 2 * 1 = 24 Or 1 * 2 * 3 * 4 = 24 ?
12
Thursday, 16/08/2018 1 * 2 * 3 * 4 * …. ? To code this recursively we will have to start at the number (e.g. 4) and recursively subtract 1 and call until 1, so that the recursion will unwind from 1 to the number. Base Case is when num reaches 1 and return 1. Otherwise return num (e.g. 2, 3, 4) * by the previous call’s return. Note: Unwinding from 1 is always the base case, no matter what the number we require the factorial is, this is not the case if we reversed this to unwind from the number required backwards (e.g. 4 * 3 * 2 * 1) as the base case depends on the number in this case. Therefore, recursive coding of factorial can only unwind from 1. ?
13
1 * 2 * 3 * 4 * …. Thursday, 16/08/2018 public class FactorialRecursionExample { public static void main(String args[]){ int num=4; System.out.println("Factorial of "+ num + " is: " + findFactorial(num)); } // return factorial of num. public static int findFactorial(int num){ if(num == 1) return 1; return num * findFactorial(num-1); e.g. Factorial 4 result = 24 return 24 num = 4 4 * 6 return 6 num = 3 3 * 2 return 2 num = 2 2 * 1 return num * findFactorial(num-1); return 1 num = 1
14
1 * 2 * 3 * 4 * …. Thursday, 16/08/2018 public class FactorialRecursionExample { public static void main(String args[]){ int num=4; System.out.println("Factorial of "+ num + " is: " + findFactorial(num)); } // return factorial of num. public static int findFactorial(int num){ if(num == 1) return 1; return num * findFactorial(num-1); Repeat of previous slide but with automatic animation. e.g. Factorial 4 result = 24 return 24 num = 4 4 * 6 return 6 num = 3 3 * 2 return 2 num = 2 2 * 1 return num * findFactorial(num-1); return 1 num = 1
15
13/04/2019 Write your own programs: Write your own programs from “scratch”. Of course you should use previous programs for reference, but write your code from “scratch” (do not copy and paste).
16
Triangle Number (Rows)
13/04/2019 Triangle Numbers Rows Triangle Number (Rows) 1 2 3 6 4 10 5 15 21 7 28 ? ? ? ? ? ? ? Write a recursive method which accepts a row number and returns the triangle number (main() in a separate class).
17
Rewrite programs which use loops so that they use recursion
13/04/2019 Rewrite programs which use loops so that they use recursion Obvious examples would be the independent programs in the “Loops” section (the “for”, “nested for” and “while” presentations).
18
4/13/2019 Grade yourself Grade yourself on the vocabulary and learning objectives of the presentation.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.