glog_td4/questioncomplementaire/Morse.java
2018-04-01 14:34:28 +02:00

51 lines
1.2 KiB
Java

package questioncomplementaire;
import java.util.HashMap;
public class Morse extends AlgoCodage{
private String[] alphabet;
private HashMap<String, String> decoder;
public Morse(){
this.nom = "Morse";
alphabet = new String[] {
".-", "-...", "-.-.", "-..", ".", "..-.",
"--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-",
"-.--", "--.."
};
decoder = new HashMap<String, String>();
int a = 65;
for (int i = 0; i < alphabet.length; i++) {
decoder.put(alphabet[i], String.valueOf((char)(a+i)));
}
}
@Override
public String toString() {
return "Morse";
}
@Override
public String encode(String s) {
String encode = "";
int a = 65; // Code ASCII de A
for (int i = 0; i < s.length(); i++) {
encode += alphabet[(int)s.charAt(i)-a] + "/";
}
return encode;
}
@Override
public String decode(String s) {
String decode = "";
int a = 65; // Code ASCII de A
String[] lettres = s.split("/");
for (int i = 0; i < lettres.length; i++) {
decode += decoder.get(lettres[i]);
}
return decode;
}
}