Added options to leave whitespace and capitals
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Caesar.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 07-25-21
|
||||
//Modified: 12-25-21
|
||||
//This is the declaration of the Caesar class
|
||||
package mattrixwv.CipherStreamJava;
|
||||
|
||||
|
||||
public class Caesar{
|
||||
public static final String version = "1.0"; //The current version number for the library
|
||||
private String inputString; //The string that needs encoded/decoded
|
||||
private String outputString; //The encoded/decoded string
|
||||
private int shift; //The amount that you need to shift each letter
|
||||
private boolean leaveCapitals; //Whether to respect capitals in the output string
|
||||
private boolean leaveWhitespace; //Whether to respect whitespace in the output string
|
||||
//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
|
||||
@@ -18,6 +19,12 @@ public class Caesar{
|
||||
}
|
||||
//Sets the input string
|
||||
private void setInputString(String inputString){
|
||||
if(!leaveCapitals){
|
||||
inputString = inputString.toLowerCase();
|
||||
}
|
||||
if(!leaveWhitespace){
|
||||
inputString = inputString.replaceAll("\\s+", "");
|
||||
}
|
||||
this.inputString = inputString;
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
@@ -92,6 +99,13 @@ public class Caesar{
|
||||
//Constructor
|
||||
public Caesar(){
|
||||
reset();
|
||||
leaveCapitals = false;
|
||||
leaveWhitespace = false;
|
||||
}
|
||||
public Caesar(boolean leaveCapitals, boolean leaveWhitespace){
|
||||
reset();
|
||||
this.leaveCapitals = leaveCapitals;
|
||||
this.leaveWhitespace = leaveWhitespace;
|
||||
}
|
||||
//Returns the inputString
|
||||
public String getInputString(){
|
||||
@@ -105,6 +119,20 @@ public class Caesar{
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Returns if capitals should be respected in the output
|
||||
public boolean getLeaveCapitals(){
|
||||
return leaveCapitals;
|
||||
}
|
||||
public void setLeaveCapitals(boolean leaveCapitals){
|
||||
this.leaveCapitals = leaveCapitals;
|
||||
}
|
||||
//Returns if whitespace should be respected in the output
|
||||
public boolean getLeaveWhitespace(){
|
||||
return leaveWhitespace;
|
||||
}
|
||||
public void setLeaveWhitespace(boolean leaveWhitespace){
|
||||
this.leaveWhitespace = leaveWhitespace;
|
||||
}
|
||||
//Sets the shift and inputString and encodes the message
|
||||
public String encode(int shiftAmount, String inputString){
|
||||
reset();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/TestCaesar.java
|
||||
//CipherStreamJava/src/test/java/mattrixwv/CipherStreamJava/TestCaesar.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 07-25-21
|
||||
//Modified: 12-25-21
|
||||
//These are the tests for the Caesar class
|
||||
package mattrixwv.CipherStreamJava;
|
||||
|
||||
@@ -14,7 +14,7 @@ import org.junit.Test;
|
||||
public class TestCaesar{
|
||||
@Test
|
||||
public void testDecode(){
|
||||
Caesar cipher = new Caesar();
|
||||
Caesar cipher = new Caesar(true, true);
|
||||
|
||||
//Test 1
|
||||
String input = "def";
|
||||
@@ -28,24 +28,139 @@ public class TestCaesar{
|
||||
correctOutput = "abc";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the second test", correctOutput, output);
|
||||
|
||||
//Test 3
|
||||
input = "DEF";
|
||||
shift = 3;
|
||||
correctOutput = "ABC";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the third test", correctOutput, output);
|
||||
|
||||
//Test 4
|
||||
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", correctOutput, output);
|
||||
//Test 4
|
||||
assertEquals("Caesar Decoding failed the fourth test", correctOutput, output);
|
||||
//Test 5
|
||||
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", correctOutput, output);
|
||||
assertEquals("Caesar Decoding failed the fifth test", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testNoWhitespaceDecode(){
|
||||
Caesar cipher = new Caesar(true, false);
|
||||
|
||||
//Test 1
|
||||
String input = "def";
|
||||
int shift = 3;
|
||||
String correctOutput = "abc";
|
||||
String output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the first test", correctOutput, output);
|
||||
//Test 2
|
||||
input = "def";
|
||||
shift = 29;
|
||||
correctOutput = "abc";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the second test", correctOutput, output);
|
||||
//Test 3
|
||||
input = "DEF";
|
||||
shift = 3;
|
||||
correctOutput = "ABC";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the third test", correctOutput, output);
|
||||
|
||||
//Test 4
|
||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
||||
shift = -3;
|
||||
correctOutput = "Thequickbrownfoxjumpsover-thelazydog";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the fourth test", correctOutput, output);
|
||||
//Test 5
|
||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
||||
shift = 23;
|
||||
correctOutput = "Thequickbrownfoxjumpsover-thelazydog";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the fifth test", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testNoCapitalDecode(){
|
||||
Caesar cipher = new Caesar(false, true);
|
||||
|
||||
//Test 1
|
||||
String input = "def";
|
||||
int shift = 3;
|
||||
String correctOutput = "abc";
|
||||
String output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the first test", correctOutput, output);
|
||||
//Test 2
|
||||
input = "def";
|
||||
shift = 29;
|
||||
correctOutput = "abc";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the second test", correctOutput, output);
|
||||
//Test 3
|
||||
input = "DEF";
|
||||
shift = 3;
|
||||
correctOutput = "abc";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the third test", correctOutput, output);
|
||||
|
||||
//Test 4
|
||||
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 fourth test", correctOutput, output);
|
||||
//Test 5
|
||||
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 fifth test", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testNoCapitalWhitespaceDecode(){
|
||||
Caesar cipher = new Caesar(false, false);
|
||||
|
||||
//Test 1
|
||||
String input = "def";
|
||||
int shift = 3;
|
||||
String correctOutput = "abc";
|
||||
String output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the first test", correctOutput, output);
|
||||
//Test 2
|
||||
input = "def";
|
||||
shift = 29;
|
||||
correctOutput = "abc";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the second test", correctOutput, output);
|
||||
//Test 3
|
||||
input = "DEF";
|
||||
shift = 3;
|
||||
correctOutput = "abc";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the third test", correctOutput, output);
|
||||
|
||||
//Test 4
|
||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
||||
shift = -3;
|
||||
correctOutput = "thequickbrownfoxjumpsover-thelazydog";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the fourth test", correctOutput, output);
|
||||
//Test 5
|
||||
input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
|
||||
shift = 23;
|
||||
correctOutput = "thequickbrownfoxjumpsover-thelazydog";
|
||||
output = cipher.decode(shift, input);
|
||||
assertEquals("Caesar Decoding failed the fifth test", correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncode(){
|
||||
//Test 1
|
||||
Caesar cipher = new Caesar();
|
||||
Caesar cipher = new Caesar(true, true);
|
||||
String input = "abc";
|
||||
int shift = 3;
|
||||
String correctOutput = "def";
|
||||
@@ -56,19 +171,130 @@ public class TestCaesar{
|
||||
shift = 29;
|
||||
correctOutput = "def";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the second test - Expected: " + correctOutput + "; actual" + output, correctOutput, output);
|
||||
|
||||
assertEquals("Caesar Encoding failed the second test", correctOutput, output);
|
||||
//Test 3
|
||||
input = "ABC";
|
||||
shift = 3;
|
||||
correctOutput = "DEF";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Ceasar Encoding failed the third test", correctOutput, output);
|
||||
|
||||
//Test 4
|
||||
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", correctOutput, output);
|
||||
//Test 4
|
||||
assertEquals("Caesar Encoding failed the fourth test", correctOutput, output);
|
||||
//Test 5
|
||||
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", correctOutput, output);
|
||||
assertEquals("Caesar Encoding failed the fifth test", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testNoWhitespaceEncode(){
|
||||
//Test 1
|
||||
Caesar cipher = new Caesar(true, false);
|
||||
String input = "abc";
|
||||
int shift = 3;
|
||||
String correctOutput = "def";
|
||||
String output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the first test", correctOutput, output);
|
||||
//Test 2
|
||||
input = "abc";
|
||||
shift = 29;
|
||||
correctOutput = "def";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the second test", correctOutput, output);
|
||||
//Test 3
|
||||
input = "ABC";
|
||||
shift = 3;
|
||||
correctOutput = "DEF";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Ceasar Encoding failed the third test", correctOutput, output);
|
||||
|
||||
//Test 4
|
||||
input = "The quick brown fox jumps over - the lazy dog";
|
||||
shift = -3;
|
||||
correctOutput = "Qebnrfzhyoltkclugrjmplsbo-qebixwvald";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the fourth test", correctOutput, output);
|
||||
//Test 5
|
||||
input = "The quick brown fox jumps over - the lazy dog";
|
||||
shift = 23;
|
||||
correctOutput = "Qebnrfzhyoltkclugrjmplsbo-qebixwvald";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the fifth test", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testNoCapitalEncode(){
|
||||
//Test 1
|
||||
Caesar cipher = new Caesar(false, true);
|
||||
String input = "abc";
|
||||
int shift = 3;
|
||||
String correctOutput = "def";
|
||||
String output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the first test", correctOutput, output);
|
||||
//Test 2
|
||||
input = "abc";
|
||||
shift = 29;
|
||||
correctOutput = "def";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the second test", correctOutput, output);
|
||||
//Test 3
|
||||
input = "ABC";
|
||||
shift = 3;
|
||||
correctOutput = "def";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Ceasar Encoding failed the third test", correctOutput, output);
|
||||
|
||||
//Test 4
|
||||
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 fourth test", correctOutput, output);
|
||||
//Test 5
|
||||
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 fifth test", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testNoCapitalWhitespaceEncode(){
|
||||
//Test 1
|
||||
Caesar cipher = new Caesar(false, false);
|
||||
String input = "abc";
|
||||
int shift = 3;
|
||||
String correctOutput = "def";
|
||||
String output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the first test", correctOutput, output);
|
||||
//Test 2
|
||||
input = "abc";
|
||||
shift = 29;
|
||||
correctOutput = "def";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the second test", correctOutput, output);
|
||||
//Test 3
|
||||
input = "ABC";
|
||||
shift = 3;
|
||||
correctOutput = "def";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Ceasar Encoding failed the third test", correctOutput, output);
|
||||
|
||||
//Test 4
|
||||
input = "The quick brown fox jumps over - the lazy dog";
|
||||
shift = -3;
|
||||
correctOutput = "qebnrfzhyoltkclugrjmplsbo-qebixwvald";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the fourth test", correctOutput, output);
|
||||
//Test 5
|
||||
input = "The quick brown fox jumps over - the lazy dog";
|
||||
shift = 23;
|
||||
correctOutput = "qebnrfzhyoltkclugrjmplsbo-qebixwvald";
|
||||
output = cipher.encode(shift, input);
|
||||
assertEquals("Caesar Encoding failed the fifth test", correctOutput, output);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user