Summer Session 2006: May 30, 2006 - June 30, 2006
Out on:
June 9, 2006
Due by:
June 12, 2006 before noon (hard deadline)
Collaboration:
None
Grading:
Documentation 10%, Style 10%, Functionality 80%
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.
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!
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:
boolean even( int n ) and
boolean odd( int n ) that call
each other recursively according to the
following idea: The number 0 is even, the number 1
is odd; a number n is even iff n-1 is odd, a number n
is odd iff n-1 is even. (You can assume that the
numbers are nonnegative; for added kicks, make the
assumption explicit with an assertion at the beginning
of each function.)
int fib_iterative( int n ) and
int fib_recursive( int n ) that compute the nth
Fibonacci Number.
(Once again, assume nonnegative or add an assertion to that
effect.)
void collatz( n ) that computes
the 3n+1 problem from Assignment 1
as specified there.
Yes, you have to print the number of recursive calls until
you get to 1 as well, just like you had to print the
number of iterations back then.
You'll have to introduce a second function (call it whatever
you want as long as the name makes sense) that does the
actual computation, collatz()
itself will just call that other function.
Why? Well, the only way to "keep track" of the number of
recursive calls that have happened so far is to add another
argument that keeps track of this information. In case of the
loop you had to add another variable and increment that. Here
you need to have an argument that is initially 0 and gets
"passed along" with 1 added for each call.
(Well, there's a way of doing it with a global variable, but
that's ugly and you'd have to add a second function anyway
since that "global counter" would have to be reset.)
Feel free to use even() or odd() from
above, or just write those tests directly using the remainder
operator as you did before.
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!
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!
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... */