elseif((index+theOffset)<0){//Otherwise if the index of ciphered character is less than zero, the index of the ciphered character is
index=letters.length+(index+theOffset);
index=letters.length+index+theOffset;//the sum of the length of the alphabet array, the index and theOffset.
}
}
else{
else{//Otherwise if the calculated index of the ciphered character is greater than length of the alphabet array, the index of the ciphered character is
index=(index+theOffset)-letters.length;
index=(index+theOffset)-letters.length;//subtracting the length of the alphabet from the sum of the index and theOffset.
}
}
}
}
if(index==-1){
if(index==-1){//If the ciphered character cannot be calculated.
returntheChar;
returntheChar;//Return the original character.
}
}
else{
else{//If the ciphered character can be calculated.
returnletters[index];
returnletters[index];//Return the character at the new index.
}
}
}
}
...
@@ -65,12 +73,12 @@ public class AssessmentPartThree {
...
@@ -65,12 +73,12 @@ public class AssessmentPartThree {
// 06 - encryptedMessage
// 06 - encryptedMessage
// Complete this method so that it uses encryptedCharacter to
// Complete this method so that it uses encryptedCharacter to
// return the encrypted version of theMessage using theOffset
// return the encrypted version of theMessage using theOffset
Stringmessage="";
Stringmessage="";//Initialises variable for the cipher text.
for(inti=0;i<theMessage.length();i++){
for(inti=0;i<theMessage.length();i++){//Iterates through the plaintext string.
charcurrent=theMessage.charAt(i);
charcurrent=theMessage.charAt(i);//Gets the character at the current index.
charnewChar=enryptedCharacter(current,theOffset);
charnewChar=enryptedCharacter(current,theOffset);//Calls the encryptedCharacter function to calculate and return the encrypted character.
message=message+newChar;
message=message+newChar;//Adds the new encrypted character to the cipher text string.