{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

05-Recursion - Quiz 1 Solution CS/ENGRD 2110...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
2/8/2011 1 CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2010 Thorsten Joachims Lecture 5: Recursion Quiz 1 Solution What IDE does the CS 2110 Staff recommend using for this class? (a) Eclipse (b) Dr. Java (c) NetBeans (d) Emacs 2. Integer num1 = new Integer(2110); Integer num2 = new Integer(2110); System.out.println((num1 == num2) + ", " + (num1.equals(num2))); What is the output of the previous code? (a) "true, true" (b) "true, false" (c) "false, true" (d) "false, false" 3. Animal parrot = new Bird(); What is the static type of the field above? (a) Bird (b) Object (c) Parrot (d) Animal 4. Which is the correct google group for this class? (a) cornell-cs2110 (b) cornell-cs3110-sp11 (c) cornell-cs2110-sp11 (d) cornell-cs2110-sp10 2 Recursion Overview Recursion is a powerful technique for specifying functions, sets, and programs Example recursively-defined functions and programs factorial combinations exponentiation (raising to an integer power) solution of combinatorial problems (i.e. search) Example recursively-defined sets grammars expressions data structures (lists, trees, ...) 3 The Factorial Function (n!) Define: n! = n·(n 1)·(n 2)···3·2·1 read : “n factorial” E.g., 3! = 3·2·1 = 6 The function int int that gives n! on input n is called the factorial function n! is the number of permutations of n distinct objects There is just one permutation of one object. 1! = 1 There are two permutations of two objects: 2! = 2 1 2 2 1 There are six permutations of three objects: 3! = 6 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 4 5 Permutations of Permutations of non- orange blocks Each permutation of the three non-orange blocks gives four permutations when the orange block is included Total number = 4·6 = 24 = 4! General: 0! = 1 (by convention) If n > 0, n! = n·(n-1)! A Recursive Program 6 static int fact(int n) { if (n == 0) return 1; else return n*fact(n-1); } Recursive definition of n!
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern