Commit a401a4a1 authored by User's avatar User

This has been uploaded to gitlab

parent e56f0d16
...@@ -8,8 +8,6 @@ public class AssessmentPartThree { ...@@ -8,8 +8,6 @@ public class AssessmentPartThree {
// - 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)
{
// 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
...@@ -17,16 +15,102 @@ public class AssessmentPartThree { ...@@ -17,16 +15,102 @@ public class AssessmentPartThree {
// 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'; public char enryptedCharacter(char theChar, int theOffset)
{
if(theOffset> theChar) // it is comparing whether theOffset is greater than theChar
{
theOffset = theOffset%theChar;// theOffset is dividing by theChar until it can no longer divide any number.
}
else if(theOffset< theChar) // Compares if theOffset is less than theChar
{
theOffset = (theOffset%theChar)+theChar; // It will also divide by theChar and add theChar as well.
}
for(int w=0; w<theChar; w++) { // goes through the loop
char morse = theChar; // theChar is passed on to the char morse.
morse = (char)(morse + theOffset); //the char morse combines the morse and theOffset
if(morse < 'z') { // if the morse is less than z, it will shift the alphabet.
morse = (char)(morse + 26); // by adding the 26 to morse, this is allowing morse to shift the alphabet to the right.
} }
else
{
if(morse > 'a') { // if the morse is greater than a, it will shift the alphabet.
morse = (char) (morse - 26); // by subtracting 26 from morse, this is allowing morse to shift to the left.
}
}
morse = theChar; // the morse is passed to theChar.
}
return (char) theOffset; // it will return the char theOffset.
}
public String encryptedString(String theMessage, int theOffset) public String encryptedString(String theMessage, int theOffset)
{ {
// 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
return "Encryptred message"; if(theOffset> theMessage.length()) // compares if the message is greater than theOffset.
{
theOffset = theOffset%theMessage.length(); // it will constantly divide theOffset with the String in order to get a number that cannot be divided by theMessage.
}
else if(theOffset< theMessage.length()) // this selection statement compares if theOffset is less than the String theMessage
{
theOffset = (theOffset%theMessage.length())+theMessage.length(); // it will also divide theOffset with theMessage with the String but it will add the String as well.
//The reason for adding the String is that, theOffset is less than theMessage and in order for the program to work, theMessage is added to theOffset to make it a fair number to be used in the program.
}
for(int w=0; w<theMessage.length(); w++) { // loops through the String
char morse = (char) theMessage.length(); // the String is passed to the char called morse
morse = (char)(morse + theOffset); // the char morse adds itself with theOffset
if(morse > 'Z') { // if morse is less than the capital Z, it will shift the alphabet to the left.
morse = (char)(morse - 26); // During the shifting process, it will minus 26 as it will shift to the left.
}
else
{
if(morse < 'A') { // if morse is greater than capital A, it will shift the alphabet to the right.
morse = (char) (morse + 26); // During the shifting process, it will add 26 as it will shift to the right.
}
}
morse = (char) theMessage.length(); // the char morse get passed to the String.
}
return theMessage; // it will return the String theMessage.
} }
} }
...@@ -26,7 +26,9 @@ class AssessmentPartThreeTest { ...@@ -26,7 +26,9 @@ class AssessmentPartThreeTest {
"T,-7,M" "T,-7,M"
}) })
void testEnryptedCharacter(char theChar, int theOffset, char encChar) { void testEnryptedCharacter(char theChar, int theOffset, char encChar) {
System.out.println("The encrypted message is " + theChar + theOffset + encChar);
assertEquals(encChar,test.enryptedCharacter(theChar, theOffset)); assertEquals(encChar,test.enryptedCharacter(theChar, theOffset));
} }
@ParameterizedTest @ParameterizedTest
...@@ -38,6 +40,7 @@ class AssessmentPartThreeTest { ...@@ -38,6 +40,7 @@ class AssessmentPartThreeTest {
"ktixevzout,-6,encryption" "ktixevzout,-6,encryption"
}) })
void testEncryptedString(String theMessage, int theOffset, String encMessage) { void testEncryptedString(String theMessage, int theOffset, String encMessage) {
System.out.println("The decoded message reads " + theMessage + theOffset + encMessage );
assertEquals(encMessage, test.encryptedString(theMessage, theOffset)); assertEquals(encMessage, test.encryptedString(theMessage, theOffset));
} }
......
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