12-6 Write a recursive definition of a valid Java identifier (see chapter 2).



Similar documents
J a v a Quiz (Unit 3, Test 0 Practice)

Chapter 5. Recursion. Data Structures and Algorithms in Java

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Example of a Java program

Unit 6. Loop statements

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Introduction to Java

Recursion and Recursive Backtracking

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Moving from CS 61A Scheme to CS 61B Java

Part I. Multiple Choice Questions (2 points each):

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

AP Computer Science Java Mr. Clausen Program 9A, 9B

Conditionals (with solutions)

8 Primes and Modular Arithmetic

COMPUTER SCIENCE. Paper 1 (THEORY)

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Python for Rookies. Example Examination Paper

Section IV.1: Recursive Algorithms and Recursion Trees

3 length + 23 size = 2

Iteration CHAPTER 6. Topic Summary

Chapter 3. if 2 a i then location: = i. Page 40

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Factoring Algorithms

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Sample CSE8A midterm Multiple Choice (circle one)

AP Computer Science Java Subset

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Solving Problems Recursively

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

ML for the Working Programmer

I PUC - Computer Science. Practical s Syllabus. Contents

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

LOOPS CHAPTER CHAPTER GOALS

In this Chapter you ll learn:

Chapter 2: Elements of Java

West Virginia University College of Engineering and Mineral Resources. Computer Engineering 313 Spring 2010

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Patterns in Pascal s Triangle

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Lecture 3: Finding integer solutions to systems of linear equations

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

10.2 Series and Convergence

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Topic 11 Scanner object, conditional execution

CS 111 Classes I 1. Software Organization View to this point:

Basic Programming and PC Skills: Basic Programming and PC Skills:

CmpSci 187: Programming with Data Structures Spring 2015

Programming Exercises

2.3 WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Answers to Review Questions Chapter 7

Recursion and Dynamic Programming. Biostatistics 615/815 Lecture 5

CS170 Lab 11 Abstract Data Types & Objects

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

10CS35: Data Structures Using C

Sources: On the Web: Slides will be available on:

(Eng. Hayam Reda Seireg) Sheet Java

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Section 4.2: The Division Algorithm and Greatest Common Divisors

Dynamic Programming. Lecture Overview Introduction

Java Program Coding Standards Programming for Information Technology

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Part 3: GridWorld Classes and Interfaces

The Euclidean Algorithm

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Homework/Program #5 Solutions

CS 2412 Data Structures. Chapter 2 Stacks and recursion

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Basic Java Syntax. Program Structure

8 Divisibility and prime numbers

Grade 6 Math Circles. Binary and Beyond

Lecture Notes on Binary Search Trees

Building Java Programs

CHAPTER 5. Number Theory. 1. Integers and Division. Discussion

Sample Questions Csci 1112 A. Bellaachia

Continued Fractions and the Euclidean Algorithm

Chapter 2 Introduction to Java programming

AP Computer Science Static Methods, Strings, User Input

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Transcription:

CHAPTER 12 Recursion Recursion is a powerful programming technique that is often difficult for students to understand. The challenge is explaining recursion in a way that is already natural to the student. Many thought processes are recursive in nature and can be used to ease the students into thinking recursively. One danger with teaching recursion is an attempt to solve all problems recursively. There are problems that are elegantly solved using recursion and there are problems that are not. The student should be encouraged to develop and understanding of when to apply recursion. Exercises 12-6 Write a recursive definition of a valid Java identifier (see chapter 2). An identifier is a letter, underscore, or dollar. An identifier is also an identifier followed by a letter, digit, underscore, or dollar sign. 12-7 Write a recursive definition of x y (x to the power y), where x and y are integers and y > 0. N(y) = N(y-1) * x; N(0) = 1; 12-8 Write a recursive definition of i * j (integer multiplication), where i > 0. Define it in terms of integer addition. M(i) = M(i-1) + j; M(0) = 0; 12-9 Write a recursive definition of the Fibonacci numbers. The Fibonacci numbers are a sequence of integers, each of which is the sum of the previous two numbers. The first two numbers in the sequence are 0 and 1. Explain why you would not normally use recursion to solve this problem. F(1) = 0; F(2) = 1; F(N) = F(N-1) + F(N-2); 12-10 Trace the execution of the Palindromes program when it is run against the string "wxyzzyxw". Produce a diagram similar to the one shown in Figure 12.3. Explain the processing of each step. 12-11 Repeat Problem 12-10 using the string "abcba". What is the primary difference between the string used in Problem 12-10 and this one? Explain how the program handles that difference. 12-12 Repeat Problem 12-10 using the string "abcxycba". 12-13 Write an iterative version of the Palindromes program. Compare and contrast it with the recursive version presented in this chapter. Java Software Solutions: Foundations of Program Design 391

