Assignment 2, due Jan 29

Solutions

Part of the homework for CS:2820, Spring 2016
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

  1. Background: Get a copy of the code for mp1.java (see the assignment for Machine Problem 1).

    a) What is the name of the encryption system supported by this program. (Hint: Read the comments, and if you are not sure if this is a proper name for an encryption system, look it up in Wikipedia.) (0.5 points)

    ROT13 -- or Rotate 13, a specific example of a Caesar cypher based on rotating the alphabet 13 places. Julius Caesar is supposed to have encrypted his secret messages by rotating 4 places.

    b) The program uses some ugly code such as:
        if ((c >= 'a') && (c <= 'z')) {
    How would the program's behavior change if you wrote this code?
        if (Character.isLowerCase(c)) {
    (Hint: Google the official documentation for the Java isLowerCase() predecate. If you use the right import statement, you can leave off the Character. prefix.) (0.5 points)

    The isLowerCase() method reports true for the basic Roman letters 'a' to 'z' and also for the lower case Greek letters 'α' to 'ω' (alpha to omega), lower case Russian, and a variety of variant and accented lower case letters. The ROT13 cypher only works for the basic Roman alphabet, so using the isLowerCase() method would be wrong in this context.

  2. Background: Look at the recursive function given on Homework 1, problem 1.

    A simple programming problem: Translate the informal function definition given in Homework 1 into a Java method, and then write a program to call that method and print the successive values of its output. Here is an example of what the first few lines of your output should look like:

       f(0)=0
       f(1)=1
       f(2)=1
       f(3)=0
       f(4)=-1
    

    Handwritten code is sufficient, but you can be more assured that it works if you write a little Java program to call it for successive integer values.

    Note that a complete, cleanly formatted solution requires fewer than 20 lines of code. You will be graded on clean formatting! Any java program can legally be written as one line of code, but it's not very readable if you do this. (1 point)

    public class Mp2 {
            private static int f(int i) {
                    if (i < 2) return i;
                    return f( i - 1 ) - f( i - 2 );
            }
    
            public static void main(String[] args) {
                    int i = 0;
                    while (true) {
                            System.out.println( "f(" + i + ")=" + f(i) );
                            i = i + 1;
                    }
            }
    }
    

    Note that the assignment asked for clean formatting, not comments.

  3. Background: Java supports 3 kinds of comments:
    — line-end comments //like this
    — in-line comments /* like this */ within program text,
    — and a special class of comments /** like this */

    A question: What application processes the final category of comments? (Hint: The assigned reading in the textbook answers this question.)

    The javadoc application.