Added Morse code
This commit is contained in:
122
src/main/java/mattrixwv/CipherStreamJava/Morse.java
Normal file
122
src/main/java/mattrixwv/CipherStreamJava/Morse.java
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Morse.java
|
||||||
|
//Matthew Ellison
|
||||||
|
// Created: 07-26-21
|
||||||
|
//Modified: 07-26-21
|
||||||
|
//This is the declaration of the Morse class
|
||||||
|
package mattrixwv.CipherStreamJava;
|
||||||
|
|
||||||
|
|
||||||
|
public class Morse{
|
||||||
|
//Holds the Morse representation of the alphanumeric characters
|
||||||
|
private static final String[] code = {
|
||||||
|
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", //A-L
|
||||||
|
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", //M-Z
|
||||||
|
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----." //0-9
|
||||||
|
};
|
||||||
|
public static final String version = "1.0"; //The current library's version
|
||||||
|
private String inputString; //The string that needs encoded/decoded
|
||||||
|
private String outputString; //The encoded/decoded message
|
||||||
|
|
||||||
|
//Encodes inputString and stores the result in outputString
|
||||||
|
private String encode(){
|
||||||
|
StringBuilder output = new StringBuilder();
|
||||||
|
//Loop through every element in the input string and see what type it is
|
||||||
|
for(char letter : inputString.toCharArray()){
|
||||||
|
//If the character is a letter get teh correct combination from code and add it to the output
|
||||||
|
if(Character.isUpperCase(letter)){
|
||||||
|
output.append(code[letter - 65]);
|
||||||
|
output.append(' ');
|
||||||
|
}
|
||||||
|
//If it is a number get the correct combination from code and add it to the output
|
||||||
|
else if(Character.isDigit(letter)){
|
||||||
|
int tempNum = Integer.parseInt(Character.toString(letter));
|
||||||
|
output.append(code[tempNum + 26]);
|
||||||
|
output.append(' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Remove the final space from the output
|
||||||
|
if(output.length() > 0){
|
||||||
|
output.replace(output.length() - 1, output.length(), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Save and return the output
|
||||||
|
outputString = output.toString();
|
||||||
|
return outputString;
|
||||||
|
}
|
||||||
|
//Decodes inputString and stores the result in outputString
|
||||||
|
private String decode(){
|
||||||
|
StringBuilder output = new StringBuilder();
|
||||||
|
for(String current : inputString.split(" ")){
|
||||||
|
boolean found = false;
|
||||||
|
//Loop through the code and see if the current letter
|
||||||
|
for(int cnt = 0;(cnt < 26) && (!found);++cnt){
|
||||||
|
//See if current is the same as an element in code
|
||||||
|
if(current.equals(code[cnt])){
|
||||||
|
//Add 65 to cnt to get the correct capital letter
|
||||||
|
output.append((char)(cnt + 'A'));
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Loop through code and see if current is a number
|
||||||
|
for(int cnt = 26;(cnt < 36) && (!found);++cnt){
|
||||||
|
if(current.equals(code[cnt])){
|
||||||
|
//Remove 26 from cnt to get the correct number
|
||||||
|
output.append(Integer.toString(cnt - 26));
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//If it is neither print an error in the output
|
||||||
|
if(!found){
|
||||||
|
output.append("<Unknown symbol: " + current + ">");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Save and return the output
|
||||||
|
outputString = output.toString();
|
||||||
|
return outputString;
|
||||||
|
}
|
||||||
|
//Encodes input and returns the result
|
||||||
|
private void setEncodeInputString(String input){
|
||||||
|
//Make sure the letters are all uppercase
|
||||||
|
input = input.toUpperCase();
|
||||||
|
//Remove all characters that are not capital letters
|
||||||
|
input = input.replaceAll("[^A-Z0-9]", "");
|
||||||
|
//Save the new input
|
||||||
|
inputString = input;
|
||||||
|
}
|
||||||
|
//Decodes input and returns the result
|
||||||
|
private void setDecodeInputString(String input){
|
||||||
|
//Remove all characters except ., -, and ' '
|
||||||
|
input = input.replaceAll("[^ \\.\\-]", "");
|
||||||
|
//Save the new input
|
||||||
|
inputString = input;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Constructor
|
||||||
|
public Morse(){
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
//Returns inputString
|
||||||
|
public String getInputString(){
|
||||||
|
return inputString;
|
||||||
|
}
|
||||||
|
//Returns outputString
|
||||||
|
public String getOutputString(){
|
||||||
|
return outputString;
|
||||||
|
}
|
||||||
|
//Encodes input and returns the result
|
||||||
|
public String encode(String input){
|
||||||
|
setEncodeInputString(input);
|
||||||
|
return encode();
|
||||||
|
}
|
||||||
|
//Decoes input and returns the result
|
||||||
|
public String decode(String input){
|
||||||
|
setDecodeInputString(input);
|
||||||
|
return decode();
|
||||||
|
}
|
||||||
|
//Makes sure all variables are empty
|
||||||
|
public void reset(){
|
||||||
|
inputString = outputString = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
59
src/test/java/mattrixwv/CipherStreamJava/TestMorse.java
Normal file
59
src/test/java/mattrixwv/CipherStreamJava/TestMorse.java
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/TestVigenere.java
|
||||||
|
//Matthew Ellison
|
||||||
|
// Created: 07-25-21
|
||||||
|
//Modified: 07-25-21
|
||||||
|
//These are the tests for the Vigenere class
|
||||||
|
package mattrixwv.CipherStreamJava;
|
||||||
|
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
|
public class TestMorse{
|
||||||
|
@Test
|
||||||
|
public void testDecode(){
|
||||||
|
Morse cipher = new Morse();
|
||||||
|
|
||||||
|
//Test 1
|
||||||
|
String input = "... --- ...";
|
||||||
|
String correctOutput = "SOS";
|
||||||
|
String output = cipher.decode(input);
|
||||||
|
assertEquals("Morse Decoding failed the first test", correctOutput, output);
|
||||||
|
|
||||||
|
//Test 2
|
||||||
|
input = "-- --- .-. ... . -.-. --- -.. .";
|
||||||
|
correctOutput = "MORSECODE";
|
||||||
|
output = cipher.decode(input);
|
||||||
|
assertEquals("Morse Decoding failed the second test", correctOutput, output);
|
||||||
|
|
||||||
|
//Test 3
|
||||||
|
input = ".---- ..--- ...-- ----. ---.. --...";
|
||||||
|
correctOutput = "123987";
|
||||||
|
output = cipher.decode(input);
|
||||||
|
assertEquals("Morse Decoding failed the third test", correctOutput, output);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testEncode(){
|
||||||
|
Morse cipher = new Morse();
|
||||||
|
|
||||||
|
//Test 1
|
||||||
|
String input = "sos";
|
||||||
|
String correctOutput = "... --- ...";
|
||||||
|
String output = cipher.encode(input);
|
||||||
|
assertEquals("Morse Encoding failed the first test", correctOutput, output);
|
||||||
|
|
||||||
|
//Test 2
|
||||||
|
input = "MORSE, CODE";
|
||||||
|
correctOutput = "-- --- .-. ... . -.-. --- -.. .";
|
||||||
|
output = cipher.encode(input);
|
||||||
|
assertEquals("Morse Encoding failed the second test", correctOutput, output);
|
||||||
|
|
||||||
|
//Test 3
|
||||||
|
input = "1.23 987";
|
||||||
|
correctOutput = ".---- ..--- ...-- ----. ---.. --...";
|
||||||
|
output = cipher.encode(input);
|
||||||
|
assertEquals("Morse Encoding failed the third test", correctOutput, output);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user