Started using stringbuilder for efficiency
This commit is contained in:
@@ -12,20 +12,24 @@ public class Atbash{
|
||||
private String outputString; //Holds teh current version of the library
|
||||
//Decodes inputString and stores in outputString
|
||||
private String decode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
//Stop through every element in the inputString and shift it the correct amount
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
outputString += (char)(inputString.charAt(cnt) + 25 - (2 * (inputString.charAt(cnt) - 'A')));
|
||||
output.append((char)(inputString.charAt(cnt) + 25 - (2 * (inputString.charAt(cnt) - 'A'))));
|
||||
}
|
||||
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
//Encodes inputString and stores in outputString
|
||||
private String encode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
//Step through every element in the inputString and shift it the correct amount
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
outputString += (char)(inputString.charAt(cnt) + 25 - (2 * (inputString.charAt(cnt) - 'A')));
|
||||
output.append((char)(inputString.charAt(cnt) + 25 - (2 * (inputString.charAt(cnt) - 'A'))));
|
||||
}
|
||||
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
//Removes all invalid characters and sets inputString
|
||||
|
||||
@@ -22,64 +22,70 @@ public class Caesar{
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
private String encode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
char temp = inputString.charAt(cnt); //A temperary holder for the current working character
|
||||
char currentChar = 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(Character.isUpperCase(currentChar)){
|
||||
currentChar += shift;
|
||||
//Wrap around if the letter is now out of bounds
|
||||
if(temp < 'A'){
|
||||
temp += 26;
|
||||
if(currentChar < 'A'){
|
||||
currentChar += 26;
|
||||
}
|
||||
else if(temp > 'Z'){
|
||||
temp -= 26;
|
||||
else if(currentChar > 'Z'){
|
||||
currentChar -= 26;
|
||||
}
|
||||
}
|
||||
//If it is a lower case letter shift it and wrap if necessary
|
||||
else if(Character.isLowerCase(temp)){
|
||||
temp += shift;
|
||||
else if(Character.isLowerCase(currentChar)){
|
||||
currentChar += shift;
|
||||
//Wrap around if the letter is now out of bounds
|
||||
if(temp < 'a'){
|
||||
temp += 26;
|
||||
if(currentChar < 'a'){
|
||||
currentChar += 26;
|
||||
}
|
||||
else if(temp > 'z'){
|
||||
temp -= 26;
|
||||
else if(currentChar > 'z'){
|
||||
currentChar -= 26;
|
||||
}
|
||||
}
|
||||
//If it is whitespace, number, or punctuation just let it pass through
|
||||
//Add it to the output string
|
||||
outputString += temp;
|
||||
output.append(currentChar);
|
||||
}
|
||||
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
private String decode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
char temp = inputString.charAt(cnt); //A temperary holder for the current working character
|
||||
char currentChar = 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;
|
||||
if(Character.isUpperCase(currentChar)){
|
||||
currentChar -= shift;
|
||||
if(currentChar < 'A'){
|
||||
currentChar += 26;
|
||||
}
|
||||
else if(temp > 'Z'){
|
||||
temp -= 26;
|
||||
else if(currentChar > 'Z'){
|
||||
currentChar -= 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(Character.isLowerCase(currentChar)){
|
||||
currentChar -= shift;
|
||||
if(currentChar < 'a'){
|
||||
currentChar += 26;
|
||||
}
|
||||
else if(temp > 'z'){
|
||||
temp -= 26;
|
||||
else if(currentChar > 'z'){
|
||||
currentChar -= 26;
|
||||
}
|
||||
}
|
||||
//If it is whitespace, number, or punctuation just let it pass through
|
||||
//Add it to the output string
|
||||
outputString += temp;
|
||||
output.append(currentChar);
|
||||
}
|
||||
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import org.junit.Test;
|
||||
|
||||
public class TestAtbash{
|
||||
@Test
|
||||
public void TestDecode(){
|
||||
public void testDecode(){
|
||||
Atbash cipher = new Atbash();
|
||||
|
||||
//Test 1
|
||||
@@ -29,7 +29,7 @@ public class TestAtbash{
|
||||
assertEquals("Atbash Decoding failed the second test", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void TestEncode(){
|
||||
public void testEncode(){
|
||||
Atbash cipher = new Atbash();
|
||||
|
||||
//Test 1
|
||||
|
||||
Reference in New Issue
Block a user