12-14 Write a recursive method to determine and print the value of N! (N factorial) using the definition given in this chapter. Explain why you would not normally use recursion to solve this problem. 12-15 Write a recursive method to reverse a String. Explain why you would not normally use recursion to solve this problem. Programming Projects 12-10 Redesign and implement the recursive program that calculates the sum of the integers between 1 and N to match the recursive definition below. Trace your solution using an N of 5. N i = i = 1 N --- 2 i + i i = 1 N N i = --- + 1 2 ******************************************************************* Formula.java In Text Application Authors: Lewis and Loftus Classes: Formula, Sigma ******************************************************************* ------------------------------------------------------------------- Class Formual is the main class for the program. It initializes a Sigma objects and prints out its sum. Methods: public static void main (String[] args) ------------------------------------------------------------------- public class Formula { public static void main (String[] argv) { Java Software Solutions: Foundations of Program Design 392

int high = 5; Sigma e = new Sigma(); System.out.println ("The sum all numbers from 1 and " + high + " is: " + e.sum ()); method main class Formula ------------------------------------------------------------------- Class Sigma performs the summation for the formula. Constructor: public Sigma() public Sigma (int upper) Methods: public int sum() private int sum (int lower, int upper) ------------------------------------------------------------------- class Sigma { Starting upper bound private int initial_upper; =========================================================== Initialization of and upper bound =========================================================== public Sigma (int upper) { initial_upper = upper; constructor Sigma =========================================================== Default constructor =========================================================== public Sigma() { initial_upper = 5; constructor Sigma Java Software Solutions: Foundations of Program Design 393

=========================================================== Initial call to start calculations =========================================================== public int sum() { return this.sum (1, initial_upper); method sum =========================================================== Recursive sum for the formula =========================================================== private int sum (int lower, int upper) { if (lower < upper) { int middle = (lower + upper - 1) / 2; Recursive call return sum (lower, middle) + sum (middle + 1, upper); else { return lower; method sum class Sigma 12-11 Design and implement a program that implements Euclid s algorithm for finding the greatest common divisor of two positive integers. The greatest common divisor is the largest integer that divides both values without producing a remainder. In a class called Divisor_Calc, define a method called gcd that accepts two integers, num1 and num2. The algorithm is defined as follows: gcd (num1, num2) is num2 if num2 <= num1 and num2 divides num1 gcd (num1, num2) is gcd (num2, num1) if num1 < num2 gcd (num1, num2 is gcd (num2, num1%num2) otherwise ******************************************************************* Euclid.java In Text Application Authors: Lewis and Loftus Classes: Euclid, Divisor_Calc Java Software Solutions: Foundations of Program Design 394

******************************************************************* ------------------------------------------------------------------- Class Euclid is a test driver for the Divisor_Calc class. Methods: public static void main (String[] argv) ------------------------------------------------------------------- public class Euclid { public static void main (String[] argv) { int num1 = 792; int num2 = 348; Divisor_Calc euclid = new Divisor_Calc(); System.out.println ("The gcd of " + num1 + " and " + num2 + " is: " + euclid.gcd (num1, num2)); method main class Euclid ------------------------------------------------------------------- Class Divisor_Calc is used to calculate gcd using Euclid's algorithm Methods: public int gcd (int num1, int num2) ------------------------------------------------------------------- class Divisor_Calc { =========================================================== Recursively solves the gcd =========================================================== public int gcd (int num1, int num2) { Java Software Solutions: Foundations of Program Design 395

if (num2 <= num1 && num1 % num2 == 0) { return num2; else { if (num1 < num2) { return gcd (num2, num1); else { return gcd (num2, num1 % num2); method gcd class Divisor_Calc 12-12 Modify the maze problem so that it prints out the path of the final solution without storing it. ******************************************************************* Maze_Search.java In Text Application Authors: Lewis and Loftus Classes: Maze_Search Maze ******************************************************************* ------------------------------------------------------------------- Class Maze_Search contains the driver of a program that demonstrates the use of recursion to solve a maze. Methods: public static void main (String[] args) ------------------------------------------------------------------- public class Maze_Search { =========================================================== Java Software Solutions: Foundations of Program Design 396

Creates a new maze, prints its original form, attempts to solve it, and prints out its final form. =========================================================== public static void main (String[] args) { Maze labyrinth = new Maze(); labyrinth.print_maze(); if (labyrinth.solve(0, 0)) System.out.println ("Maze solved!"); else System.out.println ("No solution."); method main class Maze_Search ------------------------------------------------------------------- Class Maze represents a maze of characters. The goal is to get from the top left corner to the bottom right, following a path of 1's. Methods: public void print_maze () public boolean solve (int row, int column) private boolean valid (int row, int column) ------------------------------------------------------------------- class Maze { int[][] grid = {{1,1,1,0,1,1,0,0,0,1,1,1,1, {1,0,1,1,1,0,1,1,1,1,0,0,1, {0,0,0,0,1,0,1,0,1,0,1,0,0, {1,1,1,0,1,1,1,0,1,0,1,1,1, {1,0,1,0,0,0,0,1,1,1,0,0,1, {1,0,1,1,1,1,1,1,0,1,1,1,1, {1,0,0,0,0,0,0,0,0,0,0,0,0, {1,1,1,1,1,1,1,1,1,1,1,1,1; =========================================================== Java Software Solutions: Foundations of Program Design 397

Prints the maze grid. =========================================================== public void print_maze () { System.out.println(); for (int row=0; row < grid.length; row++) { for (int column=0; column < grid[row].length; column++) System.out.print (grid[row][column]); System.out.println(); System.out.println(); method print_maze =========================================================== Attempts to recursively traverse the maze. It inserts special characters indicating locations that have been tried and that eventually become part of the solution. =========================================================== public boolean solve (int row, int column) { boolean done = false; if (valid (row, column)) { grid[row][column] = 3; cell has been tried if (row == grid.length-1 && column == grid[0].length-1) done = true; maze is solved else { done = solve (row+1, column); down if (!done) done = solve (row, column+1); right if (!done) done = solve (row-1, column); up if (!done) done = solve (row, column-1); left if (done) part of the final path System.out.println ("Path: " + row + ", " + column); Java Software Solutions: Foundations of Program Design 398

return done; method solve =========================================================== Determines if a specific location is valid. =========================================================== private boolean valid (int row, int column) { boolean result = false; check if cell is in the bounds of the matrix if (row >= 0 && row < grid.length && column >= 0 && column < grid[0].length) check if cell is not blocked and not previously tried if (grid[row][column] == 1) result = true; return result; method valid class Maze 12-13 Design and implement a recursive program which solves the Non-Attacking Queens problem. Determine how eight queens can be positioned on an eight-by-eight chessboard so that none of them are in the same row, column, or diagonal as any other queen. There are no other chess pieces on the board. ******************************************************************* Eight_Queens.java In Text Application Authors: Lewis and Loftus Classes: Eight_Queens Board ******************************************************************* ------------------------------------------------------------------- Class Eight_Queens contains the driver of a program that demonstrates the use of recursion to solve the eight queens problem. Java Software Solutions: Foundations of Program Design 399

Methods: public static void main (String[] args) ------------------------------------------------------------------- public class Eight_Queens { =========================================================== Creates a new maze, prints its original form, attempts to solve it, and prints out its final form. =========================================================== public static void main (String[] args) { Board chess_board = new Board(); boolean success = false; for (int column = 1; (column <= Board.board_size) &&! success; column++) { success = Board.place_piece (1, column, 1); if (!success) { System.out.println ("No configuration possible!"); method main eight_queens ------------------------------------------------------------------- Class Board contains the representation of a chess board and the ability to check for threatening queens. Constructors: public Board (int size) public Board () Methods: public static boolean place_piece (int r, int c, int num) private static boolean threatened (int r, int c) private static boolean check_for_piece Java Software Solutions: Foundations of Program Design 400

------------------------------------------------------------------- class Board { public static int board_size, queens; private static boolean board [][]; =========================================================== Creates a Board of a particular size with a similar number of queens. =========================================================== public Board (int size) { board_size = queens = size; board = new boolean [board_size + 1][board_size + 1]; constructor Board =========================================================== Creates an 8x8 Board with 8 queens. =========================================================== public Board () { board_size = queens = 8; board = new boolean [board_size + 1][board_size + 1]; constructor Board =========================================================== Puts a numbered queen in row r and column c. =========================================================== public static boolean place_piece (int r, int c, int num) { boolean success = false; if (num == queens) { System.out.println ("Queen positioned at " + r + ", " + c); success = true; else { board [r][c] = true; for (int row = num + 1; row < board_size+1 &&!success; row++) { for (int column = 1; column < board_size+1 &&!success; column++) { if (!threatened (row, column) &&!board [row][column]) { success = place_piece (row, column, num + 1); Java Software Solutions: Foundations of Program Design 401

if (!success) { board [r][c] = false; else { System.out.println ("Queen positioned at " + r + ", " + c); return success; method place_piece =========================================================== Returns true if the queen at row r and column c is threatened by any other queen. =========================================================== private static boolean threatened (int r, int c) { return check_for_piece (-1, -1, r, c) check_for_piece (-1, 1, r, c) check_for_piece (1, -1, r, c) check_for_piece (1, 1, r, c) check_for_piece (0, -1, r, c) check_for_piece (-1, 0, r, c) check_for_piece (0, 1, r, c) check_for_piece (1, 0, r, c); method threatened =========================================================== Returns true if a queen is threatened in a particular direction. =========================================================== private static boolean check_for_piece (int x_dir, int y_dir, int r, int c) { if (c + x_dir < board_size + 1 && c + x_dir > 0 && r + y_dir < board_size + 1 && r + y_dir > 0) { if (board [r + y_dir] [c + x_dir]) return true; else { return check_for_piece (x_dir, y_dir, r + y_dir, c + x_dir); else return false; Java Software Solutions: Foundations of Program Design 402

method check_for_piece class Board 12-14 In the language of an alien race, all words take the form of Blurbs. A Blurb is a Whoozit followed by one or more Whatzits. A Whoozit is the character x followed by zero or more ys. A Whatzit is a q followed by either a z or a d, followed by a Whoozit. Design and implement a recursive program that generates random Blurbs in this alien language. 12-15 Design and implement a recursive program to determine if a string is a valid Blurb as defined in Problem 12-14. Java Software Solutions: Foundations of Program Design 403