Commit 6773ebee authored by tomun's avatar tomun

part three

parent 2378f3da
public class AssessmentPartThree {
// The simplest form of encryption is the rotation cipher (also known as Caeser's Cipher)
// An offset value is chosen to encrypt a message. Each letter is replaced by the
// letter that that number of places away from it.
// So if an offset value of 1 is chosen, each letter is replaced by the one after it
// - so a becomes b, b becomes c, etc
// If a value of -2 is chosen a becomes y, b becomes z and so on
public char enryptedCharacter(char theChar, int theOffSet)
{
// 05 - encryptedCharacter
// Complete this method so that it returns the encrypted character for
// theChar when and offset of theOffset is used
// So if theChar='m' and theOffset is 3 the method will return 'p'
// Lower case characters remain lower case, upper case remain upper case
// Any other characters are returned unchanged
int charNum = (int) theChar;
if((65 <= charNum && charNum <= 90))
{
if(theOffSet >0) {
for(int i = theOffSet; i !=0; i--)
{
charNum = charNum +1;
if(charNum >90)
{
charNum = 65;
}
}
theChar = (char)charNum;
return theChar;
}
else
{
for(int i = theOffSet; i !=0; i++)
{
charNum = charNum -1;
if(charNum < 65)
{
charNum = 90;
}
}
theChar = (char)charNum;
return theChar;
}
}
else if(97 <= charNum && charNum <= 122)
{
if(theOffSet >0) {
for(int i = theOffSet; i !=0; i--)
{
charNum = charNum +1;
if(charNum >122)
{
charNum = 97;
}
}
theChar = (char)charNum;
return theChar;
}
else
{
for(int i = theOffSet; i !=0; i++)
{
charNum = charNum -1;
if(charNum < 97)
{
charNum = 122;
}
}
theChar = (char)charNum;
return theChar;
}
}
else
{
return theChar;
}
}
public String encryptedString(String theMessage, int theOffSet)
{
// 06 - encryptedMessage
// Complete this method so that it uses encryptedCharacter to
// return the encrypted version of theMessage using theOffset
String encryptedMessage = "";
for(int i = 0; i < theMessage.length(); i++)
{
encryptedMessage += enryptedCharacter(theMessage.charAt(i), theOffSet);
}
return encryptedMessage;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment