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