Spring 2001
(Sections 03 & 06 only) |
More Information on Lab # 8 |
You should handle gracefully any input that is not an integer. (That is, you don't yet know enough to stop from printing the error line that will be printed to the screen, but you should be able to recognize when a non-integer result is entered and keep prompting for input that is acceptable.)
While not practical for encryption and decryption, since it doesn't change the message, it is very useful during development! Test your program first using that: While a correct result with an offset of 0 does not guarantee that your program will work for other keys (do test those too), any errors with no offset should be easy to spot (easier than with encrypted text) and may even help you to narrow down the places you have to look to locate and correct your error(s). Note that the starter-code provided assigns the variable called offset
to hold the value zero, but does not use it in any calculations: the actual offset of zero mentioned here is one that is fully exercised throughout the program.
NUM_CHARS
and a cipher OFFSET
that is negative, you can combine those values to get the equivalent positive shift. Here are some examples if NUM_CHARS
is 26:
OFFSET = -1
, that's the same as a shift of 26 - 1 = +25;OFFSET = -2
, that's the same a shift of 26 - 2 = +24;OFFSET = -15
, it's the same as a shift of 26 - 15 = +11.a % b
, the result should always have the same sign as a
. That is, if a
is negative, the result will be negative. (Note that this is not the same rule as is applied in a / b
, and that the rule that sets this up was actually embedded in an early quiz question.)
| OFFSET | > NUM_CHARS
?
Remember, the offset comes from user input but the alphabet to be accommodated is set by the programmer. Here are the obvious choices you should consider:
NUM_CHARS
is still 26, then what is a reasonable thing to do if a user requests an offset of 27? of 52? of -28? Do you handle that the same as an offset of 2147483648? -2147483648? Why or why not?)cipher[ ]
), so why not try to extend that concept here? (Specifically, permitting the user to input absolutely any integer-sized offset is not required to get a simple check-off for the lab, but it is strongly recommended for practice prior to future quizzes and exams.)If you agree to accept an offset that is larger in magnitude than the number of characters in your alphabet, then you should convert it to something inside that range before you use it anywhere else. That is, if you are converting 26 different letters, your cipher[ ]
array will have indices from 0 to 25, and you should do all your own computations with numbers in the range from -25 to + 25. Hint: What one useful arithmetic operator can you use to convert any integer value input by the program's user into an integer limited to that range?
character array
of length NUM_CHARS
to hold the ciphered equivalent of each character in the user input (which you'll gather in a moment...). That is, if the array is called cipher[ ]
, then: cipher[0]
should hold the ciphered equivalent of an 'a'
; cipher[1]
should hold the ciphered equivalent of 'b'
; and so on. Make sure you understand why that is the most appropriate way to do this. In addition:
"index"
and SHIFT
to compute the ciphered letter.char
acter type.<
) character to tell the program to read from the named file rather than the keyboard, and the greater-than (>)
character to tell it to write to a named file rather than the keyboard. For example:
%java Caesar < infile > outfile |
infile
" and writing its output to the file named "outfile
".
(On a Mac, where you don't have command lines, you do this in the tool called JBindery that you run instead of the DOS/Unix java command. Redirection of input and output are fairly obvious: they are menu-buttons in the main JBindery window. In settings where one wants to use other command-line arguments, they can be entered in JBindery's Optional Parameters window.)
cipher[ ]
, indexed from 0 through 25. You really should use it, but make sure you understand the examples above illustrating what it does.It is recommended (and the design is set up so) that you do the conversion for upper case letters as follows:
Character
class in your book to see if you can identify any other methods that may be useful to you.y
(as either y
or Y
). Otherwise, exit gracefully, acknowledging that the program run has completed. Be aware, however, that this step does not require any work with arrays, and arrays are what you should be practicing with this lab, so it's far more important that you do the rest of the tasks than that you complete this one.Back: | The opening page of this lab |
Up: | This Instructor's CSC-101 HomePage |
Way Up: | This Instructor's HomePage |