Caesar cipher is implemented
This commit is contained in:
121
src/main/java/mattrixwv/CipherStreamJava/Caesar.java
Normal file
121
src/main/java/mattrixwv/CipherStreamJava/Caesar.java
Normal file
@@ -0,0 +1,121 @@
|
||||
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Caesar.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 07-25-21
|
||||
//This is the declaration of the Caesar class
|
||||
package mattrixwv.CipherStreamJava;
|
||||
|
||||
|
||||
public class Caesar{
|
||||
private String inputString; //The string that needs encoded/decoded
|
||||
private String outputString; //The encoded/decoded string
|
||||
public static final String version = "1.0"; //The current version number for the library
|
||||
private int shift; //The amount that you need to shift each letter
|
||||
//Sets shift and makes sure it is within the propper bounds
|
||||
private void setShift(int shiftAmount){
|
||||
//If you shift more than 26 you will just be wrapping back around again
|
||||
shift = shiftAmount % 26;
|
||||
}
|
||||
//Sets the input string
|
||||
private void setInputString(String inputString){
|
||||
this.inputString = inputString;
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
private String encode(){
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
char temp = inputString.charAt(cnt); //A temperary holder for the current working character
|
||||
//If it is an upper case letter shift it and wrap if necessary
|
||||
if(Character.isUpperCase(temp)){
|
||||
temp += shift;
|
||||
//Wrap around if the letter is now out of bounds
|
||||
if(temp < 'A'){
|
||||
temp += 26;
|
||||
}
|
||||
else if(temp > 'Z'){
|
||||
temp -= 26;
|
||||
}
|
||||
}
|
||||
//If it is a lower case letter shift it and wrap if necessary
|
||||
else if(Character.isLowerCase(temp)){
|
||||
temp += shift;
|
||||
//Wrap around if the letter is now out of bounds
|
||||
if(temp < 'a'){
|
||||
temp += 26;
|
||||
}
|
||||
else if(temp > 'z'){
|
||||
temp -= 26;
|
||||
}
|
||||
}
|
||||
//If it is whitespace, number, or punctuation just let it pass through
|
||||
//Add it to the output string
|
||||
outputString += temp;
|
||||
}
|
||||
return outputString;
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
private String decode(){
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
char temp = inputString.charAt(cnt); //A temperary holder for the current working character
|
||||
//If it is an upper case letter shift it and wrap if necessary
|
||||
if(Character.isUpperCase(temp)){
|
||||
temp -= shift;
|
||||
if(temp < 'A'){
|
||||
temp += 26;
|
||||
}
|
||||
else if(temp > 'Z'){
|
||||
temp -= 26;
|
||||
}
|
||||
}
|
||||
//If it is a lower case letter shift it and wrap if necessary
|
||||
else if(Character.isLowerCase(temp)){
|
||||
temp -= shift;
|
||||
if(temp < 'a'){
|
||||
temp += 26;
|
||||
}
|
||||
else if(temp > 'z'){
|
||||
temp -= 26;
|
||||
}
|
||||
}
|
||||
//If it is whitespace, number, or punctuation just let it pass through
|
||||
//Add it to the output string
|
||||
outputString += temp;
|
||||
}
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Constructor
|
||||
public Caesar(){
|
||||
reset();
|
||||
}
|
||||
//Returns the inputString
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
//Returns shift
|
||||
public int getShift(){
|
||||
return shift;
|
||||
}
|
||||
//Returns the outputString
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Sets the shift and inputString and encodes the message
|
||||
public String encode(int shiftAmount, String inputString){
|
||||
reset();
|
||||
setShift(shiftAmount);
|
||||
setInputString(inputString);
|
||||
return encode();
|
||||
}
|
||||
//Sets the shift and inputString and decodes the message
|
||||
public String decode(int shiftAmount, String inputString){
|
||||
reset();
|
||||
setShift(shiftAmount);
|
||||
setInputString(inputString);
|
||||
return decode();
|
||||
}
|
||||
//Makes sure all of the variables are empty
|
||||
public void reset(){
|
||||
inputString = outputString = "";
|
||||
shift = 0;
|
||||
}
|
||||
}
|
||||
73
src/test/java/mattrixwv/CipherStreamJava/TestCaesar.java
Normal file
73
src/test/java/mattrixwv/CipherStreamJava/TestCaesar.java
Normal file
@@ -0,0 +1,73 @@
|
||||
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/TestCaesar.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 07-25-21
|
||||
//These are the tests for the Caesar class
|
||||
package mattrixwv.CipherStreamJava;
|
||||
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class TestCaesar{
|
||||
@Test
|
||||
public void testDecode(){
|
||||
//Test 1
|
||||
Caesar cipher = new Caesar();
|
||||
String input = "def";
|
||||
int shift = 3;
|
||||
String correctOutput = "abc";
|
||||
String output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the first test - Expected: " + correctOutput + "; actual: " + output, correctOutput, output);
|
||||
//Test 2
|
||||
input = "def";
|
||||
shift = 29;
|
||||
correctOutput = "abc";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the second test - Expected: " + correctOutput + "; actual: " + output, correctOutput, output);
|
||||
|
||||
//Test 3
|
||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
||||
shift = -3;
|
||||
correctOutput = "The quick brown fox jumps over - the lazy dog";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the third test - Expected: " + correctOutput + "; actual: " + output, correctOutput, output);
|
||||
//Test 4
|
||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
||||
shift = 23;
|
||||
correctOutput = "The quick brown fox jumps over - the lazy dog";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the fourth test - Expected: " + correctOutput + "; actual: " + output, correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testEncode(){
|
||||
//Test 1
|
||||
Caesar cipher = new Caesar();
|
||||
String input = "abc";
|
||||
int shift = 3;
|
||||
String correctOutput = "def";
|
||||
String output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the first test - Expected: " + correctOutput + "; actual: " + output, correctOutput, output);
|
||||
//Test 2
|
||||
input = "abc";
|
||||
shift = 29;
|
||||
correctOutput = "def";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the second test - Expected: " + correctOutput + "; actual" + output, correctOutput, output);
|
||||
|
||||
//Test 3
|
||||
input = "The quick brown fox jumps over - the lazy dog";
|
||||
shift = -3;
|
||||
correctOutput = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the third test - Expected: " + correctOutput + "; actual: " + output, correctOutput, output);
|
||||
//Test 4
|
||||
input = "The quick brown fox jumps over - the lazy dog";
|
||||
shift = 23;
|
||||
correctOutput = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the third test - Expected: " + correctOutput + "; actual: " + output, correctOutput, output);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user