-
Notifications
You must be signed in to change notification settings - Fork 0
/
FracMorse.java
97 lines (81 loc) · 3.26 KB
/
FracMorse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Archit Ashok
Block Y
1/17/22
Fractionated Morse Code Cipher Class
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
public class FracMorse implements Cipherable {
private ArrayList<Character> key = new ArrayList<>();
private final ArrayList<String> morse = new ArrayList<>(Arrays.asList(".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
"..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
"-----", ".-.-.-", "--..--", "---...", "---...", ".-..-.", ".----.", "-.-.--", "..--..", ".--.-.", "-....-"
, "-.-.-.", "-.--.", "-.--.-", "-...-"));
private final ArrayList<String> table = new ArrayList<>(Arrays.asList("...", "..-", "..x", ".-.", ".--", ".-x", ".x.", ".x-", ".xx", "-..", "-.-", "-.x", "--.",
"---", "--x", "-x.", "-x-", "-xx", "x..", "x.-", "x.x", "x-.", "x--", "x-x", "xx.", "xx-"));
public FracMorse(String key) {
for(char val : key.toLowerCase(Locale.ROOT).toCharArray()) {
this.key.add(val);
}
ArrayList<Character> key2 = new ArrayList<>(Arrays.asList('1', '2', '3', '4', '5',
'6', '7', '8', '9', '0', '.', ',', ':', '"', '\'', '!', '?', '@', '-', ';', '(', ')', '='));
this.key.addAll(key2);
}
public FracMorse() {
this("QWERTYUIOPASDFGHJKLZXCVBNM");
}
@Override
public String encode(String in) {
in = in.toLowerCase(Locale.ROOT);
StringBuilder morseCode = new StringBuilder();
for(char val : in.toCharArray()) {
if(val == ' ') {
morseCode.append("x");
} else {
morseCode.append(this.morse.get(this.key.indexOf(val)));
morseCode.append("x");
}
}
morseCode.delete(morseCode.length(), morseCode.length() + 1);
while(morseCode.length() % 3 != 0) {
morseCode.append("x");
}
StringBuilder encoded = new StringBuilder();
for(int i = 0; i < morseCode.length(); i+=3) {
String m = morseCode.substring(i, i+3);
int j = table.indexOf(m);
encoded.append(key.get(j));
}
return encoded.toString();
}
@Override
public String decode(String in) {
in = in.toLowerCase(Locale.ROOT);
StringBuilder morseCode = new StringBuilder();
for(char val:in.toCharArray()) {
morseCode.append(table.get(key.indexOf(val)));
}
StringBuilder temp = new StringBuilder();
StringBuilder decoded = new StringBuilder();
for(char val:morseCode.toString().toCharArray()) {
if(temp.toString().equals("x")) {
if(val == 'x') {
decoded.append(" ");
continue;
}
temp.setLength(0);
} if(val != 'x') {
temp.append(val);
} else {
decoded.append(key.get(morse.indexOf(temp.toString())));
temp.setLength(0);
temp.append('x');
}
}
return decoded.toString();
}
}