PolybiusSquare.java edited online with Bitbucket

This commit is contained in:
2022-01-27 15:34:34 +00:00
parent 0ea7e142e9
commit b679139f66

View File

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