Commit 8d3e9f2f authored by jackt's avatar jackt

First Draft

parent e56f0d16
...@@ -16,8 +16,52 @@ public class AssessmentPartThree { ...@@ -16,8 +16,52 @@ public class AssessmentPartThree {
// So if theChar='m' and theOffset is 3 the method will return 'p' // So if theChar='m' and theOffset is 3 the method will return 'p'
// Lower case characters remain lower case, upper case remain upper case // Lower case characters remain lower case, upper case remain upper case
// Any other characters are returned unchanged // Any other characters are returned unchanged
return 'a'; int ascii = (int)theChar; //converts password char to ascii code to ensure it is part of the allowed character range
int offsetVal = 0;
if (ascii >= 65 && ascii <= 90 || ascii >= 97 && ascii <=122) // deduces if the character is between a-z or A-Z
{
if ((theOffset < 0 && (ascii <= 65 - theOffset) && (ascii >= 65 - theOffset))
|| (theOffset < 0 && (ascii <= 97 - theOffset) && (ascii >= 97 - theOffset))) // if the offset is negative, this accounts for the wraparound
{
offsetVal = ascii - 26 + theOffset;
theChar = (char)offsetVal;
return theChar;
}
if ((theOffset < 0 && !(ascii <= 65 - theOffset && ascii >= 65 - theOffset)
||(theOffset < 0 && !(ascii <= 97 - theOffset && ascii >= 97 - theOffset))))
{
offsetVal = ascii + theOffset;
theChar = (char)offsetVal;
return theChar;
}
if ((theOffset > 0 && ascii >= 90 - theOffset && ascii <= 90 - theOffset)
||(theOffset > 0 && ascii >= 122 - theOffset && ascii <= 122 - theOffset))
{
offsetVal = ascii - 26 + theOffset;
theChar = (char)offsetVal;
return theChar;
}
if ((theOffset > 0 && !(ascii >= 90 - theOffset && ascii <= 90 - theOffset)
||(theOffset > 0 && ! (ascii >= 122 - theOffset && ascii <= 122 - theOffset))))
{
offsetVal = ascii + theOffset;
theChar = (char)offsetVal;
return theChar;
}
}
else
{
return theChar;
}
return theChar;
} }
public String encryptedString(String theMessage, int theOffset) public String encryptedString(String theMessage, int theOffset)
...@@ -25,8 +69,72 @@ public class AssessmentPartThree { ...@@ -25,8 +69,72 @@ 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
String message = "";
for (int length = 0; length < theMessage.length(); length++) // for loop to count up through the word length
{
char theChar = theMessage.charAt(length); // selects the letter at the current char placement
String space = Character.toString(theChar);
if (space.equals(" "))
{
message = message + theChar;
continue;
}
int ascii = (int)theChar; //converts password char to ascii code to ensure it is part of the allowed character range
int offsetVal = 0;
if (ascii >= 65 && ascii <= 90 || ascii >= 97 && ascii <=122) // deduces if the character is between a-z or A-Z
{
if ((theOffset < 0 && (ascii < 65 - theOffset) && (ascii >= 65))
|| (theOffset < 0 && (ascii < 97 - theOffset) && (ascii >= 97))) // if the offset is negative, this accounts for the wraparound
{
offsetVal = ascii + 26 + theOffset;
theChar = (char)offsetVal;
message = message + theChar;
continue;
}
if ((theOffset < 0 && !(ascii < 65 - theOffset && ascii >= 65)
||(theOffset < 0 && !(ascii < 97 - theOffset && ascii >= 97))))
{
offsetVal = ascii + theOffset;
theChar = (char)offsetVal;
message = message + theChar;
continue;
}
if ((theOffset > 0 && ascii > 90 - theOffset && ascii <= 90)
||(theOffset > 0 && ascii > 122 - theOffset && ascii <= 122))
{
offsetVal = ascii - 26 + theOffset;
theChar = (char)offsetVal;
message = message + theChar;
continue;
}
if ((theOffset > 0 && !(ascii > 90 - theOffset && ascii <= 90)
||(theOffset > 0 && ! (ascii > 122 - theOffset && ascii <= 122))))
{
offsetVal = ascii + theOffset;
theChar = (char)offsetVal;
message = message + theChar;
continue;
}
if (!(ascii > 64 && ascii < 91) || !(ascii > 96 && ascii < 123))
{
message = message + theChar;
continue;
}
theMessage = message;
}
}
return message;
}
return "Encryptred message";
} }
} \ No newline at end of file
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