Solution des exercices - Cours 2
Ex 2.1 Carnet d’adresses
Q1 On obtient logiquement le squelette suivant :
package cours2;
import java.util.HashMap;
import java.util.Map;
public class CarnetAdresse {
Map<String,String> data;
public CarnetAdresse() {
}
public void ajoute(String person, String tel) {
}
public void supprime(String person) {
}
public String telephone(String person) {
}
public String personne(String tel) {
}
public static void main(String [] argv) {
CarnetAdresse ca = new CarnetAdresse();
ca.ajoute("Alphonse", "06 89 23 94 03");
ca.ajoute("Toto","+1 459-123-23-55");
}
}
Q2 En remplissant la méthode personne on se rend compte qu’il faut également tenir à jour une association inverse qui donne
la personne en fonction du téléphone :
package cours2;
import java.util.HashMap;
import java.util.Map;
public class CarnetAdresse {
Map<String,String> data;
Map<String,String> reverseData;
public CarnetAdresse() {
data = new HashMap<String,String> ();
reverseData = new HashMap<String,String> ();
}
public void ajoute(String person, String tel) {
data.put(person,tel);
reverseData.put(tel,person);
}
public void supprime(String person) {
String phone = data.get(person);
if (phone==null) return;
data.remove(person);
reverseData.remove(phone);
}
public String telephone(String person) {
return data.get(person);
Solution des exercices - Cours 2 1/3
}
public String personne(String tel) {
return reverseData.get(tel);
}
public static void main(String [] argv) {
CarnetAdresse ca = new CarnetAdresse();
ca.ajoute("Alphonse", "06 89 23 94 03");
ca.ajoute("Toto","+1 459-123-23-55");
}
}
Q3 On peut par exemple réaliser les tests suivants dans la méthode main :
// test 1 : vérification du numéro de Toto
if (! "+1 459-123-23-55".equals(ca.telephone("Toto"))) {
System.err.println("Test 1 failled");
System.exit(1);
}
// test 2 : vérification du numéro inverse
if (! "Toto".equals(ca.personne("+1 459-123-23-55"))) {
System.err.println("Test 2 failled");
System.exit(1);
}
// test 3 : vérification que null est retourné si pas de résultats
if (ca.personne("This is not a phone number!")!=null) {
System.err.println("Test 3 failled");
System.exit(1);
}
// test 4 : vérification de la suppression
ca.supprime("Toto");
if (ca.telephone("Toto")!=null || ca.telephone("Toto")!=null) {
System.err.println("Test 4 failled");
System.exit(1);
}
System.out.println("All tests ok!");
Le code complet est téléchargeable à l’url :
http://www.derepas.com/java_int/ex_2_1_sol.jar
Ex 2.2 Faire jouer l’ordinateur
On obtient par exemple les résultats ci-dessous :
public void joue() {
int [] evalResul = new int[7];
for (int i=0;i<7;++i) {
evalResul [i]= tableau.evalue(i, maCouleur);
System.out.print(" "+evalResul[i]);
}
System.out.println("");
int maxIndex=0;
for (int i=1;i<7;++i) {
if (evalResul [i]>evalResul[maxIndex]) {
maxIndex=i;
}
}
Vector<Integer> maxValues = new Vector<Integer>();
Solution des exercices - Cours 2 2/3
for (int i=0;i<7;++i) {
if (evalResul [i]==evalResul[maxIndex]) {
maxValues.add(i);
}
}
java.util.Random r = new java.util.Random();
int colonne = maxValues.elementAt(r.nextInt(maxValues.size()));
System.out.println("l’ordinateur joue en "+(colonne+1));
tableau.ajouteJeton(maCouleur,colonne);
}
Le code complet est téléchargeable à l’url :
http://www.derepas.com/java_int/ex_2_2_sol.jar
Solution des exercices - Cours 2 3/3