Project 8 - Recursion Fun - Program Design and Development | C SC 227, Study Guides, Projects, Research of Computer Science

Material Type: Project; Class: Full Course Title: Program Design and Development; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Fall 2007;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 08/31/2009

koofers-user-nvp-1
koofers-user-nvp-1 🇺🇸

9 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
C SC 227 Project 8: Recursion fun!
Due Date: Thursday, 1-Nov-2007 @ 10:00 pm via WebCat
This project asks you to solve six problems using recursion. Each of the six solutions
should be recursive, meaning that a method calls itself in order to find the solution.
The six methods outlined below should be placed in a single public Java class called
RecursionFun (in a file named RecursionFun.java). There should be a corresponding
set of JUnit test methods in a class called RecursionFunTest (in a file named
RecursionFunTest.java). Be sure to test your code thoroughly.
Note that each of the six methods should be declared static. This means that you don't
have to instantiate the class in order to call the methods. Any methods you create in
addition to the six should be both private and static.
We will provide some code for you to utilize when solving these problems. This code
includes a class named IntNode and two helper methods for Problem 6.
The code is included in this assignment as well as online at:
http://www.cs.arizona.edu/people/patrickv/project8.zip
Please note that this assignment specification is subject to change.
Problem 1: Finding the greatest common divisor
public static int gcd(int m, int n)
This method should compute the greatest integer that evenly divides the non-negative
integers m and n. “Evenly divides” means no remainder.
We define gcd(m,n) as follows using Euclid's algorithm:
if m is 0, gcd(m,n) = n
otherwise, gcd(m,n) = gcd(n, m % n)
Feel free to use Euclid’s algorithm or any other (recursive) algorithm to arrive at the
correct answers. Below are some examples of correct answers.
gcd(0,0) returns 0
gcd(12,0) returns 12
gcd(1,1) returns 1
gcd(2,4) returns 2
gcd(10,25) returns 5
pf3
pf4
pf5

Partial preview of the text

Download Project 8 - Recursion Fun - Program Design and Development | C SC 227 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

C SC 227 Project 8: Recursion fun!

Due Date: Thursday, 1-Nov-2007 @ 10:00 pm via WebCat

This project asks you to solve six problems using recursion. Each of the six solutions

should be recursive, meaning that a method calls itself in order to find the solution.

The six methods outlined below should be placed in a single public Java class called

RecursionFun (in a file named RecursionFun.java). There should be a corresponding

set of JUnit test methods in a class called RecursionFunTest (in a file named

RecursionFunTest.java). Be sure to test your code thoroughly.

Note that each of the six methods should be declared static. This means that you don't

have to instantiate the class in order to call the methods. Any methods you create in

addition to the six should be both private and static.

We will provide some code for you to utilize when solving these problems. This code

includes a class named IntNode and two helper methods for Problem 6.

The code is included in this assignment as well as online at:

http://www.cs.arizona.edu/people/patrickv/project8.zip

Please note that this assignment specification is subject to change.

Problem 1: Finding the greatest common divisor

public static int gcd(int m, int n)

This method should compute the greatest integer that evenly divides the non-negative

integers m and n. “Evenly divides” means no remainder.

We define gcd(m,n) as follows using Euclid's algorithm:

if m is 0, gcd(m,n) = n

otherwise, gcd(m,n) = gcd(n, m % n)

Feel free to use Euclid’s algorithm or any other (recursive) algorithm to arrive at the

correct answers. Below are some examples of correct answers.

gcd(0,0) returns 0 gcd(12,0) returns 12 gcd(1,1) returns 1 gcd(2,4) returns 2 gcd(10,25) returns 5

Problem 2: Reversing a String

public static String reverseString(String stringToReverse)

This method should take in a Java String and return a String that is reversed. If

null is passed in as the argument, return null. You must write the code that performs

this operation; use of anyone else’s reversal routine is prohibited.

Below are some examples of correct answers.

reverseString(null) returns null reverseString("") returns "" reverseString("1") returns "1" reverseString("abc") returns "cba" reverseString(" W") returns "W " reverseString("kayak") returns "kayak" reverseString("recursion is fun!") returns "!nuf si noisrucer"

Hint: take a look at the String documentation in the Java API. The API is linked to

from the class web site.

Problem 3: Summing up a list of nodes

public static int sumIntNodes(IntNode head)

This method should take in an IntNode reference and sum the data elements of every

IntNode following it. If null is passed in, return 0. We are providing the IntNode

class, so there is no need to write it yourself.

The following code should be placed in a file called IntNode.java:

public class IntNode { public int data; public IntNode next; public IntNode(int value) { data = value; next = null; } public IntNode(int value, IntNode nextNode) { data = value; next = nextNode; } }

On the next page are some examples of correct answers for Problem 3.

mergeSort(list)

if list has one or fewer elements

return list

else

let left, right, and result be new lists

sort the first half of list and store it in left

sort the second half of the list and store it in right

merge left and right, storing the values in result

return result

Even though pseudocode is provided, we are leaving the details up to you.

Problem 6: Solving n-Queens

public static ArrayList nQueens(int n)

Chess: a simple game to learn, nearly impossible to master, a royal pain to program...

Thankfully this problem only has a “chess-theme” to it. Your assignment is to solve the

classic n-Queens problem: given a chess board with n rows and n columns, arrange n

queens in such a fashion that they are not able to attack each other in one move (a queen's

movement is defined as any number of squares in any one direction per turn).

For this particular assignment, you will need to find ALL possible solutions for the given

n value and return them in an ArrayList of String objects (representing the

placement of queens on the board). While solving the problem, you will use a

boolean[][]to store queen locations, true meaning a queen is there, and false

meaning the square is empty.

Lost on where to start? To solve this problem, you will need to use recursive

backtracking. Place a queen in a safe spot, place the next one, and so on until you're out

of options. Then remove the prior one and keep going. Repeat. The section leaders will

be sure to do an example of this in section, as well as anytime they're on duty in the 228

lab.

Beyond that, you will need to use the “storefront” technique to help you, since the

recursive solution requires more input than the above method header. You will also be

given two private helper methods that should help you focus on the backtracking part of

this assignment instead of the array issues.

You are not required to use the methods, but it is HIGHLY RECOMMENDED (meaning

the section leaders will be better able to troubleshoot your code if they can isolate the

problems). The methods follow on the next pages. They should be placed in your

RecursionFun class.

/** This method will see if the given row and column values in the given board are safe. If no other queens can reach the given square, this method returns true. Otherwise, it returns false. */ private static boolean isSafe(int row, int column, boolean[][] board) { // check row for (int i = 0; i < board.length; i++) { if(board[i][column] && i != row) return false; } //check column for (int i = 0; i < board[0].length ; i++) { if(board[row][i] && i != column) return false; } //check diagonal int r = row-1, c = column-1; while (r >= 0 && c >= 0) { if(board[r][c]) return false; r--; c--; } r = row+1; c = column+1; while (r < board.length && c < board[r].length) { if(board[r][c]) return false; r++; c++; } r = row+1; c = column-1; while (r < board.length && c >= 0) { if(board[r][c]) return false; r++; c--; } r = row-1; c = column+1; while (r >= 0 && c < board[r].length) { if(board[r][c]) return false; r--; c++; } return true; }