11-07 Class Notes CS 107

11-07 Class Notes CS 107 -...

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

View Full Document Right Arrow Icon
See the online updates to program #5: partial credit for grading is given, along with suggestions on how to approach this problem one piece at a time. - Lab this week will again be using recursion, similar to what is required for the current program. - Announcements: Questions? What makes a computer program "smart": marble guessing game, paper-rock-scissors The class before that: recursion traces of simple methods Last Time: Review of program #5 suggestions Recursion traces The "maze" problem Today: //Illustrate simple recursion examples. import java.util.Scanner; // for keyboard input public class Recursion1 { Scanner keyboard = new Scanner( System.in); // Run all examples public void doAllProblems() { System.out.println("f1(8,3) = " + f1(8,3) ); System.out.println("f1(3,8) = " + f1(3,8) ); System.out.println("f2(2,4) = " + f2(2,4) ); System.out.println("f3(256) = " + f3(256) );
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: System.out.println("f4( 2,5) = " + f4( 2,5) ); } public int f1( int num1, int num2) { Recursion traces 11-07 Class Notes CS 107 Monday, November 07, 2011 2:09 PM CS 107 Fall 2011 Page 1 if (num1 > num2) return 0; else if (num2 == (num1 + 1)) return 1; else return f1( num1 + 1, num2 -1) + 2; } public double f2( double x, int n) { if (n == 0) return 0; else return x + f2( x, n-1); } // try this one with input of 256 int f3( int n) { if (n < 0) return f3(-n); else if (n<10) return n; else return f3( n/10); } // Call this with f4( 2,5) double f4( double x, int n) { if (n == 0) return 1.0; else if (n > 0) return f4( x, n - 1) * x; else { System.out.println("Error !"); return 0.0; } } }//end Class Recursion1 See other document under today's notes The "maze" problem: CS 107 Fall 2011 Page 2...
View Full Document

Page1 / 2

11-07 Class Notes CS 107 -...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online