Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion Formatting Decimal Numbers CSC 1401: Introduction to Programming with Java Week 9 – Lectures 1 & 2 Wanda M. Kunkle.

Similar presentations


Presentation on theme: "Recursion Formatting Decimal Numbers CSC 1401: Introduction to Programming with Java Week 9 – Lectures 1 & 2 Wanda M. Kunkle."— Presentation transcript:

1 Recursion Formatting Decimal Numbers CSC 1401: Introduction to Programming with Java Week 9 – Lectures 1 & 2 Wanda M. Kunkle

2 2 Recursion A recursive method is a method that calls itself. A recursive method is a method that calls itself. A recursive method is called to solve a problem that it divides into two (usually) smaller problems: A recursive method is called to solve a problem that it divides into two (usually) smaller problems: –A problem that it knows how to solve, the simplest, or base case –A problem that it doesn't know how to solve that is similar to but simpler than the original problem, the recursive case

3 3 Recursion A recursive method generally has two parts: A recursive method generally has two parts: –A termination part that stops the recursion, in which the method solves the simplest case and returns the result –A recursive call in which the method calls itself to go to work solving the simpler version of the original problem

4 4 Recursion The recursive method terminates when it is finally called to solve the base case, at which point all previous return values can be computed and the final result returned to the calling method. The recursive method terminates when it is finally called to solve the base case, at which point all previous return values can be computed and the final result returned to the calling method.

5 5 Example of Recursion An implementation of the factorial function can be used to illustrate recursion. An implementation of the factorial function can be used to illustrate recursion. Factorial can be formally defined as follows: Factorial can be formally defined as follows: For example, 5! can be rewritten as 5 * 4!, which can be rewritten as 5 * (4 * 3!), which can be rewritten as 5 * (4 * (3 * 2!)), and so on. For example, 5! can be rewritten as 5 * 4!, which can be rewritten as 5 * (4 * 3!), which can be rewritten as 5 * (4 * (3 * 2!)), and so on.

6 6 Sample Programs Let’s look at two different sample programs that implement the factorial function: Let’s look at two different sample programs that implement the factorial function: –iterativeFactorial.java iterativeFactorial.java –recursiveFactorial.java recursiveFactorial.java

7 7 Another Example of Recursion An implementation of the Fibonacci function can be used to further illustrate recursion. An implementation of the Fibonacci function can be used to further illustrate recursion. Would anyone care to define the Fibonacci numbers … Would anyone care to define the Fibonacci numbers … –Either formally or by example? –i.e., It’s your turn!

8 8 Another Example of Recursion The Fibonacci numbers are a sequence of numbers defined by the following recurrence relation*: The Fibonacci numbers are a sequence of numbers defined by the following recurrence relation*: (s) According to the Wikipedia, “a recurrence relation is an equation that defines a sequence recursively: each term of the sequence is defined as a function of the preceding terms.”Wikipediarecurrence relation

9 9 Another Example of Recursion In other words, each number after the two starting values is obtained by adding the two numbers that precede it. In other words, each number after the two starting values is obtained by adding the two numbers that precede it. The first few terms in the Fibonacci sequence are: The first few terms in the Fibonacci sequence are: –0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, … –Where F 0 = 0 and F 8 = 21

10 10 The Fibonacci Numbers in Nature The Fibonacci numbers occur everywhere in nature. Visit the following links to learn more about this phenomenon: The Fibonacci numbers occur everywhere in nature. Visit the following links to learn more about this phenomenon: –http://www.world-mysteries.com/sci_17.htm http://www.world-mysteries.com/sci_17.htm –http://www.mcs.surrey.ac.uk/Personal/R.Knott/F ibonacci/fibnat.html http://www.mcs.surrey.ac.uk/Personal/R.Knott/F ibonacci/fibnat.htmlhttp://www.mcs.surrey.ac.uk/Personal/R.Knott/F ibonacci/fibnat.html –http://en.wikipedia.org/wiki/Fibonacci_number# Fibonacci_numbers_in_nature http://en.wikipedia.org/wiki/Fibonacci_number# Fibonacci_numbers_in_naturehttp://en.wikipedia.org/wiki/Fibonacci_number# Fibonacci_numbers_in_nature

11 11 Sample Programs Let’s look at two different sample programs that implement the Fibonacci function: Let’s look at two different sample programs that implement the Fibonacci function: –iterativeFibonacci.java iterativeFibonacci.java –recursiveFibonacci.java recursiveFibonacci.java

12 12 Formatting Decimal Numbers Decimal numbers can be formatted using the printf method. Decimal numbers can be formatted using the printf method. Java 5.0 borrowed this method from the C programming language. Java 5.0 borrowed this method from the C programming language. A call to printf requires that it be provided with: A call to printf requires that it be provided with: –A format string –Optional arguments

13 13 Formatting Decimal Numbers Values passed to printf : Values passed to printf : –Format string Describes the format of the output Describes the format of the output May consist of: May consist of: –Fixed text –Format specifiers A format specifier is a placeholder for a value of a specific type A format specifier is a placeholder for a value of a specific type A format specifier may be used to specify precision (number of decimal places to be displayed), field width, justification, etc. A format specifier may be used to specify precision (number of decimal places to be displayed), field width, justification, etc. An example of a format specifier is %f, a placeholder for a float or double value An example of a format specifier is %f, a placeholder for a float or double value

14 14 Formatting Decimal Numbers Values passed to printf : Values passed to printf : –Optional arguments Arguments referenced by the format specifiers in the format string Arguments referenced by the format specifiers in the format string Example: System.out.printf("\n\nA circle of radius %f has a circumference of %f", radius, circumference); radius is referenced by the first %f, whereas circumference is referenced by the second %f Example: System.out.printf("\n\nA circle of radius %f has a circumference of %f", radius, circumference); radius is referenced by the first %f, whereas circumference is referenced by the second %f

15 15 Sample Programs Let’s look at two different sample programs that demonstrate formatting decimal numbers: Let’s look at two different sample programs that demonstrate formatting decimal numbers: –circumferenceAndArea.java circumferenceAndArea.java –volumeAndSurfaceAreaDemo.java volumeAndSurfaceAreaDemo.java


Download ppt "Recursion Formatting Decimal Numbers CSC 1401: Introduction to Programming with Java Week 9 – Lectures 1 & 2 Wanda M. Kunkle."

Similar presentations


Ads by Google