600.107: Introduction to Programming

Summer Session 2006: May 30, 2006 - June 30, 2006

Assignment 4: Recursive Potpourri

Out on: June 9, 2006
Due by: June 12, 2006 before noon (hard deadline)
Collaboration: None
Grading: Documentation 10%, Style 10%, Functionality 80%

Overview

Assignment 4 once again consists of several small programming problems, this time including some use of recursion, i.e. you need to write some recursive methods to do certain things. I also included some problems that will force you to read more Java documentation on your own (although not very much). All previously covered concepts are of course applicable as well.

As with the previous assignment, please follow these ground rules: Try to write your functions in such a way that only one return instruction at the end of your function is necessary! Try to write your procedures without explicit return instructions! Try to make variables as local as possible to the place where they are needed, procedures and functions should communicate only through arguments and results!

Important: Please note that the grading scheme changed again, we are now grading you on documentation as well. This includes "external" documentation of classes and methods using javadoc as well as "internal" documentation, i.e. all other comments you put in your code.

Notes: If you have problems with any of these tasks, please contact the staff or everybody in class using the mailing lists! Do this early so you have a shot at finishing the assignment. Also, if you can't solve one problem, feel free to skip it and work on the problems you can solve. Handing in something is much better than not handing in anything.

Problem 0: Recursive Warmup (20%)

The first exam included the following problem:

"Consider the positive integer number line including zero. You have two pebbles, one red, one blue. Describe your own Pebble Game for adding two numbers. You can only move each pebble up or down the number line one step at a time. ..."

The sample code for Lecture 4 includes an iterative solution of this problem. Note that all this implementation does is add or subtract 1 and compare to 0, and that's all you are allowed to use in terms of basic operations as well.

Your job is to write a recursive implementation of this pebble game! You'll also include the iterative version for comparison, but that's not the main focus. Here are the details: Write a class Recurse with the following two methods:

 int add_iterative( int red, int blue )
 int add_recursive( int red, int blue )

Each of these should implement the "addition pebble game", the first iteratively, the second recursively. You can "grab" the iterative version from the lecture examples, or you can write it from scratch yourself. The recursive version can not use any kind of loop, but if instructions are okay of course (even essential).

Please describe the principle on which these methods work as part of their javadoc comment. Describe how the state of the computation evolves for the iterative version, and how the recursive version distinguishes base case and inductive case (i.e. what's the "simple problem" and how do you map an instance of a "complex problem" to simpler ones and eventually to the "simple problem" you know how to solve already?).

The void main( String[] args ) method of your class should test the two versions of the pebble game using assertions. Be sure to include enough test cases so that each instruction in your functions will actually be executed at least once. And don't forget about style and documentation!

Problem 1: Buckets of Recursion (30%)

Alright, now that you're on the road to recursive wonderland, let's try a few more. Implement a class Buckets that consists of five (mostly unrelated) methods, four of which must be implemented recursively, that is without any loops:

The void main( String[] args ) method of your class should test the first four functions using assertions. For the collatz() procedure, you can simply include two or three example calls instead of assertions. Be sure to include enough test cases so that each instruction in your methods will actually be executed at least once. And don't forget about style and documentation!

Problem 2: Simplistic Cryptography (50%)

Important: You are not required to use recursion for this problem. You can of course use it, but you shouldn't need it anywhere as far as I can see. Also, it's up to you how you organize the program, i.e. how many functions, procedures, variables, constants you use, how you handle input and output in detail, etc. That doesn't mean organization is unimportant, it just means that we don't give you any guidance on the details.

For this problem, you're going to write a program that helps with ROT 13 encryption and decryption. When started, your program should display a menu like this one:

  Welcome to the 600.107 ROT 13 Helper!
  1 = Encrypt phrase
  2 = Decrypt phrase
  3 = About
  0 = Quit
  What do you want to do today?

If the user enters a 0 you should leave the program. If the user enters a 3 you should print some message (up to you) about the program and its author. The details for 1 and 2 are explained below, but an illegal input should repeat the menu again, ideally after a short error message.

If the user enters 1 or 2, you should prompt for a short message to encrypt or decrypt as the case may be. You need to figure out how to read in a String using the Scanner class on your own. After the message has been entered, you should encrypt/decrypt it and print the result back out to the user, followed by returning to the menu.

The ROT 13 cypher works by replacing each letter in a message with the letter "13 places futher down" in the alphabet. All other characters are not affected in any way. Of course we have to "bend" things around in order to have a letter that's "13 places down from U" for example. Fortunately you can use the % operator to your advantage again to make that happen...

Aside from figuring out stuff about Scanner you'll also need to look up some of the things you can do with the String type, for example the charAt() method to get the character at a certain position in the string. Note that your program doesn't have to work on all Unicode characters (how could it, it assumes that there are just 26 letters in the alphabet!), if it works on ASCII that's good enough. Enjoy you first "open ended" problem! And don't forget about style and documentation!

Deliverables

Submit your solutions by email to the submission address listed on the course website. Please make sure that you answer written questions in the text of the email. Please make sure that you attach programming questions as separate somename.java files. Note that you'll get a confusing automatic reply that I will explain later.

Finally, please include your name and email address as well as a brief comment explaining how to use your programs at the top of each Java file. Here's a template you can clone:

/*
 * CS 107
 * Assignment x: Problem y
 *
 * Your Name Here
 * Your Email Here
 *
 * Description of your program and any comments you may have
 * for the grader go here...
 */
Updated: $Id: assignment-4.html 137 2006-06-10 01:58:59Z phf $ Validate: XHTML CSS
Copyright © 2006 Peter H. Fröhlich. All rights reserved.