Introduction à Java Corrections des exercices - (CUI)

©Laura Perret 1 / 31 05.11.2006
Introduction à Java
Corrections des exercices
Exercice 1
Soit l’entité « caissier »
Définir
- son identité : nom, prénom, code badge
- son état : caisse numéro 3, horaire 2, etc.
- son comportement : ouvrir caisse, typer article, encaisser, etc.
Exercice 2
Dernière version du SDK : J2SE 1.4.2
http://java.sun.com/j2se/1.4.2/download.html
Exercice 3
classe java.lang.String méthode
public String toLowerCase( )
http://java.sun.com/j2se/1.4.2/docs/api/index.html
Exercice 4
Taper dans une invite de commandes l’instruction java -version
Exercice 5
Le plugin 1.4.2 est disponible pour les navigateurs.
Exercice 6
Code
public class Bienvenue {
public static void main(String args[]) {
System.out.println ("Bienvenue dans le monde Java");
}
}
Instructions de lancement
javac Bienvenue.java
java Bienvenue
Résultat
Bienvenue dans le monde Java
©Laura Perret 2 / 31 05.11.2006
Exercice 7
Code
String s = "12.34";
double a = Double.parseDouble(s);
System.out.println ("La chaine \"" + s + "\" vaut : " + a);
Résultat
La chaine "12.34" vaut : 12.34
Exercice 8
Code
Double aDouble = new Double (3.141592);
System.out.println ("Valeur : " + aDouble.doubleValue());
System.out.println ("Valeur max : " + aDouble.MAX_VALUE);
System.out.println ("Valeur min : " + aDouble.MIN_VALUE);
Résultat
Valeur : 3.141592
Valeur max : 1.7976931348623157E308
Valeur min : 4.9E-324
Exercice 9
Code
String s = "Bonjour";
String t = "";
for (int i = s.length()-1; i >= 0; i--)
t += s.charAt(i);
System.out.println ("Chaine initiale : " + s);
System.out.println ("Chaine obtenue : " + t);
Résultat
Chaine initiale : Bonjour
Chaine obtenue : ruojnoB
©Laura Perret 3 / 31 05.11.2006
Exercice 10
Code
String s = "elu par cette crapule";
int i = 0, compteur = 0;
while (i != -1) {
i = s.indexOf(' ', i+1);
compteur++;
}
System.out.println ("La chaine \"" + s + "\" contient "
+ compteur + " mots.");
Résultat
La chaine "elu par cette crapule" contient 4 mots.
Exercice 11
Code
String s = "elu par cette crapule";
//String s = "coucou";
int i = 0, j = s.length()-1;
boolean palindrome = true, done = false;
while (i < j && !done) {
while (s.charAt(i) == ' ')
i++;
while (s.charAt(j) == ' ')
j--;
if (s.charAt(i++) != s.charAt(j--)) {
palindrome = false;
done = true;
}
}
if (palindrome)
System.out.println ("La chaine \"" + s +
"\" est un palindrome");
else
System.out.println ("La chaine \"" + s +
"\" n'est pas un palindrome");
Résultat
La chaine "elu par cette crapule" est un palindrome
©Laura Perret 4 / 31 05.11.2006
Exercice 12
Code
String texte = "To be or not to be.";
String motif = "to";
int compteur = 0;
Pattern pattern = Pattern.compile(motif);
Matcher matcher = pattern.matcher(texte.toLowerCase());
while (matcher.find()) {
compteur++;
}
System.out.println ("La chaine \"" + texte +
"\" contient " +
compteur + " fois le motif \"" + motif + "\".");
Résultat
La chaine "To be or not to be." contient 2 fois le
motif "to".
Exercice 13
Code
String texte = "To be or not to be.";
String motif = "be";
String rempl = "think";
Pattern pattern = Pattern.compile(motif);
Matcher matcher = pattern.matcher(texte.toLowerCase());
String resultat = matcher.replaceAll(rempl);
System.out.println ("Chaine originale : \"" +
texte + "\"");
System.out.println ("Chaine obtenue : \"" +
resultat + "\"");
Résultat
Chaine originale : "To be or not to be."
Chaine obtenue: "to think or not to think."
©Laura Perret 5 / 31 05.11.2006
Exercice 14
Code
int[] tableau = new int[10];
tableau[0] = 1;
tableau[1] = 1;
for (int i = 2; i < tableau.length; i++)
tableau[i] = tableau[i-2] + tableau[i-1];
System.out.println ("Suite de Fibonacci\n");
for (int i = 0; i < tableau.length; i++)
System.out.print (tableau[i] + " ");
System.out.println ();
Résultat
Suite de Fibonacci
1 1 2 3 5 8 13 21 34 55
Exercice 15
Code
int[][] matrice1 = new int[3][3];
int[][] matrice2 = new int[3][3];
int[][] matrice3 = new int[3][3];
int compteur = 0;
int somme = 0, produit = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
matrice1[i][j] = compteur++;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
matrice2[i][j] = compteur--;
System.out.println ("Matrice 1\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
System.out.print(matrice1[i][j] + " ");
System.out.println ();
}
©Laura Perret 6 / 31 05.11.2006
System.out.println ("\nMatrice 2\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
System.out.print(matrice2[i][j] + " ");
System.out.println ();
}
for (int i = 0; i < 3; i++) { // ligne de m1
for (int j = 0; j < 3; j++) { // colonne de m2
for (int k = 0; k < 3; k++) { // element variable
produit = matrice1[i][k] * matrice2[k][j];
somme += produit;
}
matrice3[i][j] = somme;
somme = 0;
}
}
System.out.println ("\nMatrice 3\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
System.out.print(matrice3[i][j] + " ");
System.out.println ();
}
Résultat
Matrice 1
0 1 2
3 4 5
6 7 8
Matrice 2
9 8 7
6 5 4
3 2 1
Matrice 3
12 9 6
66 54 42
120 99 78
©Laura Perret 7 / 31 05.11.2006
Exercice 16
Code
double d = 3.141592;
int i = (int) d;
short s = 65;
char c = (char) s;
System.out.println ("Double : " + d + "\nEntier : " + i);
System.out.println ("Short : " + s + "\nCaractere : " + c);
Résultat
Double : 3.141592
Entier : 3
Short : 65
Caractere : A
Exercice 17
Code
System.out.println ("6 + 3 * 4 / 2 + 2 = " +
(6 + 3 * 4 / 2 + 2));
int a = 2;
System.out.println ("a = 2; a++ = " + (a++));
//System.out.println ("12-- = " + 12--);
System.out.println ("12-- ne passe pas la compilation");
int b = 3;
System.out.println ("b = 3; --b = " + (--b));
a = 3; b = 2;
System.out.println ("a+=b+=5 = " + (a+=b+=5));
System.out.println ("(a=8) < 10 ? a++ : --a = " +
((a=8) < 10 ? a++ : --a));
a = 2; b = 3;
System.out.println ("a = 2; b = 3; b+++a = " + (b+++a));
System.out.println ("a = " + a + "\nb = " + b);
©Laura Perret 8 / 31 05.11.2006
Résultat
6 + 3 * 4 / 2 + 2 = 14
a = 2; a++ = 2
12-- ne passe pas la compilation
b = 3; --b = 2
a+=b+=5 = 10
(a=8) < 10 ? a++ : --a = 8
a = 2; b = 3; b+++a = 5
a = 2
b = 4
Exercice 18
Code
int i = (int) (Math.random() * 10);
if (i % 2 == 0)
System.out.println ("Le nombre " + i + " est pair.");
else
System.out.println ("Le nombre " + i + " est impair.");
Résultat
Le nombre 2 est pair.
©Laura Perret 9 / 31 05.11.2006
Exercice 19
Code
int note = (int) (Math.random() * 6);
switch (note) {
case 0 :
case 1 :
case 2 :
case 3 :
System.out.println ("note " + note +
" : insuffisant");
break;
case 4 :
System.out.println ("note " + note +
" : suffisant");
break;
case 5 :
System.out.println ("note " + note + " : bien");
break;
case 6 :
System.out.println ("note " + note +
" :tres bien");
break;
}
Résultat
note 3 : insuffisant
Exercice 20
Code
int n = (int) (Math.random() * 10);
System.out.println ("Table de multiplication de " + n
+ "\n");
for (int i = 0; i <= 12; i++)
System.out.println (i + " * " + n + " = " + (i*n));
©Laura Perret 10 / 31 05.11.2006
Résultat
Table de multiplication de 9
0 * 9 = 0
1 * 9 = 9
2 * 9 = 18
3 * 9 = 27
4 * 9 = 36
5 * 9 = 45
6 * 9 = 54
7 * 9 = 63
8 * 9 = 72
9 * 9 = 81
10 * 9 = 90
11 * 9 = 99
12 * 9 = 108
Exercice 21
Code
int i = 0, j = 1;
while (j>0) {
i++;
j = i * i;
}
System.out.println("Le plus grand carre entier : " +
(i-1) + "^2 = " + ((i-1) * (i-1)));
Résultat
Le plus grand carre entier : 46340^2 = 2147395600
Exercice 22
Code
String username, password;
InputStreamReader flux = new InputStreamReader (System.in);
BufferedReader in = new BufferedReader (flux);
try {
do {
System.out.println ("Veuillez saisir votre
username et password.\n");
System.out.print ("username : " );
username = in.readLine();
System.out.print ("password : " );
password = in.readLine();
1 / 16 100%

Introduction à Java Corrections des exercices - (CUI)

La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !