cours-info/utils/Utils.java

99 lines
2.7 KiB
Java
Raw Normal View History

2019-05-30 06:47:37 +00:00
package utils;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.Scanner;
2019-06-01 15:36:27 +00:00
import java.util.function.BiFunction;
2019-05-30 06:47:37 +00:00
public class Utils {
2019-06-01 16:23:06 +00:00
public static final char[] ALPHABET = new char[] {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};
2019-06-01 15:36:27 +00:00
public static String[] CESAR_DATA_CLEAR = new String[] {
2019-06-01 16:23:06 +00:00
"quitte moi quittons nous juste un peu trop longtemps",
"pour que nous ressentions le bonheur d etre triste"
2019-06-01 15:36:27 +00:00
};
public static int[] CESAR_DATA_KEYS = new int[] {
2019-06-01 16:23:06 +00:00
7, 9
2019-06-01 15:36:27 +00:00
};
public static String[] CESAR_DATA_CRYPT = new String[] {
2019-06-01 16:23:06 +00:00
"xbpaal tvp xbpaavuz uvbz qbzal bu wlb ayvw svunaltwz",
"yxda zdn wxdb anbbnwcrxwb un kxwqnda m ncan carbcn"
2019-06-01 15:36:27 +00:00
};
2019-05-30 06:47:37 +00:00
public static final void afficher(Object... args) {
if (args == null) {
System.out.println("null");
} else {
String str = Arrays.stream(args)
.map(String::valueOf)
.collect(Collectors.joining(" "));
System.out.println(str);
}
2019-06-01 15:36:27 +00:00
System.out.print(' ');
2019-05-30 06:47:37 +00:00
}
2019-05-30 19:38:03 +00:00
public static final void afficherTabInt(int... args) {
if (args == null) {
System.out.println("null");
} else {
String str = Arrays.stream(args)
.boxed()
.map(i -> i+"")
.collect(Collectors.joining(" "));
System.out.println(str);
}
2019-06-01 15:36:27 +00:00
System.out.print(' ');
2019-05-30 19:38:03 +00:00
}
2019-05-30 06:47:37 +00:00
public static int lireEntier() {
try (Scanner reader = new Scanner(System.in)) {
System.out.println("Saisir un entier: ");
return reader.nextInt();
}
}
public static String lireChaine() {
try (Scanner reader = new Scanner(System.in)) {
System.out.println("Saisir une valeur et valider: ");
return reader.nextLine();
}
}
2019-06-01 16:23:06 +00:00
public static int position(char c) {
for (int i = 0; i<ALPHABET.length; i++) {
if (ALPHABET[i] == c) {
return i;
}
}
afficher("erreur", c);
return -1;
}
2019-06-01 15:36:27 +00:00
public static void testCesar(BiFunction<char[], Integer, char[]> func,
boolean chiffre) {
for (int i = 0; i<CESAR_DATA_KEYS.length; i++) {
int cle = CESAR_DATA_KEYS[i];
if (chiffre) {
2019-06-01 16:23:06 +00:00
String result = String.valueOf(func.apply(CESAR_DATA_CLEAR[i]
.toCharArray(), cle));
2019-06-01 15:36:27 +00:00
afficher("Clair: " + CESAR_DATA_CLEAR[i]);
2019-06-01 16:23:06 +00:00
afficher("Chiffré: " + result);
afficher("Succès: " + String.valueOf(CESAR_DATA_CRYPT[i]).equals(result));
2019-06-01 15:36:27 +00:00
} else {
2019-06-01 16:23:06 +00:00
String result = String.valueOf(func.apply(CESAR_DATA_CRYPT[i]
.toCharArray(), cle));
2019-06-01 15:36:27 +00:00
afficher("Chiffré: " + CESAR_DATA_CRYPT[i]);
2019-06-01 16:23:06 +00:00
afficher("Clair: " + result);
afficher("Succès: " + String.valueOf(CESAR_DATA_CLEAR[i]).equals(result));
2019-06-01 15:36:27 +00:00
}
2019-06-01 16:23:06 +00:00
afficher();
2019-06-01 15:36:27 +00:00
}
}
2019-05-30 06:47:37 +00:00
}