COSC 135 A Chapter 8 Programming Assignment
Due 4-9-2003
This programming assignment is OPTIONAL and must be completed alone. If you chose to complete this assignment, your score (up to 25 points) will be added to your exam 2 score, however, your exam 2 score cannot exceed 100 points.

You will complete an application that combines exercises 20 and 21 on pages 408 and 409 of Wu's text, with some added requirements. In a file named Ceasar.java, write an application that asks the user whether they want to encode or decode text. In both cases you will need to ask them for the shift amount N (which should be a positive integer from 0 to 25). If they select encode, you should ask them for their message (a regular English sentence). For each letter in their message, you need to replace the letter with the corresponding letter that is N letters "larger" in the alphabet. That is, if N = 2, each 'a' becomes 'c', each 'b' becomes 'd', and so on. Note that 'y' becomes 'a' and 'z' becomes 'b' in this case. As well, capital letters should be converted to their corresponding capital letter (so each 'A' becomes 'C', each 'B' is replaced with a 'D' etc.). Here is one way to accomplish this shifting:
  1. Define String variables alph1="abcdefghijklmnopqrstuvwxyz" and alph2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
  2. Get the next character in the message.
  3. If the character is a lowercase letter (say nextLetter is a char variable that holds the value of a lowercase letter), let pos = alph1.indexOf(nextLetter) and replace nextLetter with alph1.charAt( (pos + N) % alph1.length() ).
  4. Replace each uppercase letter in a similar manner.
  5. If the next character is not a letter, do not replace it, but leave it as it was (so spaces and punctuation do not get changed, but they are part of the encoded message.
If the user selected decode, you should ask them for their coded text and then you need to shift each letter backwards by N letters in the alphabet, again leaving spaces and punctuation alone, and shifting lowercase letters to lowercase letters, and uppercase to uppercase. Because of the way that Java evaluates remainders, subtracting creates a small problem. In Java, -3 % 26 is -3, so you'll get index out of bounds errors if you try to replace nextLetter with alph1.charAt( (pos - N) % alph1.length() ), you need to use alph1.charAt( (pos - N + alph1.length() ) % alph1.length() ) instead (which just makes sure that the number you're calculating the remainder of is not negative).

Your code should repeatedly ask the user if they want to code or decode another message, until they say no.

You MUST use a StringBuffer in this assignment also.