15-121 FALL 2009 (CORTINA)

HOMEWORK 1 - due Tuesday, January 19

WRITTEN PROBLEMS (10 pts)

For each of the following problems, write up your answers on paper (or use a word processor). Remember that the answers you write or type MUST be your own.

  1. (2 pts) Write a Java method that accepts two strings as parameters and returns a string that contains all unique two-character strings whose first character comes from the first string and second character comes from the second string. All two-character strings in your returned string should be separated by a space. Use ordinary string concatenation in your solution. Look at the examples below for a "hint" on how to proceed and look at the String API for methods you can use to simplify your solution.

    Examples:

    First string   Second string   Returned string
    ABCD           EFGH            AE AF AG AH BE BF BG BH CE CF CG CH DE DF DG DH
    ACDC           ABBA            AA AB CA CB DA DB
    

  2. (1 pt) In the previous problem, if the length of the first string is m and the length of the second string is n, what is the maximum length of your returned string in terms of m and n? What condition is required to get the maximum number of two-character strings?

  3. (1 pt) Rewrite your answer to #1 using a StringBuilder to build the returned string.

  4. (1 pt) Compare your answers for #1 and #3. Which will run faster in general? Why?

  5. (1 pt) A string consists of a series of names separated by commas, as shown in the example below:

    TOM,MARGARET,DAVE,TIM,BARBARA,VICTOR,ANGIE,MARK,SCOTT
    

    Write a method that takes a string of comma-delimited names as a parameter and prints out the names in the string, one per line using a Scanner. (HINT: The Scanner class has a useDelimiter method that will be helpful.)

  6. (2 pts) Write a Java method that requires a String parameter that contains the name of a text file that contains one integer per line. Your method should try to open the file and compute and return the sum of all of the integers in the file. If the file cannot be opened, return 0. You may assume that if the file can be opened, it contains only one integer per line, with no other text.

  7. (1 pt) Write a Java method that has a parameter referencing an array of integers. The method should return the maximum value stored in the array.

  8. (1 pt) Write a Java method that has a parameter referencing an array of integers. The method should return the index of the maximum value stored in the array. If there is a tie, return the index of the maximum closest to the beginning of the array.

PROGRAMMING PROJECT (10 pts)

See the instructions online on how to hand in your program electronically. Electronic handin for this assignment will be available starting Thurs by 5:00PM.

Consider the following encryption scheme to encode messages. Create a matrix with the letters of the alphabet as follows:

     0   1   2   3   4
0    A   B   C   D   E
1    F   G   H   I   J
2    K   L   M   N   O
3    P   Q/Z R   S   T
4    U   V   W   X   Y

Then to encode any letter, create a string consisting of the row number followed by the column number. For example, the letter "G" becomes "11" and the letter "T" becomes "34".

To encrypt a message, first convert each letter to its corresponding two-digit string. For example:

GATES HILLMAN CENTER
1100340433 12132121220023 020423340432
Then, using a key word that does not have any repeated letters, create a grid of the digits using the numerical sequence of your converted message such that the number of columns is the same as the length of the key word. For example, if the keyword is CORTINA, we would have:

C O R T I N A
1 1 0 0 3 4 0
4 3 3 1 2 1 3
2 1 2 1 2 2 0
0 2 3 0 2 0 4
2 3 3 4 0 4 3
2

Next, rearrange the grid so that the letters of the key word are in alphabetical order, shifting the corresponding columns of digits along with the letters. In our example, we would get

A C I N O R T
0 1 3 4 1 0 0
3 4 2 1 3 3 1
0 2 2 2 1 2 1 
4 0 2 0 2 3 0
3 2 0 4 3 3 4
  2

Finally, the message would be written as a sequence of digits, column by column (ignoring the letters at the top of each column):

03043 142022 32220 41204 13123 03233 01104

This is the final encoded message that is transmitted. In order to decode the message, the decrypter would have to work in reverse, as long as he or she knows the secret key word. Without this, cracking this code is much harder (but certainly not impossible).

Assignment

Write a Java program that reads in a message from the keyboard. After the message is entered, read in a key word. If the key word contains duplicate letters or non-letters, ask the user for another key word until a valid key word is entered. The user may input the message and key word using uppercase and lowercase letters. Convert everything to uppercase and remove all digits and punctuation from the message.

The program should compute the encrypted version of the input message using the algorithm above. You may assume for this assignment that the message, when converted to digits, will be at least as long as its corresponding key word.

Your solution should include methods to perform each of the steps shown above. Define your variables inside of your main method as local variables. Then as you call each of your methods, pass whatever variables are needed to the method. Do NOT define all of your variables as instance ("global") variables to avoid parameter passing.

Coding Style

Your code should include comments for each method you write. Each method (except main) should have a comment that gives its precondition (what is assumed to be true before it runs) and a postcondition (what should be true and/or what it returns after it runs). The precondition and postcondition usually depend on the parameters of the method.

You should also use standard indentation and variable-naming conventions to make your code easily readable. Finally, you should review each of your methods to see if you are writing redundant or excessive code. Look for places where you can take advantage of patterns to simplify your code. A portion of your programming grade will be based on your coding style.

Required Input and Output Format

Once your program reads in the message and the key word, your program should output the original message first in uppercase with spaces but without digits or punctuation, followed by the numerical sequence for the message, then the grid of numbers for the key word before the columns are sorted, then the grid of numbers for the key word after the columns are sorted, and finally the encrypted message as a sequence of digit "words".

You should follow the given guidelines and sample format shown below for maximum credit. Here is a sample run (user input in italics):

Please enter your message:
GATES HILLMAN center-6017
Please enter your key word:
simpson
Please enter your key word:
7UP
Please enter your key word:
ABBA
Please enter your key word:
cortina

GATES HILLMAN CENTER
1100340433 12132121220023 020423340432
C O R T I N A
1 1 0 0 3 4 0
4 3 3 1 2 1 3
2 1 2 1 2 2 0
0 2 3 0 2 0 4
2 3 3 4 0 4 3
2  
A C I N O R T
0 1 3 4 1 0 0
3 4 2 1 3 3 1
0 2 2 2 1 2 1
4 0 2 0 2 3 0
3 2 0 4 3 3 4
  2
03043 142022 32220 41204 13123 03233 01104

ACADEMIC INTEGRITY

Remember that you must follow the policies of this course with regard to academic integrity. The work you submit must be your own work and ONLY your own work. You may discuss the problems or Java issues with your class mates or others (e.g. "where did we cover this topic in class?", "how do you write a do/while loop again?", "what does this syntax error mean?", etc.) but when you work on the assignment, you should do your OWN WORK. If you work with a group of friends, try sitting in separate locations when you write your answers so you do not get tempted to work as a group and submit a common or copied answer. Start working early so if you run into trouble, you can come to see the course staff for assistance.

DISCUSSION BOARD: There will be a discussion board set up on Blackboard for the first assignment so you can ask general questions about the assignment or about Java for the course staff or for other students to answer. You should treat the discussion board with the same academic integrity policies outlined above and in the academic integrity policy statement on the course website. Please use appropriate etiquette when writing on the discussion board.

HAND-IN INSTRUCTIONS & LATE SUBMISSIONS

See the course website for instructions on how to hand in your answers and the policy for late submissions.