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
|
class Interface1{
public static void main(String[] argv){
String[] t = {"toto", "Pierre", "Alain", "paul", "Marie-Amélie"};
TableauStringOrdonne[] tab = new TableauStringOrdonne[3];
tab[0] = new TableauStringOrdonne(new OrdreLexicographique());
tab[1] = new TableauStringOrdonne(
new OrdreLexicographiqueCaseInsensitive());
tab[2] = new TableauStringOrdonne(new OrdreFolklorique());
for(int i=0; i<5; i++){
for(int j=0; j<3; j++){
tab[j].ajouter(t[i]);
}
}
for(int i=0; i<3; i++){
tab[i].print();
}
}
}
interface Comparaison{
boolean plusPetitStrict(String s1, String s2);
boolean egal(String s1, String s2);
}
class OrdreLexicographique implements Comparaison{
public boolean plusPetitStrict(String s1, String s2){
return s1.compareTo(s2) < 0;
}
public boolean egal(String s1, String s2){
return s1.compareTo(s2) == 0;
}
}
class OrdreLexicographiqueCaseInsensitive
implements Comparaison {
public boolean plusPetitStrict(String s1, String s2){
String s3 = s1.toLowerCase();
String s4 = s2.toLowerCase();
return s3.compareTo(s4) < 0;
}
public boolean egal(String s1, String s2){
String s3 = s1.toLowerCase();
String s4 = s2.toLowerCase();
return s3.compareTo(s4) == 0;
}
}
class OrdreFolklorique implements Comparaison{
int sommeCodesAscii(String s){
int n = 0;
for (int i=0; i<s.length(); i++){
n = n + (int) s.charAt(i);
}
return n;
}
public boolean plusPetitStrict(String s1, String s2){
int n1,n2;
n1= this.sommeCodesAscii(s1);
n2= this.sommeCodesAscii(s2);
return n1 < n2;
}
public boolean egal(String s1, String s2){
int n1,n2;
n1= this.sommeCodesAscii(s1);
n2= this.sommeCodesAscii(s2);
return n1 == n2;
}
}
class TableauStringOrdonne{
String[] t;
int indice;
Comparaison comp;
TableauStringOrdonne(Comparaison c){
this.comp = c;
t = new String[20];
indice = 0;
}
void ajouter(String s){
int i = 0;
while ((i<indice) &&
(comp.plusPetitStrict(t[i],s)))
i++;
for (int j = indice; j>i; j--)
t[j]=t[j-1];
t[i] = s;
indice++;
}
void print(){
for (int i=0; i<indice; i++)
System.out.print(t[i]+" ");
System.out.println();
}
}
|