Vous êtes sur la page 1sur 2

JavaProgramming:Arrays,Lists,andStructuredData

ProgrammingExercise:ImplementingtheCaesarCipher

Assignment1:WordPlay
Youwillwriteaprogramtotransformwordsfromafileintoanotherform,suchasreplacing
vowelswithanasterix.

Specifically,youshoulddothefollowing.
CreateanewclasscalledWordPlay.
Writeamethod
isVowel
thathasoneparameternamed
ch
.Thismethodreturnstrueif
ch
isavowel(oneofa,e,i,o,oruortheuppercaseversions)andfalseotherwise.You
shouldwriteatestermethodtoseeifthismethodworkscorrectly.Forexample,
isVowel(F) isVowel(a)
shouldreturnfalse,and shouldreturntrue.
Writeamethod
replaceVowels
thathastwoparameters,aStringnamed
phrase
anda
characternamed
ch
.ThismethodshouldreturnaStringthatisthestringphrasewithall
thevowels(uppercaseorlowercase)replacedby
ch
.Forexample,thecall
replaceVowels(HelloWorld,*) H*ll*W*rld
returnsthestring .Be
suretocallthemethod
isVowel
thatyouwroteandalsotestthismethod.
Writeamethod
emphasize
withtwoparameters,aStringnamed
phrase
anda
characternamed
ch
.ThismethodshouldreturnaStringthatisthestringphrasebutwith
thecharacter
ch
(upperorlowercase)replacedby
*
ifitisinanoddnumberlocationinthestring(e.g.thefirstcharacterhasan
oddnumberlocationbutanevenindex,itisatindex0)
+
ifitisinanevennumberlocationinthestring(e.g.thesecondcharacterhas
anevennumberlocationbutanoddindex,itisatindex1)
emphasize(dnactgaaactga,a)
Forexample,thecall wouldreturnthe
dn*ctg+*+ctg+
string ,andthecall
emphasize(MaryBella
Abracadabra,a) M+ryBell++br*c*d*br+
wouldreturnthestring .Be
suretotestthismethod.


JavaProgramming:Arrays,Lists,andStructuredData

Assignment2:CaesarCipher
YouwillstartwiththeCaesarCipheralgorithmyoulearnedaboutinthevideos,andyouwill
makesomeenhancementstoit,sothatitworkswithallletters(bothuppercaseandlowercase)
andtomakeitalittlebithardertodecrypt.
WritethesemethodsinaCaesarCipherclassyou
canuseinthenextlesson.
Specifically,youshoulddothefollowing:
Createanewclasscalled
CaesarCipher
.
Writethemethod
encrypt
thathastwoparameters,aStringnamed
input
andanint
named
key
.ThismethodreturnsastringthathasbeenencryptedusingtheCaesar
CipherAlgorithmexplainedinthevideos.Assumethatallthealphabeticcharactersare
uppercaseletters.Forexample,thecall

encrypt(FIRSTLEGIONATTACKEASTFLANK!,23)
shouldreturnthestring

CFOPQIBDFLKXQQXZHBXPQCIXKH!
Writethevoidmethod
testCaesar
thathasnoparameters.Thismethodshouldreada
fileandencryptthecompletefileusingtheCaesarCipheralgorithm,printingthe
encryptedmessage.Youmaywanttoincludethelines:
FileResourcefr=newFileResource()
Stringmessage=fr.asString()
Stringencrypted=encrypt(message,key)
System.out.println("keyis"+key+"\n"+encrypted)
Modifythe
encrypt
methodtobeabletohandlebothuppercaseandlowercaseletters.
encrypt(FirstLegion,23)
Forexample, shouldreturn
CfopqIbdflk
encrypt(FirstLegion,17)
and WzijkCvxzfe.
shouldreturn Besure
totestthe
encrypt
method.
Writethemethod
encryptTwoKeys
thathasthreeparameters,aStringnamed
input
,
andtwointegersnamed
key1
and
key2
.ThismethodreturnsaStringthathasbeen
encryptedusingthefollowingalgorithm.Parameter
key1
isusedtoencrypteveryother
characterwiththeCaesarCipheralgorithm,startingwiththefirstcharacter,and
key2
is
usedtoencrypteveryothercharacter,startingwiththesecondcharacter.Forexample,
encryptTwoKeys(FirstLegion,23,17)
thecall shouldreturn
Czojq
Ivdzle
.
F
Notethe i
isencryptedwithkey23,thefirst r
with17,the with23,
s
andthe with17,etc.Besuretotestthismethod.