Académique Documents
Professionnel Documents
Culture Documents
Filière ingénieur
Systèmes Intelligents, Communicants et Mobiles
SICoM
Travaux dirigés
Programmation Orientée Java
Auteur :
Pr. Hicham GHENNIOUI
Page 2/43
Page 3/43
SOMMAIRE
OBJET ...................................................................................................................... 5
ENNONCES, TD 1 – TECHNIQUES DE BASE EN PROGRAMMATION
JAVA ........................................................................................................................ 7
1.1 EXERCICE 1 – CHAMPS STATIQUE ...................................................................................... 7
1.2 EXERCICE 2 – UTILISATION DE LA CLASSE WRAPPER ......................................................... 8
1.3 EXERCICE 3 – UTILISATION DE LA CLASSE STRINGBUFFER................................................ 8
1.4 EXERCICE 4 – CLASSE ABSTRAITE ..................................................................................... 9
ENNONCES, TD 2 – GESTION DES EXCEPTIONS......................................... 11
2.1 EXERCICE 1 – DETECTION ET TRAITEMENT DE L’EXCEPTION : DIVISION PAR ZERO ........ 11
2.2 EXERCICE 2 – UTILISATION DES METHODES DE LA CLASSE THROWABLE ......................... 11
2.3 EXERCICE 3 – EXCEPTION PERSONNALISEE DE LA DIVISION PAR ZERO ............................ 12
2.4 EXERCICE 4 – DEPASSEMENT DES BORNES D’UN TABLEAU ............................................... 12
3.1 EXERCICE 1 – UTILISATION DE LA CLASSE SCANNER ....................................................... 15
3.2 EXERCICE 2 – UTILISATION DE LA CLASSE BUFFEREDREADER ........................................ 15
3.3 EXERCICE 3 – STREAM DE DONNEES BONAIRES................................................................ 16
3.4 EXERCICE 4 – STREAM DE CARACTERES .......................................................................... 16
ENNONCES, TD 4 – PROGRAMMATION EVENEMENTIELLE .................. 17
4.1 EXERCICE 1 – PROGRAMMATION EVENEMENTIELLE AVEC AWT .................................... 17
4.1.1Trace d’un segment ............................................................................................................................ 17
4.1.2Trace d’un rectangle .......................................................................................................................... 17
4.1.3Affichage des polices disponibles........................................................................................................ 18
4.1.4Création d’une étiquette ..................................................................................................................... 18
4.1.5Création des case à cocher ................................................................................................................. 18
4.1.6Création des boutons radio ................................................................................................................. 19
4.1.7Création d’une liste déroulante .......................................................................................................... 19
4.1.8Création d’une liste à choix multiple – 1- ........................................................................................... 19
4.1.9Création d’une liste à choix multiple – 2- ........................................................................................... 20
4.1.10
Création bouton avec événement ........................................................................................................ 20
4.2 EXERCICE 2 – PROGRAMMATION EVENEMENTIELLE AVEC SWING................................... 21
4.2.1 Bonjour en swing ............................................................................................................................... 21
4.2.2 Création d’une applet « Bonjour » ..................................................................................................... 22
4.2.3 Création d’une applet « Couleur du fond » ........................................................................................ 22
4.2.4 Création d’une applet « Image » ........................................................................................................ 22
OBJET
Pour en tirer un bon profit de ces séances de travaux dirigés, il est conseillé de
préparer tous les exercices avant l’arrivé en salle de TD.
Page 6/43
Enoncés
Page 7/43
Ligne Résultat
L5 ………………………………………………………………………………………………
L6 ………………………………………………………………………………………………
L8 ………………………………………………………………………………………………
L10 ………………………………………………………………………………………………
L11 ………………………………………………………………………………………………
Page 8/43
Expliquer les lignes L6., L8., L10., L11., L12., L13., L15. et L17.
Ligne Résultat
L5 ………………………………………………………………………………………………
L6 ………………………………………………………………………………………………
L7 ………………………………………………………………………………………………
L9 ………………………………………………………………………………………………
String Nom;
return 2*(longueur+largeur); }
double rayon;
……………………………(………………………. ………………..) {
……………………….=rayon; }
return 2 * ……………………….*rayon ;}
}}
Page 11/43
Indication :
- Le premier bloc de try{} contient l’erreur (j/i)
- Le second bloc catch(ArithmeticException e){} contient le traitement à faire. Par exemple,
afficher le message "Une exception arithmétique est levée".
12 ………………………………………………………………………………………………
14 ………………………………………………………………………………………………
16 ………………………………………………………………………………………………
Page 12/43
Créer une classe qui contient la méthode principale. Cette classe contient la méthode qui génère
l’exception,
public static void controledivisionParZeros(int m) throws SaisieException
Entrée Indication
un mot nextLine()
un entier nextInt()
Un flottant nextFloat()
Un caractère charAt(0)
3. Ecrire un programme java permettant de lire le fichier binaire crée dans la question 1.
3. Ecrire un programme permettant de copier un fichier texte vers un autre fichier texte.
Utiliser les méthodes write et read (condition de EOF : objet.read())!=-1).
Page 17/43
Corrigés
Page 24/43
Page 25/43
Ligne Résultat
Expliquer les lignes L6., L8., L10., L11., L12., L13., L15. et L17.
Ligne Explication
L6. Plus grande valeur positive de type float
L8. Plus petite valeur positive de type float
L10. Conversion float -> Float
L11. Conversion Float -> float
L12. Conversion String -> Float
L13. Conversion Float -> String
L15. Conversion float -> String
L17. Conversion String -> float
Page 27/43
Ligne Résultat
L5 Hello
String Nom;
return 2*(longueur+largeur); }
}
public class Cercle extends Forme {
5.1 EXERCICE 5 – HERITAGE SIMPEL
double rayon;
Cercle(double rayon) {
this.rayon =rayon; }
}}
Page 29/43
Ligne Résultat
14 java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
16 at ExempleException2.main(ExempleException2.java:8)
Créer une classe qui contient la méthode principale. Cette classe contient la méthode qui génère
l’exception,
public static void controledivisionParZeros(int m) throws SaisieException
flottant et un caractère,
3. Ecrire un programme java permettant de lire le fichier binaire crée dans la question 1.
import java.io.*;
public class LectureFichierBinaire {
public static void main(String[] args) throws IOException {
DataInputStream l;
l = new DataInputStream(new FileInputStream("TestFile"));
System.out.println(l.readUTF());
System.out.println(l.readInt());
System.out.println(l.readChar());
System.out.println(l.readBoolean());
l.close();
}
}
2. Lire maintenant les données du fichier crée et afficher le contenu du fichier sur l’écran.
import java.io.*;
public class LectureFichierTexte {
public static void main(String[] args) throws IOException{
BufferedReader r = null;
String l;
try{
r = new BufferedReader (new FileReader("TexttFile.txt"));
}
Page 35/43
catch(FileNotFoundException e){
System.out.println("Erreur d'ouverture du fichier!");
}
while((l = r.readLine())!= null)
System.out.println(l);
r.close();
}
3. Ecrire un programme permettant de copier un fichier texte vers un autre fichier texte.
Utiliser les méthodes write et read (condition de EOF : objet.read())!=-1).
import java.io.*;
public class CopierFichierTexte {
public static void main(String[] args) throws IOException {
int i;
FileReader r = new FileReader("TexttFile.txt");
FileWriter e = new FileWriter("CTextFile.txt");
while((i=r.read())!=-1)
e.write(i);
r.close();
e.close();
}
}
Page 36/43
Page 37/43
li2.addItem(li1.getSelectedItem());
li1.delItem(li1.getSelectedIndex());
}
else if (e.target==b2){
int indice[]=li2.getSelectedIndexes();
for (int i=0; i<indice.length;i++)
li2.delItem(indice[i]);
}
else if (e.target==b3)
System.exit(0);
return true;
}
public static void main(String[] args) {
Exemple59ListeChoixMultipleBis maListe = new
Exemple59ListeChoixMultipleBis("Liste d'éléments");
maListe.setSize(700,400);
maListe.setVisible(true);
}
}
}
}
import java.applet.*;
import java.awt.*;
public class FondJaune extends Applet{
public void init( ) {
this.setBackground(Color.yellow); }}