Commit 714c33f2 authored by daniel.blaho's avatar daniel.blaho

First blood. Added all files.

parent 6f2443e9
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Assessment03-18</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
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
char resultChar = 'e'; // Declared a char called resultChar to be used later.
for(int i = 0; i < theChar; i++){ // I used a for loop to iterate through the passed characters.
char letter = (char)(theChar + theOffset); // Declared a char called letter while using type casting
// to convert it to char which is usable with the passed through values of theChar and theOffset.
// Basically, to be able to add the char and int together both are converted.
if (Character.isUpperCase(i)) { // This if loop handles the conversion if the character is uppercase.
if (letter > 'Z') { // This if statement runs if the character letter is bigger than the letter Z.
resultChar = (char)(theChar - (26-theOffset)); // Within last brackets, the position of the wanted
// character is found by taking away theOffset from the length of the alphabet, which is taken away
// from the passed theChar to find the position of the result character.
}
else if (letter < 'A') { // // This else if statement runs if the character letter is smaller than the letter A.
resultChar = (char)(theChar + (26+theOffset)); // Just like above, but runs the other way.
}
}
if (Character.isLowerCase(i)) { // This if loop handles the conversion if the character is lowercase.
if (letter > 'z') {
resultChar = (char)(theChar - (26-theOffset));
}
else if (letter < 'a') {
resultChar = (char)(theChar + (26+theOffset));
}
}
else { // If none of the above is applicable
resultChar = (char)(theChar + theOffset); // The result is
}
if (Character.isDigit(i)) { // If the character passed through is a digit and not a letter,
resultChar = theChar; // the result is unchanged and returns unchanged.
}
}
return resultChar; // Prints the final result.
}
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 resultMessage = ""; // Initiates a string for the final result.
for(int x = 0; x < theMessage.length(); x++){ // I used a for loop to iterate through the passed string.
char letter = (char)(theMessage.charAt(x) + theOffset); // I did this by checking through character by character.
if (letter > 'z') {
resultMessage += (char)(theMessage.charAt(x) - (26-theOffset)); // This uses the same code as encryptedCharacter,
// Except for checking through the passed string theMessage.
}
else if (letter < 'a') {
resultMessage += (char)(theMessage.charAt(x) + (26+theOffset));
}
else {
resultMessage += (char)(theMessage.charAt(x) + theOffset);
}
}
return resultMessage; // Returns final result.
}
// Couldn't figure out how to pass encryptedCharacter to be able to return the correct answer, or how to
// check and figure out a solution to the space in the second test.
}
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
class AssessmentPartThreeTest {
public static AssessmentPartThree test;
@BeforeAll
static void setUpBeforeClass() throws Exception {
test = new AssessmentPartThree();
}
@ParameterizedTest
@DisplayName("Testing encryptedCharacter")
@CsvSource({
"a,5,f",
"q,-3,n",
"G,6,M",
"5,-3,5",
"T,-7,M"
})
void testEnryptedCharacter(char theChar, int theOffset, char encChar) {
assertEquals(encChar,test.enryptedCharacter(theChar, theOffset));
}
@ParameterizedTest
@DisplayName("Testing encryptedString")
@CsvSource({
"hello,5,mjqqt",
"Java Coding,-3,Gxsx Zlafkd",
"dandelion,8,livlmtqwv",
"ktixevzout,-6,encryption"
})
void testEncryptedString(String theMessage, int theOffset, String encMessage) {
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