Commit 615a4fa7 authored by jackt's avatar jackt

Final draft

parent cccc02bd
public class AssessmentPartThree { public class AssessmentPartThree {
// The simplest form of encryption is the rotation cipher (also known as Caeser's Cipher) // The simplest form of encryption is the rotation cipher (also known as
// An offset value is chosen to encrypt a message. Each letter is replaced by the // 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. // 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 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 // - so a becomes b, b becomes c, etc
// If a value of -2 is chosen a becomes y, b becomes z and so on // If a value of -2 is chosen a becomes y, b becomes z and so on
public char enryptedCharacter(char theChar, int theOffset) public char enryptedCharacter(char theChar, int theOffset) {
{ // 05 - encryptedCharacter
// 05 - encryptedCharacter // Complete this method so that it returns the encrypted character for
// Complete this method so that it returns the encrypted character for // theChar when and offset of theOffset is used
// theChar when and offset of theOffset is used // 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
int ascii = (int) theChar; // converts password char to ascii code to ensure it is part of the allowed
int ascii = (int)theChar; //converts password char to ascii code to ensure it is part of the allowed character range // character range
int offsetVal = 0; int offsetVal = 0;
if (ascii >= 65 && ascii <= 90 || ascii >= 97 && ascii <= 122) // deduces if the character is between a-z or A-Z
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
{ {
if ((theOffset < 0 && (ascii <= 65 - theOffset) && (ascii >= 65 - theOffset)) offsetVal = ascii - 26 + theOffset; //determines the offset value to use
|| (theOffset < 0 && (ascii <= 97 - theOffset) && (ascii >= 97 - theOffset))) // if the offset is negative, this accounts for the wraparound theChar = (char) offsetVal;
{ return theChar;
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 if ((theOffset < 0 && !(ascii <= 65 - theOffset && ascii >= 65 - theOffset) //for each if statement, if the character falls between the range, then the offset value is subtracted
{ || (theOffset < 0 && !(ascii <= 97 - theOffset && ascii >= 97 - theOffset)))) { // inclusive to a wrap around (this goes for the following 3 if statements)
return theChar; offsetVal = ascii + theOffset;
} theChar = (char) offsetVal;
return theChar; return theChar;
} }
public String encryptedString(String theMessage, int theOffset) if ((theOffset > 0 && ascii >= 90 - theOffset && ascii <= 90 - theOffset)
{ || (theOffset > 0 && ascii >= 122 - theOffset && ascii <= 122 - theOffset)) {
// 06 - encryptedMessage offsetVal = ascii - 26 + theOffset;
// Complete this method so that it uses encryptedCharacter to theChar = (char) offsetVal;
// return the encrypted version of theMessage using theOffset return theChar;
String message = ""; }
for (int length = 0; length < theMessage.length(); length++) // for loop to count up through the word length if ((theOffset > 0 && !(ascii >= 90 - theOffset && ascii <= 90 - theOffset)
{ || (theOffset > 0 && !(ascii >= 122 - theOffset && ascii <= 122 - theOffset)))) {
char theChar = theMessage.charAt(length); // selects the letter at the current char placement offsetVal = ascii + theOffset;
theChar = (char) offsetVal;
return theChar;
}
}
else {
return theChar; //the character is returned if it is not within a-z
}
return theChar; // the encrypted answer is returned
}
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 message = "";
int a = theMessage.length();
for (int length = 0; length < a; 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); String space = Character.toString(theChar);
if (space.equals(" ")) a = theMessage.length();
{
message = message + theChar; if (space.equals(" ")) {
continue; 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 ascii = (int) theChar; // converts password char to ascii code to ensure it is part of the allowed
// character range
int offsetVal = 0; int offsetVal = 0;
if (ascii >= 65 && ascii <= 90 || ascii >= 97 && ascii <= 122) // deduces if the character is between a-z or
if (ascii >= 65 && ascii <= 90 || ascii >= 97 && ascii <=122) // deduces if the character is between a-z or A-Z // A-Z
{ {
if ((theOffset < 0 && (ascii < 65 - theOffset) && (ascii >= 65)) 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 || (theOffset < 0 && (ascii < 97 - theOffset) && (ascii >= 97))) // if the offset is negative,
// this accounts for the
// wraparound
{ {
offsetVal = ascii + 26 + theOffset; offsetVal = ascii + 26 + theOffset;
theChar = (char)offsetVal; theChar = (char) offsetVal;
message = message + theChar; message = message + theChar;
continue; continue;
} }
if ((theOffset < 0 && !(ascii < 65 - theOffset && ascii >= 65) if ((theOffset < 0 && !(ascii < 65 - theOffset && ascii >= 65)
||(theOffset < 0 && !(ascii < 97 - theOffset && ascii >= 97)))) || (theOffset < 0 && !(ascii < 97 - theOffset && ascii >= 97)))) {
{
offsetVal = ascii + theOffset; offsetVal = ascii + theOffset;
theChar = (char)offsetVal; theChar = (char) offsetVal;
message = message + theChar; message = message + theChar;
continue; continue;
} }
if ((theOffset > 0 && ascii > 90 - theOffset && ascii <= 90) if ((theOffset > 0 && ascii > 90 - theOffset && ascii <= 90)
||(theOffset > 0 && ascii > 122 - theOffset && ascii <= 122)) || (theOffset > 0 && ascii > 122 - theOffset && ascii <= 122)) {
{
offsetVal = ascii - 26 + theOffset; offsetVal = ascii - 26 + theOffset;
theChar = (char)offsetVal; theChar = (char) offsetVal;
message = message + theChar; message = message + theChar;
continue; continue;
} }
if ((theOffset > 0 && !(ascii > 90 - theOffset && ascii <= 90) if ((theOffset > 0 && !(ascii > 90 - theOffset && ascii <= 90)
||(theOffset > 0 && ! (ascii > 122 - theOffset && ascii <= 122)))) || (theOffset > 0 && !(ascii > 122 - theOffset && ascii <= 122)))) {
{
offsetVal = ascii + theOffset; offsetVal = ascii + theOffset;
theChar = (char)offsetVal; theChar = (char) offsetVal;
message = message + theChar; message = message + theChar;
continue; continue;
} }
if (!(ascii > 64 && ascii < 91) || !(ascii > 96 && ascii < 123)) if (!(ascii > 64 && ascii < 91) || !(ascii > 96 && ascii < 123)) {
{ message = message + theChar;
message = message + theChar; continue;
continue;
} }
theMessage = message; theMessage = message;
}
} }
return message;
} }
return 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