Java Programming: Arrays, Lists, and Structured Data
Programming Exercise: Implementing the Caesar Cipher
Assignment 1: Word Play
You will write a program to transform words from a file into another form, such as replacing
vowels with an asterix.
Specifically, you should do the following.
● Create a new class called WordPlay.
● Write a method
isVowel h
that has one parameter named c . This method returns true if
ch
is a vowel (one of a, e, i, o, or u or the uppercase versions) and false otherwise. You
should write a tester method to see if this method works correctly. For example,
isVowel(‘F’) sVowel(‘a’)
should return false, and i should return true.
● Write a method that has two parameters, a String named
replaceVowels phrase
and a
character named
ch
. This method should return a String that is the string phrase with all
h
the vowels (uppercase or lowercase) replaced by c . For example, the call
replaceVowels(“Hello World”, ‘*’)
returns the string
“H*ll* W*rld”
. Be
sure to call the method
isVowel
that you wrote and also test this method.
● Write a method
emphasize hrase
with two parameters, a String named p and a
character named
ch
. This method should return a String that is the string phrase but with
the character
ch
(upper or lowercase) replaced by
○ ‘*’
if it is in an odd number location in the string (e.g. the first character has an
odd number location but an even index, it is at index 0)
○ ‘+’
if it is in an even number location in the string (e.g. the second character has
an even number location but an odd index, it is at index 1)
mphasize(“dna ctgaaactga”, ‘a’)
For example, the call e would return the
string
“dn* ctg+*+ctg+”
, and the call
emphasize(“Mary Bella
Abracadabra”, ‘a’)
would return the string
“M+ry Bell+ +br*c*d*br+”
. Be
sure to test this method.
Java Programming: Arrays, Lists, and Structured Data
Assignment 2: Caesar Cipher
You will start with the Caesar Cipher algorithm you learned about in the videos, and you will
make some enhancements to it, so that it works with all letters (both uppercase and lowercase)
rite these methods in a CaesarCipher class you
and to make it a little bit harder to decrypt. W
can use in the next lesson.
Specifically, you should do the following:
● aesarCipher
Create a new class called C .
● Write the method that has two parameters, a String named i
encrypt nput
and an int
named
key
. This method returns a string that has been encrypted using the Caesar
Cipher Algorithm explained in the videos. Assume that all the alphabetic characters are
uppercase letters. For example, the call
encrypt(“FIRST LEGION ATTACK EAST FLANK!”, 23)
should return the string
“CFOPQ IBDFLK XQQXZH BXPQ CIXKH!”
● Write the void method
testCaesar
that has no parameters. This method should read a
file and encrypt the complete file using the Caesar Cipher algorithm, printing the
encrypted message. You may want to include the lines:
FileResource fr = new FileResource();
String message = fr.asString();
String encrypted = encrypt(message, key);
System.out.println("key is " + key + "\n" + encrypted);
● Modify the
encrypt
method to be able to handle both uppercase and lowercase letters.
For example,
encrypt(“First Legion”, 23) Cfopq Ibdflk”
should return “
andencrypt(“First Legion”, 17)
should return
“Wzijk Cvxzfe”.Be sure
to test the
encrypt
method.
● Write the method that has three parameters, a String named i
encryptTwoKeys nput
,
and two integers named nd
key1 a key2
. This method returns a String that has been
ey1
encrypted using the following algorithm. Parameter k is used to encrypt every other
ey2
character with the Caesar Cipher algorithm, starting with the first character, and k is
used to encrypt every other character, starting with the second character. For example,
the call
encryptTwoKeys(“First Legion”, 23, 17) Czojq
should return “
Ivdzle”
.
Note the
‘F’
is encrypted with key 23, the first
‘i’
with 17, the
‘r’
with 23,
and the
‘s’
with 17, etc. Be sure to test this method.