MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )

Slides:



Advertisements
Similar presentations
C++ Programming:. Program Design Including
Advertisements

Recursion Recursive Thinking Recursive Programming
Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion. Binary search example postponed to end of lecture.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Programming Recursion “To Iterate is Human, to Recurse, Divine” -- L. Peter Deutsch.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion.
Exercise 4 Recursion. Functions – a short reminder a group of variables and statements that is assigned a name a sub-program  inside main we can call.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Unit 7 1 Unit 7: Recursion H Recursion is a fundamental programming technique that can provide an elegant solution to a certain kinds of problems H In.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Recursion CITS1001.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Recursion James Wright St Peter’s Secondary School 733 Parkhill Road West Peterborough,Ontario K9J-8M4
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
M180: Data Structures & Algorithms in Java
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
October 3, 2001CSE 373, Autumn Mathematical Background Exponents X A X B = X A+B X A / X B = X A-B (X A ) B = X AB X N +X N = 2X N 2 N +2 N = 2 N+1.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion Reading L&C 3 rd : 7.1 – nd :
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Classes - Intermediate
Methods.
R ECURRSION Prepared by Miss Simab Shahid Lecturer computer Science and Software Engineering department, University of Hail Chapter.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Understanding Recursion
CS314 – Section 5 Recitation 9
Sections 4.1 & 4.2 Recursive Definitions,
Chapter Topics Chapter 16 discusses the following main topics:
Topic 6 Recursion.
Recursion DRILL: Please take out your notes on Recursion
Java Software Structures: John Lewis & Joseph Chase
Recursion Recursive Thinking Recursive Programming
Recursive Definitions
Recursion This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the instructor’s class materials.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Java Programming: Chapter 9: Recursion Second Edition
CS148 Introduction to Programming II
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion Method calling itself (circular definition)
Recursion.
Recursive Thinking.
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Presentation transcript:

MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )

Methods The name of a method together with the types of its parameters are called the signature of the method. For example, the method: public void meth1(int abc, String def) The signature of the method is the name meth1, the parameter types which is first an integer and then later a string.

Methods The return type of a method is the type of the value to be returned after calling the method. Consider the following class:

Methods public class MethodClass { int a=2; public int sum(int b) { return a+b; } public static void main(String st[]) { int p=4; MethodClass mClass=new MethodClass(); int result=mClass.sum(p); System.out.println("sum of "+2+"+"+b+" is "+result); }

Recursion A recursive method is a method that calls itself either directly or indirectly through other methods.

Recursion In writing a recursive method, two things are needed to consider: – How the recursion stop. – How to describe the original problem in terms of the same problem but with smaller size.

Recursive methods For example, consider a method used to calculate the factorial of an integer. By definition, the factorial of n, written as n!, is defined as: n!=n*(n-1)*(n-2)*..*2*1 Also, by definition, 0! is 1.

Factorial Consider the expressions of n! and (n-1)!: n!=n*(n-1)*...*2*1 (n-1)!=(n-1)*(n-2)....*2*1 We see that n! and (n-1)! are actually related in by the following equation: n!=n*(n-1)! So, the problem of finding the factorial of n becomes the problem of finding the factorial of (n-1).

Factorial We have just find a way of describing the problem of finding the factorial of an integer in terms of the same problem but with a smaller number. Now, we need to find a way to stop the recursion.

Factorial The value of 0! is known. So when we want to get the value 0!, we do not need to find the value of another factorial, we can simply return the value of 1. So the problem of finding the value of n! can be expressed mathematically as:

A static method of factorial public class Factorial { static int factorial(int n) { if (n==0) { return 1; } return n*factorial(n-1); } The method can be defined as a static method because it does not use any attributes of Factorial.

A multiplication method We want to implement an multiplication method of integers without using the multiplication operator. Now, we know that a*b can be expressed as a*(b-1)+a, so the original problem is expressed in terms of the a similar problem but with different size.

A multiplication method When will the recursion stop? We know that if b is 0, then the a*b must be 0. So we do not need to find another product. So mathematically, we can write:

A multiplication method public class Multiplication { static int multiply(int a, int b) { if (b==0) { return 0; } return multiply(a,b-1)+a; }

A multiplication method The multiply method actually has a problem. Consider the case of Multiplication.multiply(3,-2) If we follow the logic of the method multiply, we find that when we call the above method, it will then call the method Multiplication.multiply(3,-3), and then Multiplication.multiply(3,-4) and the process will never end until the program uses up all the resource of the computer.

A multiplication method The problem of the multiply method is that it has made an assume that Multiplication.multiply(a,b-1) is closer to the termination condition than Multiplication.multiply(a,b). But this is not the case for negative b. In fact, if b is negative, it is the other way round. So the correct method is:

A multiplication method public class Multiplication { static int multiply(int a, int b) { if (b==0) { return 0; } if (b>0) { return multiply(a,b-1)+a; } return multiply(a,b+1)-a; }

Signature of a method No two methods of a class can have the same signature. However, two methods of a class can have the same name provided that the parameter types are different. So a class can have the following methods:

Overloading Methods public class ABC { public void myMethod(int a) {..... } public void myMethod(String a) { } When you call myMethod of an object of ABC, the actual method to be called will be dependent on the type of the parameter.

Overloading methods Consider the following code:..... ABC abc=new ABC(); abc.myMethod(3); // this would call the version that accepts an integer abc.myMethod("abc"); // this would call the version that accepts a string.....