PolybiusSquare.java edited online with Bitbucket
This commit is contained in:
@@ -13,7 +13,7 @@ import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
|
|||||||
|
|
||||||
public class PolybiusSquare{
|
public class PolybiusSquare{
|
||||||
//A class representing the location of a character in the grid
|
//A class representing the location of a character in the grid
|
||||||
private class CharLocation{
|
protected class CharLocation{
|
||||||
private int x;
|
private int x;
|
||||||
private int y;
|
private int y;
|
||||||
public CharLocation(int x, int y){
|
public CharLocation(int x, int y){
|
||||||
@@ -29,17 +29,17 @@ public class PolybiusSquare{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Variables
|
//Variables
|
||||||
private String inputString; //The message that needs to be encoded/decoded
|
protected String inputString; //The message that needs to be encoded/decoded
|
||||||
private String outputString; //The encoded/decoded message
|
protected String outputString; //The encoded/decoded message
|
||||||
private String keyword; //The keyword used to create the grid
|
protected String keyword; //The keyword used to create the grid
|
||||||
private char[][] grid; //The grid used to encode/decode the message
|
protected char[][] grid; //The grid used to encode/decode the message
|
||||||
private char replaced; //The letter that will need to be replaced in the grid and any input string or keyword
|
protected char replaced; //The letter that will need to be replaced in the grid and any input string or keyword
|
||||||
private char replacer; //The letter that replaces replaced in the input string or keyword
|
protected char replacer; //The letter that replaces replaced in the input string or keyword
|
||||||
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
||||||
private boolean preserveSymbols; //Whether to respect symbols in the output string
|
protected boolean preserveSymbols; //Whether to respect symbols in the output string
|
||||||
|
|
||||||
//Create the grid from the keyword
|
//Create the grid from the keyword
|
||||||
public void createGrid(){
|
protected void createGrid(){
|
||||||
for(int row = 0;row < 5;++row){
|
for(int row = 0;row < 5;++row){
|
||||||
for(int col = 0;col < 5;++col){
|
for(int col = 0;col < 5;++col){
|
||||||
char letter = keyword.charAt((5 * row) + col);
|
char letter = keyword.charAt((5 * row) + col);
|
||||||
@@ -48,7 +48,7 @@ public class PolybiusSquare{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Strips invalid characters from the string that needs encoded/decoded
|
//Strips invalid characters from the string that needs encoded/decoded
|
||||||
public void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
|
protected void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||||
if(inputString == null){
|
if(inputString == null){
|
||||||
throw new NullPointerException("Input cannot be null");
|
throw new NullPointerException("Input cannot be null");
|
||||||
}
|
}
|
||||||
@@ -92,7 +92,7 @@ public class PolybiusSquare{
|
|||||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void setInputStringDecoding(String inputString) throws InvalidCharacterException, InvalidInputException{
|
protected void setInputStringDecoding(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||||
if(inputString == null){
|
if(inputString == null){
|
||||||
throw new NullPointerException("Input cannot be null");
|
throw new NullPointerException("Input cannot be null");
|
||||||
}
|
}
|
||||||
@@ -129,17 +129,17 @@ public class PolybiusSquare{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Returns the input string ready for encoding
|
//Returns the input string ready for encoding
|
||||||
public String getPreparedInputStringEncoding(){
|
protected String getPreparedInputStringEncoding(){
|
||||||
String cleanString = inputString.toUpperCase();
|
String cleanString = inputString.toUpperCase();
|
||||||
cleanString = cleanString.replaceAll("[^A-Z]", "");
|
cleanString = cleanString.replaceAll("[^A-Z]", "");
|
||||||
return cleanString;
|
return cleanString;
|
||||||
}
|
}
|
||||||
public String getPreparedInputStringDecoding(){
|
protected String getPreparedInputStringDecoding(){
|
||||||
String cleanString = inputString.replaceAll("[^0-9]", "");
|
String cleanString = inputString.replaceAll("[^0-9]", "");
|
||||||
return cleanString;
|
return cleanString;
|
||||||
}
|
}
|
||||||
//Strips invalid characters from the keyword and creates the grid
|
//Strips invalid characters from the keyword and creates the grid
|
||||||
public void setKeyword(String keyword){
|
protected void setKeyword(String keyword){
|
||||||
if(keyword == null){
|
if(keyword == null){
|
||||||
throw new NullPointerException("Keyword cannot be null");
|
throw new NullPointerException("Keyword cannot be null");
|
||||||
}
|
}
|
||||||
@@ -164,7 +164,7 @@ public class PolybiusSquare{
|
|||||||
createGrid();
|
createGrid();
|
||||||
}
|
}
|
||||||
//Returns the location of the given charcter in the grid
|
//Returns the location of the given charcter in the grid
|
||||||
public CharLocation findChar(char letter) throws InvalidInputException{
|
protected CharLocation findChar(char letter) throws InvalidInputException{
|
||||||
for(int row = 0;row < grid.length;++row){
|
for(int row = 0;row < grid.length;++row){
|
||||||
for(int col = 0;col < grid[row].length;++col){
|
for(int col = 0;col < grid[row].length;++col){
|
||||||
if(grid[row][col] == letter){
|
if(grid[row][col] == letter){
|
||||||
@@ -176,7 +176,7 @@ public class PolybiusSquare{
|
|||||||
throw new InvalidInputException("The character '" + letter + "' was not found in the grid");
|
throw new InvalidInputException("The character '" + letter + "' was not found in the grid");
|
||||||
}
|
}
|
||||||
//Adds characters that aren't letters to the output
|
//Adds characters that aren't letters to the output
|
||||||
public void addCharactersToCleanStringEncode(String cleanString){
|
protected void addCharactersToCleanStringEncode(String cleanString){
|
||||||
int outputCnt = 0;
|
int outputCnt = 0;
|
||||||
StringBuilder fullOutput = new StringBuilder();
|
StringBuilder fullOutput = new StringBuilder();
|
||||||
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
||||||
@@ -192,7 +192,7 @@ public class PolybiusSquare{
|
|||||||
}
|
}
|
||||||
outputString = fullOutput.toString();
|
outputString = fullOutput.toString();
|
||||||
}
|
}
|
||||||
public void addCharactersToCleanStringDecode(String cleanString){
|
protected void addCharactersToCleanStringDecode(String cleanString){
|
||||||
int outputCnt = 0;
|
int outputCnt = 0;
|
||||||
StringBuilder fullOutput = new StringBuilder();
|
StringBuilder fullOutput = new StringBuilder();
|
||||||
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
||||||
|
|||||||
Reference in New Issue
Block a user