Escolar Documentos
Profissional Documentos
Cultura Documentos
Alexandre Meslé
15 juillet 2009
Table des matières
1 Notes de cours 4
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.1 Hello World ! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.2 Formats de fichiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.3 Machine virtuelle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.4 Linkage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 Le Java procédural . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2.1 Structure d’une classe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2.3 Entrées-sorties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2.4 Sous-programmes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2.5 Main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2.6 Instructions de contrôle de flux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2.7 Exemple récapitulatif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3 Objets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3.1 Création d’un type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3.2 Les méthodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3.3 L’instanciation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3.4 Les packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.5 Le mot-clé this . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4 Tableaux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.1 Déclaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.2 Instanciation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.3 Accès aux éléments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.4 Longueur d’un tableau . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4.5 Tableaux à plusieurs dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.5 Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.5.1 Exemple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.5.2 Visibilité . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.5.3 Constructeur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.5.4 Accesseurs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.5.5 Surcharge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.6 Héritage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.6.1 Héritage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.6.2 Polymorphisme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.6.3 Redéfinition de méthodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.6.4 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.6.5 Classes Abstraites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.7 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.7.1 Rattraper une exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.7.2 Méthodes levant des exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.7.3 Propagation d’une exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.7.4 Définir une exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1
1.7.5 Lever une exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1.7.6 Rattraper plusieurs exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1.7.7 Finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1.7.8 RuntimeException . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
1.8 Interfaces graphiques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.8.1 Fenêtres . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.8.2 Un premier objet graphique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.8.3 Ecouteurs d’événements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.8.4 Premier exemple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.8.5 Classes anonymes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.8.6 Gestionnaires de mise en forme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.8.7 Un exemple complet : Calcul d’un carré . . . . . . . . . . . . . . . . . . . . . . . . 35
1.9 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.9.1 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.9.2 Types paramétrés . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.9.3 Collections standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
1.10 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
1.10.1 Le principe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
1.10.2 Synchronisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
1.10.3 Mise en Attente . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2 Exercices 44
2.1 Le Java procédural . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.1.1 Initiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.1.2 Arithmétique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.1.3 Pour le sport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.2 Objets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.2.1 Création d’une classe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.2.2 Méthodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.3 Tableaux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.3.1 Prise en main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.3.2 Tris . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.4 Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.4.1 Prise en main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.4.2 Implémentation d’une Pile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.5 Héritage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.5.1 Héritage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.5.2 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.5.3 Classes abstraites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.6 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.7 Interfaces graphiques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.7.1 Prise en main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.7.2 Maintenant débrouillez-vous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.8 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.8.1 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.8.2 Types paramétrées . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.8.3 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.9 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
2.9.1 Prise en main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
2.9.2 Synchronisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
2.9.3 Débrouillez-vous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
2
3 Corrigés 59
3.1 Java Procédural . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.1.1 Initiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.1.2 Arithmétique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.1.3 Pour le sport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
3.2 Objets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
3.2.1 Création d’une classe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
3.2.2 Méthodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.3 Tableaux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.3.1 Prise en main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.3.2 Tris . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.4 Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.4.1 Prise en main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.4.2 Implémentation d’une pile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3.5 Héritage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.5.1 Héritage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.5.2 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
3.5.3 Classes abstraites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
3.6 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
3.7 Interfaces graphiques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
3.7.1 Écouteurs d’événement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
3.7.2 Gestionnaires de mise en forme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
3.8 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.8.1 Deux objets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.8.2 Package Pile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.8.3 Parcours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
3.9 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.9.1 Prise en main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.9.2 Synchronisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.9.3 Mise en attente . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
3
Chapitre 1
Notes de cours
1.1 Introduction
1.1.1 Hello World !
Copiez le code ci-dessous dans un fichier que vous enregistrerez sous le nom HelloWorld.java.
public c l a s s H e l l o W o r l d
{
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
System . o u t . p r i n t l n ( ” H e l l o World ” ) ;
}
}
javac HelloWorld.java
java HelloWorld
Hello World
4
1.1.4 Linkage
En java, le linkage se fait à l’exécution, cela signifie que si vous modifiez un fichier, vous aurez une seule
classe à recompiler, et vous pourrez immédiatement exécuter votre application.
5
1.2 Le Java procédural
Ce cours vous introduit au Java dans sa dimension procédurale. Le but de ce cours est de vous familiariser
avec les instructions de base avant d’aborder les concepts de la programmation objet.
1.2.2 Variables
Nous disposons en Java des mêmes types qu’en C. Tous les types mis à votre disposition par Java sont
appelé types primitifs.
Booléens
L’un deux nous servira toutefois à écrire des choses que l’on rédige habituellement de façon crade en C :
boolean. Une variable de type boolean peut prendre une des deux valeurs true et false, et seulement
une de ces deux valeurs. On déclare et utilise les variables exactement de la même façon qu’en C.
Chaı̂nes de caractères
Il existe un type chaı̂ne de caractères en C. Nous l’examinerons plus détails ultérieurement. Les deux
choses à savoir est que le type chaı̂ne de caractères est String, et que l’opérateur de concaténation est +.
final
Les variables dont la déclaration de type est précédée du mot-clé final sont non-modifiables. Une fois
qu’une valeur leur a été affecté, il n’est plus possible de les modifier. On s’en sert pour représenter des
constantes. Les règles typographiqus sont les mêmes : toutes les lettres en majuscules et les mots séparés
par des . Par exemple,
f i n a l int TAILLE = 1 0 0 ;
Déclare une constante TAILLE de type int initialisée à 100.
1.2.3 Entrées-sorties
La saisie de variables est un calvaire inutile en Java, nous nous en passerons pour le moment. Pour afficher
un message, quel que soit son type, on utilise le sous-programme System.out.print. Par exemple,
System . out . p r i n t ( ” H e l l o World\n” ) ;
Le sous-programme System.out.println ajoute automatiquement un retour à la ligne après l’affichage.
On aurait donc pu écrire :
System . out . p r i n t l n ( ” H e l l o World” ) ;
6
Il est aussi possible d’intercaler valeurs de variables et chaı̂nes de caractères constantes, on sépare les
arguments par des +. Par exemple,
System . out . p r i n t l n ( ”La v a l e u r de a e s t ” + a +
” e t c e l l e de b e s t ” + b ) ;
1.2.4 Sous-programmes
On définit en Java des sous-programmes de la même façon qu’en C, attention toutefois, les sous-programmes
que nous définirons dans ce cours seront déclarés avec le mot-clé static. Par exemple, si je veux faire un
sous-programme qui retourne le successeur de la valeur passée en argument cela donne
class TestJavaProcedural
{
s t a t i c int s u c c ( int i )
{
return i + 1 ;
}
/∗
A ut re s sous−programmes
∗/
}
Si vous oubliez le mot clé static, le compilateur vous enverra des insultes. Nous verrons plus tard ce que
signifie ce mot et dans quels cas il est possible de s’en passer. Attention : Tous les types primitifs en Java
se passent en paramètre par valeur, et cette fois-ci je ne vous mens pas !
1.2.5 Main
Lorsque vous invoquez la machine virtuelle, elle part à la recherche d’un sous-programme particulier
appelé main. Il est impératif qu’il soit défini comme suit :
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
/∗
instructions
∗/
}
La signification du mot-clé public vous sera expliquée ultérieurement. La seule chose dont il faut prendre
note est que vous ne devez pas l’oubliez, sinon la machine virtuelle vous enverra des insultes !
7
int r e s = 1 ;
for ( int i = 1 ; i <= n ; i ++)
r e s ∗= b ;
return r e s ;
}
/∗
A f f i c h e {2ˆ k | k = 0 , . . . , 3 0 } .
∗/
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
f o r ( i n t k = 0 ; k <= 30 ; k++)
System . o u t . p r i n t l n ( ” 2ˆ ” + k + ” = ” + p u i s s a n c e ( 2 , k ) ) ;
}
}
8
1.3 Objets
Dans un langage de programmation, un type est
– Un ensemble de valeurs
– Des opérations sur ces valeurs
En plus des types primitifs, il est possible en Java de créer ses propres types. On appelle type construit
un type non primitif, c’est-à-dire composé de types primitifs. Certains types construits sont fournis dans
les bibliothèques du langage. Si ceux-là ne vosu satisfont pas, vous avez la possibilité de créer vos propres
types.
void p r e s e n t e T o i ( )
{
System . out . p r i n t l n ( ” Je s u i s un p o i n t , mes c o o r d o n né e s s o n t ( ”
+ a b s c i s s e + ” , ” + ordonnee + ” ) ” ) ;
}
}
La méthode presenteToi s’invoque à partir d’un objet de type Point. La syntaxe est p.presenteToi()
où p est de type Point. p est alors le contexte de l’exécution de presenteToi et les champs auquel
accèdera cette méthode seront ceux de l’objet p. Si par exemple, on ecrit q.presenteToi(), c’est q qui
servira de contexte à l’exécution de presenteToi. Lorsque l’on rédige une méthode, l’objet servant de
contexte à l’exécution de la méthode est appelé l’objet courant.
9
1.3.3 L’instanciation
Essayons maintenant, impatients et nerveux, de vérifier ce que donnerait l’exécution de
public c l a s s P oi nt
{
float abscisse ;
f l o a t ordonnee ;
void p r e s e n t e T o i ( )
{
System . out . p r i n t l n ( ” Je s u i s un p o i n t , mes c o o r d o n né e s s o n t ( ”
+ a b s c i s s e + ” , ” + ordonnee + ” ) ” ) ;
}
Que peut bien signifier ce charabia ? Cela signifie que le compilateur a de très sérieuses raisons de penser
que p contient pas d’objet. En Java, toutes les variables de type construit sont des pointeurs. Et les poin-
teurs non initialisés (avec malloc en C) sont des pointeurs ayant la valeur null. Pour faire une allocation
dynamique en Java, on utilise l’instruction new. On fait alors ce que l’on appelle une instanciation. La
syntaxe est donnée dans l’exemple suivant :
p = new P oi nt ( ) ;
Cette instruction crée un objet de type Point et place son adresse dans p. Ainsi p n’est pas un objet,
mais juste un pointeur vers un objet de type Point qui a été crée par le new. Et à partir de ce moment-là,
il devient possible d’accéder aux champs de p. new fonctionne donc de façon similaire à malloc et le
programme suivant est valide :
public c l a s s P o i n t
{
float abscisse ;
f l o a t ordonnee ;
void p r e s e n t e T o i ( )
{
System . o u t . p r i n t l n ( ” Je s u i s un p o i n t , mes c o o r d o n n e e s s o n t ( ”
+ a b s c i s s e + ” , ” + ordonnee + ” ) ” ) ;
}
10
Vous remarquez qu’il n’y a pas de fonction de destruction (free() en C). Un programme appelé Garbage
Collector (ramasse-miette en français) est exécuté dans la machine virtuelle et se charge d’éliminer les
objets non référencés.
11
1.4 Tableaux
1.4.1 Déclaration
Un tableau en Java est un objet. Il est nécessaire de le créer par allocation dynamique avec un new en
précisant ses dimensions. On note
T[ ]
le type tableau d’éléments de type T. La taille du tableau n’est précisée qu’à l’instanciation. On déclare
un tableau t d’éléments de type T de la façon suivante :
T[ ] t ;
Par exemple, si l’on souhaite créer un tableau i d’éléments de type int, on utilise l’instruction :
int [ ] i ;
1.4.2 Instanciation
Comme un tableau est un objet, il est nécessaire d’instancier pendant l’exécution. On instancie un tableau
avec l’instruction
new T [ t a i l l e ]
Par exemple, si l’on souhaite déclarer et allouer dynamiquement un tableau de 100 entiers, en utilise
int [ ] i = new int [ 1 0 0 ] ;
12
1.4.4 Longueur d’un tableau
Pour connaı̂tre la taille d’un tableau on utilise l’attribut length. Par exemple,
int [ ] t = new int [ T ] ;
f o r ( int i = 0 ; i < t . l e n g t h ; i ++)
t[i] = i;
f o r ( int i = 0 ; i < t . l e n g t h ; i ++)
System . out . p r i n t l n ( t [ i ] ) ;
}
}
13
1.5 Encapsulation
1.5.1 Exemple
Implémentons une file de nombre entiers. Nous avons un exemple dans le fichier suivant :
public c l a s s F i l e P u b l i c
{
/∗
Elements de l a f i l e
∗/
int [ ] e n t i e r s ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
I n d i c e de l a t e t e de f i l e e t du p r e m i e r emplacement l i b r e
dans l e t a b l e a u .
∗/
int f i r s t , f i r s t F r e e ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
I n i t i a l i s e l e s a t t r i b u t s de l a f i l e .
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
D e ca l e i d ’ une p o s i t i o n v e r s l a d r o i t e dans l e t a b l e a u ,
r e v i e n t au d e b u t s i i d e b o r d e .
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne v r a i s i e t s e u l e m e n t s i l a f i l e
est pleine .
∗/
14
public boolean e s t P l e i n ( )
{
return f i r s t == i n c r e m e n t e I n d i c e ( f i r s t F r e e ) ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne v r a i s i e t s e u l e m e n t s i
la f i l e est vide .
∗/
public boolean e s t V i d e ( )
{
return f i r s t == f i r s t F r e e ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
A j o u t e l ’ e l e m e n t n dans l a f i l e .
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Supprime l a t e t e de f i l e .
∗/
public void d e f i l e ( )
{
i f ( ! estVide ( ) )
f i r s t = incrementeIndice ( f i r s t ) ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l a t e t e de f i l e .
∗/
public int p r e m i e r ( )
{
i f ( ! estVide ( ) )
15
return e n t i e r s [ f i r s t ] ;
return 0 ;
}
}
Un exemple typique d’utilisation de cette file est donné ci-dessous :
public c l a s s T e s t F i l e P u b l i c
{
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
F i l e P u b l i c f = new F i l e P u b l i c ( ) ;
f . init (20);
f o r ( int i = 0 ; i < 30 ; i +=2)
{
f . enfile ( i );
f . e n f i l e ( i +1);
System . out . p r i n t l n ( f . p r e m i e r ( ) ) ;
f . defile ();
}
while ( ! f . e s t V i d e ( ) )
{
System . out . p r i n t l n ( f . p r e m i e r ( ) ) ;
f . defile ();
}
}
}
Si vous travaillez en équipe, et que vous êtes l’auteur d’une classe FilePublic, vous n’aimeriez pas que
vos collègues programmeurs s’en servent n’importe comment ! Vous n’aimeriez pas par exemple qu’ils
manipulent le tableau entiers ou l’attribut first sans passer par les méthodes, par exemple :
F i l e P u b l i c f = new F i l e P u b l i c ( ) ;
f . init (20);
f . entiers [ 4 ] = 3;
f . f i r s t += 5 ;
/∗ . . . a r r e t o n s l a l e s h o r r e u r s . . . ∗/
Il serait appréciable que nous puissions interdire de telles instructions. C’est-à-dire forcer l’utilisateur de
la classe à passer par les méthodes pour manier la file.
1.5.2 Visibilité
La visibilité d’un identificateur (attribut, méthode ou classe) est l’ensemble des endroits dans le code ou
il est possible de l’utiliser. Si un identificateur et précédé du mot clé public, cela signifie qu’il est visible
partout. Si un identificateur et précédé du mot clé private, cela signifie qu’il n’est visible qu’à l’intérieur
de la classe. Seule l’instance à qui cet identificateur appartient pourra l’utiliser. Par exemple :
public c l a s s F i l e P r i v a t e
{
private int [ ] e n t i e r s ;
private int f i r s t , f i r s t F r e e ;
16
private int i n c r e m e n t e I n d i c e ( int i )
{
i ++;
i f ( i == e n t i e r s . l e n g t h )
i = 0;
return i ;
}
public boolean e s t P l e i n ( )
{
return f i r s t == f i r s t F r e e + 1 ;
}
public boolean e s t V i d e ( )
{
return f i r s t == i n c r e m e n t e I n d i c e ( f i r s t F r e e ) ;
}
public void d e f i l e ( )
{
i f ( ! estVide ( ) )
f i r s t = incrementeIndice ( f i r s t ) ;
}
public int p r e m i e r ( )
{
i f ( ! estVide ( ) )
return e n t i e r s [ f i r s t ] ;
return 0 ;
}
}
On teste cette classe de la même façon :
public c l a s s T e s t F i l e P r i v a t e
{
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
F i l e P r i v a t e f = new F i l e P r i v a t e ( ) ;
f . init (20);
f o r ( int i = 0 ; i < 30 ; i +=2)
{
f . enfile ( i );
f . e n f i l e ( i +1);
System . out . p r i n t l n ( f . p r e m i e r ( ) ) ;
f . defile ();
17
}
while ( ! f . e s t V i d e ( ) )
{
System . out . p r i n t l n ( f . p r e m i e r ( ) ) ;
f . defile ();
}
}
}
S’il vous vient l’idée saugrenue d’exécuter les instructions :
F i l e P r i v a t e f = new F i l e P r i v a t e ( ) ;
f . init (20);
f . entiers [ 4 ] = 3;
f . f i r s t += 5 ;
/∗ . . . a r r e t o n s l a l e s h o r r e u r s . . . ∗/
vous ne passerez pas la compilation. Comme les champs entiers et first sont private, il est impossible
de les utiliser avec la notation pointée. Cela signifie qu’ils ne sont accessibles que depuis l’instance de
la classe FilePrivate qui sert de contexte à leur exécution. De cette façon vous êtes certain que votre
classe fonctionnera correctement. En déclarant des champs privés, vous avez caché les divers détails
de l’implémentation, cela s’appelle l’encapsulation. Celle-ci a pour but de faciliter le travail de tout
programmeur qui utilisera cette classe en masquant la complexité de votre code. Les informations à
communiquer à l’utilisateur de la classe sont la liste des méthodes publiques. A savoir
public c l a s s F i l e P r i v a t e
{
public void i n i t ( int t a i l l e ) { /∗ . . . ∗/ }
public boolean e s t P l e i n ( ) { /∗ . . . ∗/ }
public boolean e s t V i d e ( ) { /∗ . . . ∗/ }
public void e n f i l e ( int n ) { /∗ . . . ∗/ }
public void d e f i l e ( ) { /∗ . . . ∗/ }
public int p r e m i e r ( ) { /∗ . . . ∗/ }
}
On remarque non seulement qu’il plus aisé de comprendre comment utiliser la file en regardant ces
quelques méthodes mais surtout que la façon dont a été implémenté la file est totalement masquée.
1.5.3 Constructeur
Supposons que notre utilisateur oublie d’invoquer la méthode init(int taille), que va-t-il se passer ?
Votre classe va planter. Comment faire pour être certain que toutes les variables seront initialisées ? Un
constructeur est un sous-programme appelé automatiquement à la création de tout objet. Il porte le
même nom que la classe et n’a pas de valeur de retour. De plus, il est possible de lui passer des paramètres
au moment de l’instanciation. Remplaçons init par un constructeur :
public c l a s s F i l e C o n s t r u c t e u r
{
private int [ ] e n t i e r s ;
private int f i r s t , f i r s t F r e e ;
F i l e C o n s t r u c t e u r ( int t a i l l e )
{
e n t i e r s = new int [ t a i l l e ] ;
f i r s t = firstFree = 0;
}
18
{
i ++;
i f ( i == e n t i e r s . l e n g t h )
i = 0;
return i ;
}
public boolean e s t P l e i n ( )
{
return f i r s t == f i r s t F r e e + 1 ;
}
public boolean e s t V i d e ( )
{
return f i r s t == i n c r e m e n t e I n d i c e ( f i r s t F r e e ) ;
}
public void d e f i l e ( )
{
i f ( ! estVide ( ) )
f i r s t = incrementeIndice ( f i r s t ) ;
}
public int p r e m i e r ( )
{
i f ( ! estVide ( ) )
return e n t i e r s [ f i r s t ] ;
return 0 ;
}
}
On peut alors l’utiliser sans la méthode init,
public c l a s s T e s t F i l e C o n s t r u c t e u r
{
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
F i l e C o n s t r u c t e u r f = new F i l e C o n s t r u c t e u r ( 2 0 ) ;
f o r ( int i = 0 ; i < 30 ; i +=2)
{
f . enfile ( i );
f . e n f i l e ( i +1);
System . out . p r i n t l n ( f . p r e m i e r ( ) ) ;
f . defile ();
}
while ( ! f . e s t V i d e ( ) )
{
19
System . out . p r i n t l n ( f . p r e m i e r ( ) ) ;
f . defile ();
}
}
}
1.5.4 Accesseurs
Il est de bonne programmation de déclarer tous les attributs en privé, et de permettre leur accès en
forçant l’utilisateur à passer par des méthodes. Si un attribut X est privé, on crée deux méthodes getX
et setX permettant de manier X. Par exemple,
public c l a s s ExempleAccesseurs
{
private int f o o ;
1.5.5 Surcharge
Il est possible de definir dans une même classe plusieurs fonctions portant le même nom. Par exemple,
public c l a s s F i l e S u r c h a r g e
{
private int [ ] e n t i e r s ;
private int f i r s t , f i r s t F r e e ;
F i l e S u r c h a r g e ( int t a i l l e )
{
e n t i e r s = new int [ t a i l l e ] ;
f i r s t = firstFree = 0;
}
20
i f ( i == e n t i e r s . l e n g t h )
i = 0;
return i ;
}
public boolean e s t P l e i n ( )
{
return f i r s t == f i r s t F r e e + 1 ;
}
public boolean e s t V i d e ( )
{
return f i r s t == i n c r e m e n t e I n d i c e ( f i r s t F r e e ) ;
}
public void d e f i l e ( )
{
i f ( ! estVide ( ) )
f i r s t = incrementeIndice ( f i r s t ) ;
}
public int p r e m i e r ( )
{
i f ( ! estVide ( ) )
return e n t i e r s [ f i r s t ] ;
return 0 ;
}
}
On remarque qu’il y a deux constructeurs. L’un prend la taille du tableau en paramètre et l’autre est
un constructeur de copie. Selon le type de paramètre passé au moment de l’instanciation, le constructeur
correspondant est exécuté. Dans l’exemple ci-dessous, on crée une file g en applicant le constructeur de
copie à la file f .
public c l a s s T e s t F i l e S u r c h a r g e
{
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
F i l e S u r c h a r g e f = new F i l e S u r c h a r g e ( 2 0 ) ;
f o r ( int i = 0 ; i < 30 ; i +=2)
{
f . enfile ( i );
f . e n f i l e ( i +1);
System . out . p r i n t l n ( f . p r e m i e r ( ) ) ;
f . defile ();
}
F i l e S u r c h a r g e g = new F i l e S u r c h a r g e ( f ) ;
21
while ( ! f . e s t V i d e ( ) )
{
System . out . p r i n t l n ( f . p r e m i e r ( ) ) ;
f . defile ();
}
while ( ! g . e s t V i d e ( ) )
{
System . out . p r i n t l n ( g . p r e m i e r ( ) ) ;
g. defile ();
}
}
}
22
1.6 Héritage
1.6.1 Héritage
Le principe
Quand on dispose d’une classe c, on a la possibilité de l’agrandir (ou de l’étendre) en créant une deuxième
classe c0 . On dit dans ce cas que c0 hérite de c, ou encore que c est la classe mère et c0 la classe fille.
Par exemple, considérons les deux classes suivantes :
public c l a s s ClasseMere
{
private f i n a l int x ;
23
Héritage simple Vs. héritage multiple
En java, une classe ne peut avoir qu’une seule classe mère. Dans d’autres langages (comme le C++) cela
est permis, mais pour des raisons de fiabilité, Java l’interdit.
Object
En java, toutes les classes héritent implicitement d’une classe Object.
1.6.2 Polymorphisme
Considérons l’exemple suivant :
public c l a s s T e s t C l a s s e F i l l e P o l y m o r p h i s m e
{
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
ClasseMere o = new C l a s s e F i l l e ( 1 , 2 ) ;
System . out . p r i n t l n ( ” ( ” + o . getX ( ) + ” , ” +
( ( C l a s s e F i l l e ) o ) . getY ( ) + ” ) ” ) ;
}
}
On remarque d’une part que o référence un objet de la classe fille de son type. Cela s’appelle le polymor-
phisme. D’autre part, si l’on souhaite effectuer des opérations spécifiques aux objets de la classe fille, il
est nécessaire d’effectuer un cast.
1.6.4 Interfaces
Une interface est un ensemble de constantes et de méthodes vides. Il est impossible d’instancier une
interface. Il est nécessaire que la classe qui hérite de l’interface (mot-clé implements) définisse les méthodes
qu’elle contient. En voici un exemple d’utilisation :
interface Presentable
{
public void p r e s e n t e T o i ( ) ;
}
public c l a s s E x e m p l e I n t e r f a c e implements P r e s e n t a b l e
{
public int x , y ;
public void p r e s e n t e T o i ( )
{
System . out . p r i n t l n ( ” Je s u i s un E x e m p l e I n t e r f a c e , x = ”
+ x + ” et y = ” + y + ” . ” ) ;
}
24
t . x = 1;
t . y = 2;
Presentable j = t ;
j . presenteToi ( ) ;
}
}
Vous remarquez que la méthode PresenteToi est implémentée dans la classe fille (sinon ça ne compilerait
pas). Comme t est aussi de type Presentable (en vertu du polymorphisme), il est possible de le placer
dans j. Comme tout objet Presentable possède une méthode PresenteToi, on peut invoquer cette
méthode depuis j.
public S t r i n g t o S t r i n g ( )
{
return ”somme = ” + somme + ” ” ;
}
}
La classe devise contient une méthode setSomme qui est surchargée et qui peut prendre en paramètre soit
une somme exprimée dans la bonne unité, soit une autre devise.
public c l a s s Euros extends D e v i s e
{
public Euros ( D e v i s e d )
{
setSomme ( d ) ;
}
public double g e t C o u r s ( )
{
25
return 1 . 4 6 2 5 ;
}
public S t r i n g t o S t r i n g ( )
{
return super . t o S t r i n g ( ) + ” Euros ” ;
}
}
On remarque qu’il est impossible d’implémenter getCours() car le cours varie selon la devise. La classe
Euros hérite de la classe Devise et implémente getCours(). La classe Dollars fonctionne de façon
similaire :
public c l a s s D o l l a r s extends D e v i s e
{
public D o l l a r s ( D e v i s e d )
{
setSomme ( d ) ;
}
public double g e t C o u r s ( )
{
return 1 . ;
}
public S t r i n g t o S t r i n g ( )
{
return super . t o S t r i n g ( ) + ” D o l l a r s ” ;
}
}
Ces classes permettent de prendre en charge automatiquement les conversions entre devises :
public c l a s s T e s t D e v i s e s
{
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
D o l l a r s d = new D o l l a r s ( 1 2 ) ;
System . out . p r i n t l n ( d ) ;
Euros e = new Euros ( d ) ;
System . out . p r i n t l n ( e ) ;
L i v r e s l = new L i v r e s ( e ) ;
System . out . p r i n t l n ( l ) ;
}
}
26
1.7 Exceptions
Le mécanisme des exceptions en Java (et dans d’autres langages objets) permet de gérer de façon élégante
les erreurs pouvant survenir à l’exécution. Elles présentent trois avantages :
– Obliger les programmeurs à gérer les erreurs.
– Séparer le code de traitement des erreurs du reste du code.
– Rattraper les erreurs en cours d’exécution.
27
1.7.2 Méthodes levant des exceptions
Si vous rédigez une méthode susceptible de lever une exception, vous devez le déclarer en ajoutant à son
entête l’expression throws <listeExceptions>. Par exemple,
public void b o u r r e T a b l e a u ( ) throw ArrayIndexOutOfBoundException
{
int i = 0 ;
while ( true )
{
t [ i ++]++;
}
}
Vous remarquez qu’il n’y a pas de try ... catch. Cela signifie que l’erreur doit être rattrapée dans le
sous-programme appelant. Par exemple,
try
{
bourreTableau ( ) ;
}
catch ( ArrayIndexOutOfBoundException e )
{
System . out . p r i n t l n ( ” I l f a l l a i t s ’ y a t t e n d r e . . . ” ) ;
}
Bref, lorsqu’une instruction est susceptible de lever une exception, vous devez soit la rattraper tout de
suite, soit indiquer dans l’entête du sous-programme qu’une exception peut se propager et qu’elle doit
donc être traitée dans le sous-programme appelant.
public S t r i n g t o S t r i n g ( )
{
28
return ”You t r i e d t o do an i l l e g a l a s s i g n e m e n t ! ” ;
}
}
1.7.7 Finally
Il est quelquefois nécessaire qu’une section de code s’exécute quoi qu’il advienne (pour par exemple fermer
un fichier). On place dans ce cas une section finally.
try
{
/∗
......;
∗/
}
29
catch ( /∗ . . . ∗/ )
{
/∗
.....
∗/
}
finally
{
/∗
.....
∗/
}
Par exemple,
c l a s s MonException extends E x c e p t i o n
{
public S t r i n g t o S t r i n g ( )
{
return ” F a l l a i t pas i n v o q u e r c e t t e methode . . . ” ;
}
}
public c l a s s F i n a l l y
{
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
F i n a l l y f = new F i n a l l y ( ) ;
try
{
try
{
throw new MonException ( ) ;
}
catch ( MonException e )
{
throw new MonException ( ) ;
}
finally
{
System . out . p r i n t l n ( ”Tu t ’ a f f i c h e r a s q u o i qu ’ i l a d v i e n n e ! ” ) ;
}
}
catch ( E x c e p t i o n e )
{
System . out . p r i n t l n ( e ) ;
}
}
}
1.7.8 RuntimeException
Je vous vous ai menti au sujet de ArrayIndexOutOfBoundException, il s’agit d’une exception héritant de
RunTimeException qui lui-même hérite de Exception. Un code Java dans lequel une RunTimeException
n’est pas traitée passera la compilation. Par contre, MonException n’est pas une RunTimeException et
doit être rattrapée !
30
1.8 Interfaces graphiques
1.8.1 Fenêtres
La classe JFrame
Une fenêtre est un objet de type JFrame, classe à laquelle on accède avec la commande d’importation
import javax.swing.*. Nous utiliserons les méthodes suivantes :
– public void setTitle(String title), pour définir le titre de la fenêtre.
– public void setVisible(boolean b), pour afficher la fenêtre.
– public void setSize(int width, height), pour préciser les dimensions de la fenêtre.
– public void setDefaultCloseOperation(int operation), pour déterminer le comportement de
la fenêtre lors de sa fermeture.
Exemples
Voici un code de création d’une fenêtre vide.
import j a v a x . swing . ∗ ;
public c l a s s P r e m i e r e F e n e t r e
{
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
JFrame f = new JFrame ( ) ;
f . s e t V i s i b l e ( true ) ;
f . s e t T i t l e ( ”My f i r s t window ! ” ) ;
f . s e t S i z e (200 , 200);
f . s e t D e f a u l t C l o s e O p e r a t i o n ( JFrame . EXIT ON CLOSE ) ;
}
}
La façon suivante nous sera utile pour gérer les événements :
import j a v a x . swing . ∗ ;
public void a f f i c h e ( )
{
s e t V i s i b l e ( true ) ;
}
31
1.8.2 Un premier objet graphique
Nous allons ajouter un bouton dans notre fenêtre. Les boutons sont de type JButton, le constructeur de
cette classe prend en paramètre le libellé du bouton. Un des attributs de la classe JFrame est un objet
contenant tous les composants graphiques de la fenêtre, on accède à cet objet par la méthode public
Container getContentPane(). Tout Container possède une méthode public Component add(Component
comp) permettant d’ajouter n’importe quel composant graphique (par exemple un JButton...). On ajoute
donc un Bouton de la façon suivante getContentPane().add(new JButton("my First JButton")) ;.
import j a v a x . swing . ∗ ;
import j a v a . awt . ∗ ;
32
public void a c t i o n P e r f o r m e d ( ActionEvent e )
{
int k = 0 ;
while ( j B u t t o n s [ k++] != e . g e t S o u r c e ( ) ) ;
System . out . p r i n t l n ( ” c l i c k on JButton ” + k ) ;
}
public PremierEcouteur ( )
{
super ( ) ;
j B u t t o n s = new JButton [ 3 ] ;
s e t T i t l e ( ”My f o u r t h window ! ” ) ;
s e t S i z e (200 , 200);
s e t D e f a u l t C l o s e O p e r a t i o n (EXIT ON CLOSE ) ;
getContentPane ( ) . s e t L a y o u t (new FlowLayout ( ) ) ;
j B u t t o n s [ 0 ] = new JButton ( ”my F i r s t JButton ” ) ;
j B u t t o n s [ 1 ] = new JButton ( ”my Second JButton ” ) ;
j B u t t o n s [ 2 ] = new JButton ( ”my Third JButton ” ) ;
f o r ( int i = 0 ; i < 3 ; i ++)
{
getContentPane ( ) . add ( j B u t t o n s [ i ] ) ;
jButtons [ i ] . addActionListener ( this ) ;
}
s e t V i s i b l e ( true ) ;
}
33
j B u t t o n s [ 2 ] = new JButton ( ”my Third JButton ” ) ;
f o r ( int i = 0 ; i < 3 ; i ++)
getContentPane ( ) . add ( j B u t t o n s [ i ] ) ;
j B u t t o n s [ 0 ] . a d d A c t i o n L i s t e n e r (new A c t i o n L i s t e n e r ( )
{
public void a c t i o n P e r f o r m e d ( ActionEvent e )
{
System . out . p r i n t l n ( ” c l i c k on F i r s t JButton ” ) ;
}
}
);
j B u t t o n s [ 1 ] . a d d A c t i o n L i s t e n e r (new A c t i o n L i s t e n e r ( )
{
public void a c t i o n P e r f o r m e d ( ActionEvent e )
{
System . out . p r i n t l n ( ” c l i c k on Second JButton ” ) ;
}
}
);
j B u t t o n s [ 2 ] . a d d A c t i o n L i s t e n e r (new A c t i o n L i s t e n e r ( )
{
public void a c t i o n P e r f o r m e d ( ActionEvent e )
{
System . out . p r i n t l n ( ” c l i c k on Third JButton ” ) ;
}
}
);
s e t V i s i b l e ( true ) ;
}
34
while ( j B u t t o n s [ k++] != e . g e t S o u r c e ( ) ) ;
System . out . p r i n t l n ( ” c l i c k on JButton ” + k ) ;
}
public TestGridLayout ( )
{
super ( ) ;
j B u t t o n s = new JButton [ 4 ] ;
s e t T i t l e ( ”One More Window ! ” ) ;
s e t S i z e (200 , 200);
s e t D e f a u l t C l o s e O p e r a t i o n (EXIT ON CLOSE ) ;
getContentPane ( ) . s e t L a y o u t (new GridLayout ( 2 , 2 ) ) ;
j B u t t o n s [ 0 ] = new JButton ( ”my F i r s t JButton ” ) ;
j B u t t o n s [ 1 ] = new JButton ( ”my Second JButton ” ) ;
j B u t t o n s [ 2 ] = new JButton ( ”my Third JButton ” ) ;
j B u t t o n s [ 3 ] = new JButton ( ”my Fourth JButton ” ) ;
f o r ( int i = 0 ; i < 4 ; i ++)
{
getContentPane ( ) . add ( j B u t t o n s [ i ] ) ;
jButtons [ i ] . addActionListener ( this ) ;
}
s e t V i s i b l e ( true ) ;
}
import j a v a x . swing . ∗ ;
import j a v a . awt . e v e n t . ∗ ;
import j a v a . awt . ∗ ;
35
r e s u l t T e x t F i e l d . setText ( ”” ) ;
}
}
public Carre ( )
{
super ( ) ;
s e t S i z e (200 , 200);
s e t T i t l e ( ” Square computer ! ” ) ;
s e t D e f a u l t C l o s e O p e r a t i o n (EXIT ON CLOSE ) ;
getContentPane ( ) . s e t L a y o u t (new GridLayout ( 2 , 2 ) ) ;
getContentPane ( ) . add (new JLabel ( ”x = ” ) ) ;
o p e r a n d T e x t F i e l d = new J T e x t F i e l d ( ) ;
operandTextField . addKeyListener ( this ) ;
getContentPane ( ) . add ( o p e r a n d T e x t F i e l d ) ;
getContentPane ( ) . add (new JLabel ( ”x ˆ2 = ” ) ) ;
r e s u l t T e x t F i e l d = new JLabel ( ) ;
getContentPane ( ) . add ( r e s u l t T e x t F i e l d ) ;
s e t V i s i b l e ( true ) ;
}
36
1.9 Collections
1.9.1 Packages
L’instruction import que vous utilisez depuis le début sert à localiser une classe parmi les packages
standard de Java. Si par exemple vous importez import java.awt.event.*, cela signifie que vous allez
utiliser les classes se trouvant dans le package qui se trouve dans le package event qui se trouve dans le
package awt qui se trouve dans le package java (qui est la bibliothèque standard).
public c l a s s ExemplePackage
{
public s t a t i c void p r e s e n t e T o i ( )
{
System . out . p r i n t l n ( ” Je s u i s c o l l e c t i o n s . ExemplePackage ” ) ;
}
Visibilité
En l’absence de mots-clés public (tout le monde), private (seulement la classe) et protected (classe +
classes dérivées), une visibilité par défaut est appliquée. Par défaut, la visibilité des éléments est limitée
au package.
37
Java Vs C++
Le mécanisme des classes paramétrées en Java en bien plus fin que les templates utilisées en C++. Les
templates ne sont que de vulgaires rechercher/remplacer, alors qu’en Java, le code compilé est le même
que si les types ne sont pas paramétrés. Les types paramétrés de java ne sont donc qu’un moyen pour le
compilateur de s’assurer que vous faites les choses proprement en vous obligeant à déclarer les types que
vous placez dans les collections.
La syntaxe
La syntaxe est par contre la même qu’en C++ : il suffit d’utiliser des chevrons pour placer le paramètre.
Par exemple,
public c l a s s ExempleClasseParametree<T>
{
T data ;
public T g e t ( )
{
return data ;
}
}
On est amené naturellement à se demander si ExempleClasseParametree<String> hérite de ExempleClasseParametre
Un exercice traite cette question.
38
Parcours d’une collection
Il existe, de façon analogue à que l’on observe en C++, une syntaxe permettant simplement de parcourir
une collection qui est :
f o r (T x : c )
{
/∗ . . . ∗/
}
Dans la spécification ci dessus, c est la collection, T le type (ou un sur-type) des objets de la collection et
x une variable (muette) dans laquelle sera placé tour à tour chaque objet de la collection. Il n’est possible
d’utiliser cette syntaxe que si le type de c implémente Iterable<T>.
Détails
Parmi les classes implémentant List<T>, on trouve ArrayList<T>, Vector<T> et LinkedList<T>. Les
deux premières sont des tableaux et la dernière une liste chaı̂née. L’interface Set<T> possède une sous-
classe HashSet<T> et une sous-interface SortedSet<T>. Dans un SortedSet<T>, les éléments sont automa-
tiquement maintenus dans l’ordre. Parmi les classes implémentant SortedSet<T> se trouve TreeSet<T>
(arbre binaire de recherche).
39
1.10 Threads
1.10.1 Le principe
Un programme peut être découpé en processus légers, ou threads, s’exécutant chacun de leur coté. On
définit un thread de deux façon :
– En héritant de la classe Thread
– En implémentant l’interface Runnable
Bien que la première solution soit généralement plus commode, la deuxième est quelquefois le seul moyen
d’éviter l’héritage multiple. Nous détaillerons le premier cas, le deuxième est décrit dans la documentation.
La classe Thread
La classe Thread dispose entre autres de deux méthodes
– public void start() qui est la méthode permettant de démarrer l’exécution du thread.
– public void run() qui est la méthode automatiquement invoquée par start quand le thread est
demarré.
Voici un exemple d’utilisation :
public c l a s s B i n a i r e A l e a t o i r e extends Thread
{
private int v a l u e ;
private int n b I t e r a t i o n s ;
L’interface Runnable
Le constructeur de la classe Thread est surchargé pour prendre un paramètre une instance Runnable.
Runnable est une interface contenant une méthode public void run(), celle-ci sera invoquée par le
thread au moment de son lancement.
1.10.2 Synchronisation
Une section critique se construit avec le mot-clé synchronized.
40
Méthodes synchronisées
Une méthode synchronisée ne peut être exécutée que par un thread à la fois. On synchronise une méthode
en plaçant le mot clé synchronized dans sa définition.
Instructions synchronisées
On synchronise des instructions en les plaçant dans un bloc synchronized(o){...}, où o est l’objet ne
pouvant être accédé par deux threads simultanément.
41
i f ( v a l u e == upperBound − 1 )
notify ();
}
}
Plus ( Counter c )
{
this . c = c ;
}
Moins ( Counter c )
{
this . c = c ;
}
42
Ce programme affiche aléatoirement les valeurs prises par un compteur incrémenté et décrémenté alter-
nativement par deux threads. Si l’on tente de décrémenter la valeur minimale, le thread de décrémentation
s’endort pour laisser la main au thread d’incrémentation. Si le thread d’incrémentation est parti de la
valeur minimale, il réveille le thread de décrémentation qui peut reprendre son exécution. Et vice-versa.
43
Chapitre 2
Exercices
2.1.2 Arithmétique
Exercice 2 - chiffres et nombres
Rapellons que a % b est le reste de la division entière de a par b.
1. Ecrire la fonction static int unites(int n) retournant le chiffre des unités du nombre n.
2. Ecrire la fonction static int dizaines(int n) retournant le chiffre des dizaines du nombre n.
3. Ecrire la fonction static int extrait(int n, int p) retourant le p-ème chiffre de représentation
décimale de n en partant des unités.
4. Ecrire la fonction static int nbChiffres(int n) retournant le nombre de chiffres que comporte
la représentation décimale de n.
5. Ecrire la fonction static int sommeChiffres(int n) retournant la somme des chiffres de n.
44
3. Que dire d’un nombre ami avec lui-même ?
4. Ecrire la fonction static boolean estParfait(int n), elle doit retourner true si n est un nombre
parfait, f alse sinon.
45
2.2 Objets
2.2.1 Création d’une classe
Exercice 1 - La classe Rationnel
Créez une classe Rationnel contenant un numérateur et un dénominateur tous deux de type entier.
1 4
Instanciez deux rationnels a et b et initialisez-les aux valeurs respectives et . Affichez ensuite les
2 3
valeurs de ces champs.
2.2.2 Méthodes
Exercice 2 - Opérations sur les Rationnels
Ajoutez à la classe Rationnel les méthodes suivantes :
1. public String toString(), retourne une chaı̂ne de caractères contenant une représentation du
rationnel courant sous forme de chaı̂ne de caractères.
2. public Rationnel copy(), retourne une copie du rationnel courant.
3. public Rationnel opposite(), retourne l’opposé du rationnel courant.
4. public void reduce(), met le rationnel sous forme de fraction irréductible.
5. public boolean isPositive(), retourne true si et seulement si le rationnel courant est stricte-
ment positif.
6. public Rationnel add(Rationnel other), retourne la somme du rationnel courant et du ration-
nel other.
7. public void addBis(Rationnel other), additionne le rationnel other au rationnel courant.
8. public Rationnel multiply(Rationnel other), retourne le produit du rationnel courant avec le
rationnel others.
9. public Rationnel divide(Rationnel other), retourne le quotient du rationnel courant avec le
rationnel others.
10. public int compareTo(Rationnel other), retourne 0 si le rationnel courant est égal au rationnel
other, −1 si le rationnel courant est inférieur à other, 1 dans le cas contraire.
46
2.3 Tableaux
2.3.1 Prise en main
Exercice 1 - Création et affichage
Créer un tableau de 20 entiers, y placer les nombre de 1 à 20, inverser l’ordre des éléments avec une
fonction (récursive si vous y arrivez).
2.3.2 Tris
Exercice 2 - Tris
Créer une classe RatTab contenant un tableau de rationnels, ajoutez les méthodes suivantes :
1. public void init(int taille), instancie le tableau et y place taille valeurs aléatoires import
java.math.Random ;).
2. public String toString(), retourne une chaı̂ne de caractère représentant le tableau.
3. public void triInsertion(), tri le tableau par la méthode du tri par insertion.
4. public void triBulle(), tri le tableau par la méthode du tri à bulle.
5. public void triSelection(), tri le tableau par la méthode du tri par sélection.
6. public void initTab(RatTab otther), place dans le tableau une copie du tableau de other.
7. public RatTab copie(), retourne une copie de l’instance courante.
8. public void interclasse(RatTab t1, RatTab t2), place dans le tableau l’interclassement des
tableaux t1 et t2.
47
2.4 Encapsulation
2.4.1 Prise en main
Exercice 1 - Rationnels propres
Reprennez la classe Rationnel et faites en une classe RationnelPropre. Vous encapsulerez le numérateur,
le dénominateur, et vous utiliserez un constructeur pour initialiser les champs privés. Vous prendrez
ensuite le soin de modifier les corps des méthodes de sorte que la classe compile et fonctionne correctement.
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Pointeur vers l e maillon suivant
∗/
private I n t L i s t next ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
C o n s t r u c t e u r i n i t i a l i s a n t l a donnee e t
l e pointeur vers l ’ element suivant .
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
C o n s t r u c t e u r i n i t i a l i s a n t l a donnee e t
mettant l e suivant a n u l l .
∗/
I n t L i s t ( int data )
{
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
C o n s t r u c t e u r r e c o p i a n t t o u s l e s m a i l l o n s de o t h e r .
48
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l a donnee .
∗/
/∗
M o d i f i e l a donnee
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l e m a i l l o n s u i v a n t .
∗/
public I n t L i s t getNext ( )
{
return null ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Modifie l e maillon suivant
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne une repr Ã
sentation
c s o u s forme de
c h a i n e de l a l i s t e .
∗/
49
public S t r i n g t o S t r i n g ( )
{
return null ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T e s t e l e f o n c t i o n n e m e n t de l a l i s t e .
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T a i l l e de l a p i l e
∗/
private f i n a l int t a i l l e ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
I n d i c e du p r e m i e r e l e m e n t non occupe dans
le tableau .
∗/
private int f i r s t F r e e ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
50
/∗
Constructeur
∗/
P i l e ( int t a i l l e )
{
this . t a i l l e = 0 ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
C o n s t r u c t e u r de c o p i e
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne v r a i s i e t s e u l e m e n t
si la p i l e est vide
∗/
public boolean e s t V i d e ( )
{
return true ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne v r a i s i e t s e u l e m e n t s i l a p i l e e s t
pleine .
∗/
public boolean e s t P l e i n e ( )
{
return true ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l ’ e l e m e n t s e t r o u v a n t au sommet de
l a p i l e , −1 s i l a p i l e e s t v i d e .
∗/
51
return 0 ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Supprime l ’ e l e m e n t s e t r o u v a n t au sommet
de l a p i l e , ne f a i t r i e n s i l a p i l e e s t
vide .
∗/
public void d e p i l e ( )
{
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
A j o u t e d a t a en h a u t de l a p i l e , ne f a i t r i e n
si la pile est pleine .
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne une r e p r e s e n t a t i o n de l a p i l e
au f o r m a t c h a i n e de c a r a c t e r e s .
∗/
public S t r i n g t o S t r i n g ( )
{
return null ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T e s t e l e f o n c t i o n n e m e n t de l a p i l e .
∗/
52
p. depile ();
}
}
}
53
2.5 Héritage
2.5.1 Héritage
Exercice 1 - Point
Définir une classe Point permettant de représenter un point dans R2 . Vous utiliserez proprement les
concepts d’encapsulation.
Exercice 2 - Cercle
Définir une classe Cercle héritant de de la classe précédente. Vous prendrez soin de ne pas recoder des
choses déjà codées...
Exercice 3 - Pile
Adaptez la pile pour qu’elle puisse contenir des objets de n’importe quel type.
2.5.2 Interfaces
Exercice 5 - Animaux
Téléchargez l’archive animaux.tgz (depuis le site seulement), et complétez le code source, sachant qu’un
chien dit ”Ouaf !”, un chat ”Miaou !” et une vache ”Meuh !”.
Exercice 6 - Tab
Reprennez la classe RatTab et adaptez-là de sorte qu’on puisse y placer tout objet d’un type implémentant
l’interface prédéfinie Comparable.
54
2.6 Exceptions
Exercice 1 - l’exception inutile
Créer une classe ClasseInutile contenant une seule méthode nePasInvoquer() contenant une seule
instruction, qui lève l’exception ExceptionInutile. Lorsqu’elle est levée, l’ExceptionInutile affiche
”Je vous avais dit de ne pas invoquer cette fonction !”. Vous testerez la méthode nePasInvoquer() dans
le main de ClasseInutile.
Exercice 2 - Pile
Reprennez le code de la pile, modifiez-le de sorte que si l’on tente de dépiler une pile vide, une exception
soit levée.
55
2.7 Interfaces graphiques
2.7.1 Prise en main
Exercice 1 - Ecouteur d’événement
Créer une fenêtre ”Gestionnaire de disque dur”. Ajouter un bouton ayant pour intitulé ”Formater le
disque dur” et affichant dans la console le message ”formatage en cours”. Vous utiliserez les trois méthodes
suivantes :
1. La même que dans le premier exemple du cours : impléméntation de l’écouteur d’événement par la
JFrame.
2. Avec une classe anonyme.
3. Avec une classe non anonyme.
Exercice 4 - Calculatrice
Coder une calculatrice.
56
2.8 Collections
2.8.1 Packages
Exercice 1 - Pile
Récupérer le code source de pile, implémentée avec des listes chaı̂nées, pour créer un package portant
votre prénom. Vous vérifierez votre installation en utilisant la classe pile en dehors du package. Vous
déterminerez intelligemment la visibilité des divers objets.
Exercice 4 - Pile
Reprennez le package sur la pile et adaptez la pour que la pile, implémentée avec des listes chaı̂nées puisse
fonctionner avec des objets de n’importe quel type.
2.8.3 Collections
Exercice 5 - Parcours
Créez une ArrayList<T>, placez-y des valeurs quelconques, et parcourez-là en affichant ses valeurs.
Exercice 6 - Iterable<T>
Modifiez la Pile pour qu’elle implémente Iterable<T>. Adaptez la fonction toString() en conséquence.
Exercice 7 - Parcours
Créez un TreeSet<T>, placez-y des DeuxObjets<Integer>, et parcourez-le en affichant ses valeurs.
57
2.9 Threads
2.9.1 Prise en main
Exercice 1 - La méthode start()
Remplacez les appels à start() par run(), que remarquez-vous ?
Exercice 2 - Runnable
Reprennez le code de BinaireAleatoire et adaptez-le pour utiliser l’interface Runnable.
2.9.2 Synchronisation
Exercice 3 - Méthode synchronisée
Créez un compteur commun aux deux threads un et zero, vous y placerez une fonction synchronized
boolean stepCounter() qui retournera vrai tant que le compteur n’aura pas atteint sa limite, et qui
retournera tout le temps faux ensuite. Le but étant d’afficher 30000 chiffres, peu importe que ce soit des
1 ou des 0.
2.9.3 Débrouillez-vous
Exercice 5 - Producteur consommateur
Implémentez une file permettant des accès synchronisés. Créez une classe écrivant des valeurs aléatoires
dans la file et une classe lisant ces valeurs et les affichant au fur et à mesure. Lancez plusiseurs instances
de la classe de lecture et plusieurs instances de la classe d’écriture sur la même file.
58
Chapitre 3
Corrigés
3.1.2 Arithmétique
class Arithmetique
{
static int puissance ( int b , int n)
{
int r e s = 1 ;
f o r ( i n t i = 1 ; i <= n ; i ++)
r e s ∗= b ;
return r e s ;
}
public s t a t i c i n t n b C h i f f r e s ( i n t n )
{
int r e s = 0 ;
while ( n != 0 )
{
r e s ++;
n /=10;
}
59
return r e s ;
}
s t a t i c boolean d i v i s e ( i n t a , i n t b )
{
return b%a == 0 ;
}
s t a t i c boolean sontAmis ( i n t a , i n t b )
{
return s o m m e D i v i s e u r s S t r i c t s ( a ) == s o m m e D i v i s e u r s S t r i c t s ( b ) ;
}
s t a t i c boolean e s t P a r f a i t ( i n t n )
{
return n == s o m m e D i v i s e u r s S t r i c t s ( n ) ;
}
s t a t i c i n t sommeParties ( i n t n , i n t p )
{
return n%p u i s s a n c e ( 1 0 , p ) + n/ p u i s s a n c e ( 1 0 , p ) ;
}
s t a t i c boolean e s t K a p r e k a r ( i n t n )
{
i n t c a r r e = n∗n ;
i n t nbc = n b C h i f f r e s ( n∗n ) ;
f o r ( i n t i = 1 ; i < nbc ; i ++)
i f ( sommeParties ( c a r r e , i ) == n )
return true ;
return f a l s e ;
}
/∗
A f f i c h e l e nombre p a s s e en parametre s ’ i l e s t compris e n t r e 1 e t 1 9 .
∗/
s t a t i c void a f f i c h e U n i t e s ( i n t n )
{
switch ( n )
{
case 1 : System . o u t . p r i n t ( ”un” ) ; break ;
case 2 : System . o u t . p r i n t ( ” deux ” ) ; break ;
case 3 : System . o u t . p r i n t ( ” t r o i s ” ) ; break ;
case 4 : System . o u t . p r i n t ( ” q u a t r e ” ) ; break ;
case 5 : System . o u t . p r i n t ( ” c i n q ” ) ; break ;
case 6 : System . o u t . p r i n t ( ” s i x ” ) ; break ;
case 7 : System . o u t . p r i n t ( ” s e p t ” ) ; break ;
case 8 : System . o u t . p r i n t ( ” h u i t ” ) ; break ;
case 9 : System . o u t . p r i n t ( ” n e u f ” ) ; break ;
60
case 10 : System . o u t . p r i n t ( ” d i x ” ) ; break ;
case 11 : System . o u t . p r i n t ( ” o n z e ” ) ; break ;
case 12 : System . o u t . p r i n t ( ” douze ” ) ; break ;
case 13 : System . o u t . p r i n t ( ” t r e i z e ” ) ; break ;
case 14 : System . o u t . p r i n t ( ” q u a t o r z e ” ) ; break ;
case 15 : System . o u t . p r i n t ( ” q u i n z e ” ) ; break ;
case 16 : System . o u t . p r i n t ( ” s e i z e ” ) ; break ;
case 17 : System . o u t . p r i n t ( ” dix−s e p t ” ) ; break ;
case 18 : System . o u t . p r i n t ( ” dix−h u i t ” ) ; break ;
case 19 : System . o u t . p r i n t ( ” dix−n e u f ” ) ; break ;
default : break ;
}
}
/∗
Affiche les d i z a i n e s de 10∗n s i n e s t compris e n t r e 2 e t 9 .
∗/
s t a t i c void a f f i c h e D i z a i n e s ( i n t n )
{
switch ( n )
{
case 2 : System . o u t . p r i n t ( ” v i n g t ” ) ; break ;
case 3 : System . o u t . p r i n t ( ” t r e n t e ” ) ; break ;
case 4 : System . o u t . p r i n t ( ” q u a r a n t e ” ) ; break ;
case 5 : System . o u t . p r i n t ( ” c i n q u a n t e ” ) ; break ;
case 6 :
case 7 : System . o u t . p r i n t ( ” s o i x a n t e ” ) ; break ;
case 8 :
case 9 : System . o u t . p r i n t ( ” q u a t r e −v i n g t ” ) ; break ;
d e f a u l t : break ;
}
}
/∗
Retourne b ˆn
∗/
/∗
E x t r a i t l e s c h i f f r e s de n par t r a n c h e . On i n d i c e s c e s c h i f f r e s a p a r t i r de 1
en p a r t a n t de l a d r o i t e . d e b u t e s t l ’ i n d i c e du d e b u t de l a t r a n c h e e t f i n
l ’ i n d i c e de l a f i n . Par exemple e x t r a i t T r a n c h e (987654321 , 5 , 2) = 5 4 3 2 .
∗/
s t a t i c i n t e x t r a i t T r a n c h e ( i n t n , i n t debut , i n t f i n )
{
return ( n%p u i s s a n c e ( 1 0 , debut ) ) / p u i s s a n c e ( 1 0 , f i n − 1 ) ;
}
/∗
A f f i c h e en t o u t e s lettres l e nombre n , n d o i t e t r e compris e n t r e
0 et 999.
∗/
s t a t i c void a f f i c h e T r o i s C h i f f r e s ( i n t n )
{
int u n i t e s = extraitTranche (n , 1 , 1 ) ;
int d i z a i n e s = extraitTranche (n , 2 , 2 ) ;
int d i z a i n e s U n i t e s = extraitTranche (n , 2 , 1 ) ;
int c e n t a i n e s = extraitTranche (n , 3 , 3 ) ;
i f ( c e n t a i n e s >= 2 )
{
afficheUnites ( centaines );
System . o u t . p r i n t ( ”−” ) ;
}
i f ( c e n t a i n e s != 0 )
System . o u t . p r i n t ( ” c e n t ” ) ;
i f ( d i z a i n e s U n i t e s == 0 && c e n t a i n e s != 1 && c e n t a i n e s != 0 )
System . o u t . p r i n t ( ” s ” ) ;
i f ( d i z a i n e s U n i t e s != 0 && c e n t a i n e s != 0 )
System . o u t . p r i n t ( ”−” ) ;
afficheDizaines ( dizaines );
i f ( u n i t e s == 0 && d i z a i n e s == 8 )
61
System . o u t . p r i n t ( ” s ” ) ;
if ( d i z a i n e s != 0 && d i z a i n e s != 1 &&
( u n i t e s != 0 | | d i z a i n e s == 7 | | d i z a i n e s == 9 ) )
System . o u t . p r i n t ( ”−” ) ;
i f ( u n i t e s == 1 && d i z a i n e s >= 2 && d i z a i n e s <= 6 )
System . o u t . p r i n t ( ” e t −” ) ;
i f ( d i z a i n e s == 1 | | d i z a i n e s == 7 | | d i z a i n e s == 9 )
a f f i c h e U n i t e s ( ( d i z a i n e s U n i t e s − 10 ∗ ( d i z a i n e s − 1 ) ) ) ;
else
afficheUnites ( unites );
}
/∗
A f f i c h e l e nombre n s u i v i de l a c h a i n e u n i t e , a c c o r d e
e s t v r a i s i l ’ u n i t e n ’ e s t pas i n v a r i a b l e .
∗/
s t a t i c void a f f i c h e N o m b r e E t U n i t e ( i n t n , S t r i n g u n i t e , boolean a c c o r d e )
{
i f ( a c c o r d e | | n != 1 )
{
afficheTroisChiffres (n ) ;
i f ( n != 0 )
System . o u t . p r i n t ( ” ” ) ;
}
System . o u t . p r i n t ( u n i t e ) ;
i f ( a c c o r d e && n > 1 )
System . o u t . p r i n t ( ” s ” ) ;
}
s t a t i c void a f f i c h e N o m b r e ( i n t n )
{
int b i l l i o n s = extraitTranche (n , 15 , 1 3 ) ;
int m i l l i a r d s = extraitTranche (n , 12 , 1 0 ) ;
int m i l l i o n s = extraitTranche (n , 9 , 7 ) ;
int m i l l i e r s = extraitTranche (n , 6 , 4 ) ;
int u n i t e s = extraitTranche (n , 3 , 1 ) ;
i f ( b i l l i o n s != 0 )
{
a f f i c h e N o m b r e E t U n i t e ( b i l l i o n s , ” b i l l i o n ” , true ) ;
System . o u t . p r i n t ( ” ” ) ;
}
i f ( m i l l i a r d s != 0 )
{
a f f i c h e N o m b r e E t U n i t e ( m i l l i a r d s , ” m i l l i a r d ” , true ) ;
System . o u t . p r i n t ( ” ” ) ;
}
i f ( m i l l i o n s != 0 )
{
a f f i c h e N o m b r e E t U n i t e ( m i l l i o n s , ” m i l l i o n ” , true ) ;
System . o u t . p r i n t ( ” ” ) ;
}
i f ( m i l l i e r s != 0 )
{
afficheNombreEtUnite ( m i l l i e r s , ” m i l l e ” , false ) ;
System . o u t . p r i n t ( ” ” ) ;
}
i f ( u n i t e s != 0 )
{
afficheNombreEtUnite ( unites , ”” , f a l s e ) ;
}
}
3.2 Objets
3.2.1 Création d’une classe
La classe Rationnel
62
public c l a s s R a t i o n n e l
{
i n t num , den ;
3.2.2 Méthodes
Opérations sur les Rationnels
public c l a s s R a t i o n n e l
{
i n t num , den ;
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public S t r i n g t o S t r i n g ( )
{
return num + ” / ” + den ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l copy ( )
{
R a t i o n n e l r = new R a t i o n n e l ( ) ;
r . num = num ;
r . den = den ;
return r ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l o p p o s i t e ( )
{
R a t i o n n e l r = copy ( ) ;
r . num = −r . num ;
return r ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
private i n t pgcd ( i n t a , i n t b )
{
i f ( b == 0 )
return a ;
return pgcd ( b , a%b ) ;
}
public void r e d u c e ( )
{
i n t p = pgcd (num , den ) ;
num/=p ;
den∗=p ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public boolean i s P o s i t i v e ( )
{
return num > 0 && den > 0 | | num < 0 && den < 0 ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l add ( R a t i o n n e l o t h e r )
{
R a t i o n n e l r e s = new R a t i o n n e l ( ) ;
63
r e s . num = num∗ o t h e r . den + den ∗ o t h e r . num ;
r e s . den = den ∗ o t h e r . den ;
other . reduce ( ) ;
return r e s ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l m u l t i p l y ( R a t i o n n e l o t h e r )
{
R a t i o n n e l r e s = new R a t i o n n e l ( ) ;
r e s . num = num∗ o t h e r . num ;
r e s . den = den ∗ o t h e r . den ;
other . reduce ( ) ;
return r e s ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l d i v i d e ( R a t i o n n e l o t h e r )
{
R a t i o n n e l r e s = new R a t i o n n e l ( ) ;
r e s . num = num∗ o t h e r . den ;
r e s . den = den ∗ o t h e r . num ;
other . reduce ( ) ;
return r e s ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public i n t compareTo ( R a t i o n n e l o t h e r )
{
R a t i o n n e l sub = add ( o t h e r . o p p o s i t e ( ) ) ;
i f ( sub . i s P o s i t i v e ( ) )
return 1 ;
i f ( sub . o p p o s i t e ( ) . i s P o s i t i v e ( ) )
return −1;
return 0 ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
}
}
3.3 Tableaux
3.3.1 Prise en main
Création et affichage
64
public c l a s s PriseEnMainTableaux
{
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public s t a t i c void a f f i c h e ( i n t [ ] t )
{
f o r ( i n t i = 0 ; i < t . l e n g t h ; i ++)
{
System . o u t . p r i n t ( t [ i ] + ” ” ) ;
}
System . o u t . p r i n t l n ( ) ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public s t a t i c void r e v e r s e ( i n t [ ] t )
{
int i = 0 , j = t . length − 1 ;
while ( i < j )
swap ( t , i ++, j −−);
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public s t a t i c void r e v e r s e R e c ( i n t [ ] t , i n t i , i n t j )
{
if ( i < j )
{
swap ( t , i , j ) ;
reverseRec ( t , i + 1 , j − 1);
}
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public s t a t i c void r e v e r s e B i s ( i n t [ ] t )
{
reverseRec ( t , 0 , t . length − 1);
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
3.3.2 Tris
Tris
import j a v a . u t i l . Random ;
import j a v a . l a n g . Math ;
public c l a s s RatTab
{
Rationnel [ ] t ;
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
65
t = new R a t i o n n e l [ t a i l l e ] ;
Random r = new Random ( ) ;
f o r ( i n t i = 0 ; i < t a i l l e ; i ++)
{
t [ i ] = new R a t i o n n e l ( ) ;
t [ i ] . num = r . n e x t I n t ( ) % 1 0 ;
t [ i ] . den = Math . ab s ( r . n e x t I n t ()%10)+1;
}
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public S t r i n g t o S t r i n g ( )
{
String res = ” [ ” ;
i f ( t . l e n g t h >= 1 )
r e s += t [ 0 ] ;
for ( int i = 1 ; i < t . length ; i ++)
r e s += ” , ” + t [ i ] ;
r e s += ” ] ” ;
return r e s ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public void e c h a n g e ( i n t i , i n t j )
{
R a t i o n n e l temp = t [ i ] ;
t[ i ] = t[ j ];
t [ j ] = temp ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public void t r i S e l e c t i o n ( )
{
f o r ( i n t i = 0 ; i < t . l e n g t h − 1 ; i ++)
{
int indiceMin = i ;
f o r ( i n t j = i + 1 ; j < t . l e n g t h ; j ++)
i f ( t [ i n d i c e M i n ] . compareTo ( t [ j ] ) > 1 )
indiceMin = j ;
echange ( i , indiceMin ) ;
}
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public boolean o rd o nn e ( i n t i , i n t j )
{
i f ( ( i − j ) ∗ ( t [ i ] . compareTo ( t [ j ] ) ) < 0 )
{
echange ( i , j ) ;
return true ;
}
return f a l s e ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public void t r i I n s e r t i o n ( )
{
f o r ( i n t i = 1 ; i < t . l e n g t h ; i ++)
{
int j = i ;
while ( j > 0 && o rd on n e ( j −1 , j ) )
j −−;
}
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public void t r i B u l l e ( )
{
f o r ( i n t i = t . l e n g t h − 1 ; i > 0 ; i −−)
f o r ( i n t j = 0 ; j < i ; j ++)
o rd o nn e ( j , j + 1 ) ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
66
public void i n i t T a b ( RatTab o t h e r )
{
t = new R a t i o n n e l [ o t h e r . t . l e n g t h ] ;
f o r ( i n t i = 0 ; i < t . l e n g t h ; i ++)
t [ i ] = other . t [ i ] ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public RatTab c o p i e ( )
{
RatTab o t h e r = new RatTab ( ) ;
other . initTab ( this ) ;
return o t h e r ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
3.4 Encapsulation
3.4.1 Prise en main
Rationnels propres
public c l a s s R a t i o n n e l P r o p r e
{
private i n t num , den ;
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
67
t h i s . num = num ;
t h i s . den = den ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public i n t getNum ( )
{
return num ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public i n t getDen ( )
{
return den ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public S t r i n g t o S t r i n g ( )
{
return num + ” / ” + den ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l P r o p r e copy ( )
{
R a t i o n n e l P r o p r e r = new R a t i o n n e l P r o p r e (num , den ) ;
return r ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l P r o p r e o p p o s i t e ( )
{
R a t i o n n e l P r o p r e r = copy ( ) ;
r . setNum(− r . getNum ( ) ) ;
return r ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
private i n t pgcd ( i n t a , i n t b )
{
i f ( b == 0 )
return a ;
return pgcd ( b , a%b ) ;
}
public void r e d u c e ( )
{
i n t p = pgcd (num , den ) ;
num/=p ;
den∗=p ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public boolean i s P o s i t i v e ( )
{
return num > 0 && den > 0 | | num < 0 && den < 0 ;
}
68
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l P r o p r e add ( R a t i o n n e l P r o p r e o t h e r )
{
RationnelPropre res =
new R a t i o n n e l P r o p r e (
num∗ o t h e r . getDen ( ) + den ∗ o t h e r . getNum ( ) ,
den ∗ o t h e r . getDen ( ) ) ;
other . reduce ( ) ;
return r e s ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l P r o p r e m u l t i p l y ( R a t i o n n e l P r o p r e o t h e r )
{
R a t i o n n e l P r o p r e r e s = new R a t i o n n e l P r o p r e (num∗ o t h e r . getNum ( ) ,
den ∗ o t h e r . getDen ( ) ) ;
other . reduce ( ) ;
return r e s ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l P r o p r e d i v i d e ( R a t i o n n e l P r o p r e o t h e r )
{
R a t i o n n e l P r o p r e r e s = new R a t i o n n e l P r o p r e (num∗ o t h e r . getDen ( ) ,
den ∗ o t h e r . getNum ( ) ) ;
other . reduce ( ) ;
return r e s ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public i n t compareTo ( R a t i o n n e l P r o p r e o t h e r )
{
R a t i o n n e l P r o p r e sub = add ( o t h e r . o p p o s i t e ( ) ) ;
i f ( sub . i s P o s i t i v e ( ) )
return 1 ;
i f ( sub . o p p o s i t e ( ) . i s P o s i t i v e ( ) )
return −1;
return 0 ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
}
}
Listes chaı̂nées
public c l a s s I n t L i s t C o r r i g e
{
/∗
Donnee s t o c k e e dans l e m a i l l o n
∗/
69
private i n t d a t a ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Pointeur vers l e maillon suivant
∗/
private I n t L i s t C o r r i g e n e x t ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
C o n s t r u c t e u r i n i t i a l i s a n t l a donnee e t
l e pointeur vers l ’ element s u i v a n t .
∗/
I n t L i s t C o r r i g e ( i n t data , I n t L i s t C o r r i g e next )
{
this . data = data ;
this . next = next ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
C o n s t r u c t e u r i n i t i a l i s a n t l a donnee e t
mettant l e s u i v a n t a n u l l .
∗/
I n t L i s t C o r r i g e ( int data )
{
t h i s ( data , n u l l ) ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
C o n s t r u c t e u r i n i t i a l i s a n t l a donnee e t
mettant l e s u i v a n t a n u l l .
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l a donnee .
∗/
public i n t g e t D a t a ( )
{
return d a t a ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
M o d i f i e l a donnee
∗/
public void s e t D a t a ( i n t d a t a )
{
this . data = data ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l e m a i l l o n s u i v a n t .
∗/
public I n t L i s t C o r r i g e g e t N e x t ( )
{
return n e x t ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
70
/∗
Modifie l e maillon suivant
∗/
public void s e t N e x t ( I n t L i s t C o r r i g e n e x t )
{
this . next = next ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne une repr Ã
sentation
c s o u s forme de
c h a i n e de l a l i s t e .
∗/
public S t r i n g t o S t r i n g ( )
{
S t r i n g r e s = ”” + data ;
i f ( n e x t != n u l l )
r e s += ” −> ” + n e x t . t o S t r i n g ( ) ;
return r e s ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T e s t e l e f o n c t i o n n e m e n t de l a liste .
∗/
public c l a s s P i l e C o r r i g e
{
/∗
Tableau c o n t e n a n t l e s e l e m e n t s de l a pile .
∗/
private i n t [ ] tab ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T a i l l e de l a pile
∗/
private f i n a l i n t taille ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
I n d i c e du premier e l e m e n t non occupe dans
le tableau .
∗/
private i n t f i r s t F r e e ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Constructeur
∗/
P i l e C o r r i g e ( int t a i l l e )
{
this . t a i l l e = t a i l l e ;
t a b = new i n t [ t a i l l e ] ;
71
firstFree = 0;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
C o n s t r u c t e u r de c o p i e .
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne v r a i s i e t s e u l e m e n t
si la p i l e est vide
∗/
public boolean e s t V i d e ( )
{
return f i r s t F r e e == 0 ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne v r a i s i e t s e u l e m e n t s i la pile est
pleine .
∗/
public boolean e s t P l e i n e ( )
{
return f i r s t F r e e == t a i l l e ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l ’ e l e m e n t s e t r o u v a n t au sommet de
l a p i l e , −1 s i l a p i l e e s t v i d e .
∗/
public i n t sommet ( )
{
i f ( estVide ( ) )
return −1;
return t a b [ f i r s t F r e e − 1 ] ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Supprime l ’ e l e m e n t s e t r o u v a n t au sommet
de l a p i l e , ne f a i t r i e n s i l a p i l e e s t
vide .
∗/
public void d e p i l e ( )
{
i f ( ! estVide ( ) )
f i r s t F r e e −−;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Ajoute d a t a en hau t de l a p i l e , ne f a i t rien
si la pile est pleine .
∗/
public void e m p i l e ( i n t d a t a )
{
if (! estPleine ())
t a b [ f i r s t F r e e ++] = d a t a ;
}
72
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne une r e p r e s e n t a t i o n de l a pile
au format c h a i n e de c a r a c t e r e s .
∗/
public S t r i n g t o S t r i n g ( )
{
String res = ” [ ” ;
for ( int i = 0 ; i < f i r s t F r e e ; i ++)
r e s += ” ” + t a b [ i ] ;
return r e s + ” ] ” ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T e s t e l e f o n c t i o n n e m e n t de l a pile .
∗/
public c l a s s P i l e I n t L i s t
{
/∗
L i s t e c o n t e n a n t l e s e l e m e n t s de l a pile .
∗/
private I n t L i s t C o r r i g e l;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T a i l l e de l a pile
∗/
private f i n a l i n t taille ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Nombre d ’ e l e m e n t s dans l a liste
∗/
private i n t nbItems ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Constructeur
∗/
P i l e I n t L i s t ( int t a i l l e )
{
l = null ;
this . t a i l l e = t a i l l e ;
nbItems = 0 ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
C o n s t r u c t e u r de c o p i e .
∗/
73
P i l e I n t L i s t ( P i l e I n t L i s t other )
{
t a i l l e = other . t a i l l e ;
l = new I n t L i s t C o r r i g e ( o t h e r . l ) ;
nbItems = o t h e r . nbItems ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne v r a i s i e t s e u l e m e n t
si la p i l e est vide
∗/
public boolean e s t V i d e ( )
{
return nbItems == 0 ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne v r a i s i e t s e u l e m e n t s i la pile est
pleine .
∗/
public boolean e s t P l e i n e ( )
{
return nbItems == t a i l l e ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l ’ e l e m e n t s e t r o u v a n t au sommet de
l a p i l e , −1 s i l a p i l e e s t v i d e .
∗/
public i n t sommet ( )
{
i f ( ! estVide ( ) )
return l . g e t D a t a ( ) ;
return −1;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Supprime l ’ e l e m e n t s e t r o u v a n t au sommet
de l a p i l e , ne f a i t r i e n s i l a p i l e e s t
vide .
∗/
public void d e p i l e ( )
{
i f ( ! estVide ( ) )
{
l = l . getNext ( ) ;
nbItems −−;
}
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Ajoute d a t a en hau t de l a p i l e , ne f a i t rien
si la pile est pleine .
∗/
public void e m p i l e ( i n t d a t a )
{
l = new I n t L i s t C o r r i g e ( data , l );
nbItems ++;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne une r e p r e s e n t a t i o n de l a pile
au format c h a i n e de c a r a c t e r e s .
74
∗/
public S t r i n g t o S t r i n g ( )
{
return l . t o S t r i n g ( ) ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T e s t e l e f o n c t i o n n e m e n t de l a pile .
∗/
3.5 Héritage
3.5.1 Héritage
Point
public c l a s s P o i n t C o r r i g e
{
private double abs , o r d ;
PointCorrige ()
{
this (0 , 0 ) ;
}
public S t r i n g t o S t r i n g ( )
{
return ” ( ” + abs + ” , ” + o r d + ” ) ” ;
}
}
Cercle
public c l a s s C e r c l e C o r r i g e extends P o i n t C o r r i g e
75
{
private double r a d i u s ;
CercleCorrige ()
{
this (0 , 0 , 0 ) ;
}
public double g e t R a d i u s ( )
{
return r a d i u s ;
}
Liste d’objets
public c l a s s O b j e c t L i s t
{
/∗
Donnee s t o c k e e dans l e m a i l l o n
∗/
private O b j e c t d a t a ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Pointeur vers l e maillon suivant
∗/
private O b j e c t L i s t n e x t ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
C o n s t r u c t e u r i n i t i a l i s a n t l a donnee e t
l e pointeur vers l ’ element s u i v a n t .
∗/
O b j e c t L i s t ( O b j e c t data , O b j e c t L i s t next )
{
this . data = data ;
this . next = next ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
C o n s t r u c t e u r i n i t i a l i s a n t l a donnee e t
mettant l e s u i v a n t a n u l l .
∗/
O b j e c t L i s t ( Object data )
{
t h i s ( data , n u l l ) ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l a donnee .
∗/
public O b j e c t g e t D a t a ( )
{
return d a t a ;
76
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
M o d i f i e l a donnee
∗/
public void s e t D a t a ( O b j e c t d a t a )
{
this . data = data ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l e m a i l l o n s u i v a n t .
∗/
public O b j e c t L i s t g e t N e x t ( )
{
return n e x t ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Modifie l e maillon suivant
∗/
public void s e t N e x t ( O b j e c t L i s t n e x t )
{
this . next = next ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne une repr Ã
sentation
c s o u s forme de
c h a i n e de l a l i s t e .
∗/
public S t r i n g t o S t r i n g ( )
{
S t r i n g r e s = ”” + data ;
i f ( n e x t != n u l l )
r e s += ” −> ” + n e x t . t o S t r i n g ( ) ;
return r e s ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T e s t e l e f o n c t i o n n e m e n t de l a liste .
∗/
Pile d’objets
public c l a s s O b j e c t S t a c k
{
/∗
L i s t e c o n t e n a n t l e s e l e m e n t s de l a pile .
∗/
private O b j e c t L i s t l;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T a i l l e de l a pile
∗/
77
private f i n a l i n t taille ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Nombre d ’ e l e m e n t s dans l a liste
∗/
private i n t nbItems ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Constructeur
∗/
ObjectStack ( int t a i l l e )
{
l = null ;
this . t a i l l e = t a i l l e ;
nbItems = 0 ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne v r a i s i e t s e u l e m e n t
si la p i l e est vide
∗/
public boolean e s t V i d e ( )
{
return nbItems == 0 ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne v r a i s i e t s e u l e m e n t s i la pile est
pleine .
∗/
public boolean e s t P l e i n e ( )
{
return nbItems == t a i l l e ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l ’ e l e m e n t s e t r o u v a n t au sommet de
l a p i l e , −1 s i l a p i l e e s t v i d e .
∗/
public O b j e c t sommet ( )
{
i f ( ! estVide ( ) )
return l . g e t D a t a ( ) ;
return −1;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Supprime l ’ e l e m e n t s e t r o u v a n t au sommet
de l a p i l e , ne f a i t r i e n s i l a p i l e e s t
vide .
∗/
public void d e p i l e ( )
{
i f ( ! estVide ( ) )
{
l = l . getNext ( ) ;
nbItems −−;
}
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
78
Ajoute d a t a en hau t de l a p i l e , ne f a i t rien
si la pile est pleine .
∗/
public void e m p i l e ( i n t d a t a )
{
l = new O b j e c t L i s t ( data , l ) ;
nbItems ++;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne une r e p r e s e n t a t i o n de l a pile
au format c h a i n e de c a r a c t e r e s .
∗/
public S t r i n g t o S t r i n g ( )
{
return l . t o S t r i n g ( ) ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T e s t e l e f o n c t i o n n e m e n t de l a pile .
∗/
3.5.2 Interfaces
Animaux
public i n t e r f a c e Animal
{
public void setNom ( S t r i n g nom ) ;
public S t r i n g getNom ( ) ;
public S t r i n g p a r l e ( ) ;
}
public S t r i n g getNom ( )
{
return nom ;
}
public S t r i n g p a r l e ( )
{
return ”Waf ! ” ;
}
79
public c l a s s Chat implements Animal
{
private S t r i n g nom ;
public S t r i n g getNom ( )
{
return nom ;
}
public S t r i n g p a r l e ( )
{
return ” Miaou ! ” ;
}
public S t r i n g getNom ( )
{
return nom ;
}
public S t r i n g p a r l e ( )
{
return ”Meuh ! ” ;
}
}
Tableau de Comparables
import j a v a . u t i l . Random ;
import j a v a . l a n g . Math ;
public c l a s s ComparableTab
{
private Comparable [ ] t ;
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
ComparableTab ( i n t t a i l l e )
{
t = new Comparable [ t a i l l e ] ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public S t r i n g t o S t r i n g ( )
{
String res = ” [ ” ;
i f ( t . l e n g t h >= 1 )
r e s += t [ 0 ] ;
for ( int i = 1 ; i < t . length ; i ++)
r e s += ” , ” + t [ i ] ;
r e s += ” ] ” ;
return r e s ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
80
public Comparable g e t ( i n t i n d e x )
{
return t [ i n d e x ] ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public void e c h a n g e ( i n t i , i n t j )
{
Comparable temp = t [ i ] ;
t[ i ] = t[ j ];
t [ j ] = temp ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public void t r i S e l e c t i o n ( )
{
f o r ( i n t i = 0 ; i < t . l e n g t h − 1 ; i ++)
{
int indiceMin = i ;
f o r ( i n t j = i + 1 ; j < t . l e n g t h ; j ++)
i f ( t [ i n d i c e M i n ] . compareTo ( t [ j ] ) > 1 )
indiceMin = j ;
echange ( i , indiceMin ) ;
}
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public boolean o rd o nn e ( i n t i , i n t j )
{
i f ( ( i − j ) ∗ ( t [ i ] . compareTo ( t [ j ] ) ) < 0 )
{
echange ( i , j ) ;
return true ;
}
return f a l s e ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public void t r i I n s e r t i o n ( )
{
f o r ( i n t i = 1 ; i < t . l e n g t h ; i ++)
{
int j = i ;
while ( j > 0 && o rd on n e ( j −1 , j ) )
j −−;
}
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public void t r i B u l l e ( )
{
f o r ( i n t i = t . l e n g t h − 1 ; i > 0 ; i −−)
f o r ( i n t j = 0 ; j < i ; j ++)
o rd on n e ( j , j + 1 ) ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public ComparableTab c o p i e ( )
81
{
ComparableTab o t h e r = new ComparableTab ( t . l e n g t h ) ;
other . initTab ( this ) ;
return o t h e r ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
Rationnels comparables
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public i n t getNum ( )
{
return num ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public i n t getDen ( )
{
return den ;
82
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public S t r i n g t o S t r i n g ( )
{
return num + ” / ” + den ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l C o m p a r a b l e copy ( )
{
R a t i o n n e l C o m p a r a b l e r = new R a t i o n n e l C o m p a r a b l e (num , den ) ;
return r ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l C o m p a r a b l e o p p o s i t e ( )
{
R a t i o n n e l C o m p a r a b l e r = copy ( ) ;
r . setNum(− r . getNum ( ) ) ;
return r ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
private i n t pgcd ( i n t a , i n t b )
{
i f ( b == 0 )
return a ;
return pgcd ( b , a%b ) ;
}
public void r e d u c e ( )
{
i n t p = pgcd (num , den ) ;
num/=p ;
den∗=p ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public boolean i s P o s i t i v e ( )
{
return num > 0 && den > 0 | | num < 0 && den < 0 ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l C o m p a r a b l e add ( R a t i o n n e l C o m p a r a b l e o t h e r )
{
RationnelComparable r e s =
new R a t i o n n e l C o m p a r a b l e (
num∗ o t h e r . getDen ( ) + den ∗ o t h e r . getNum ( ) ,
den ∗ o t h e r . getDen ( ) ) ;
other . reduce ( ) ;
return r e s ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
83
{
num = num∗ o t h e r . getDen ( ) + den ∗ o t h e r . getNum ( ) ;
den = den ∗ o t h e r . getDen ( ) ;
reduce ( ) ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l C o m p a r a b l e m u l t i p l y ( R a t i o n n e l C o m p a r a b l e o t h e r )
{
R a t i o n n e l C o m p a r a b l e r e s = new R a t i o n n e l C o m p a r a b l e (num∗ o t h e r . getNum ( ) ,
den ∗ o t h e r . getDen ( ) ) ;
other . reduce ( ) ;
return r e s ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public R a t i o n n e l C o m p a r a b l e d i v i d e ( R a t i o n n e l C o m p a r a b l e o t h e r )
{
R a t i o n n e l C o m p a r a b l e r e s = new R a t i o n n e l C o m p a r a b l e (num∗ o t h e r . getDen ( ) ,
den ∗ o t h e r . getNum ( ) ) ;
other . reduce ( ) ;
return r e s ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
/∗
−1 s i t h i s < o t h e r
0 si this = others
1 s i t h i s > other
∗/
public i n t compareTo ( O b j e c t o t h e r )
{
R a t i o n n e l C o m p a r a b l e sub = add ( ( ( R a t i o n n e l C o m p a r a b l e ) o t h e r ) . o p p o s i t e ( ) ) ;
i f ( sub . i s P o s i t i v e ( ) )
return 1 ;
i f ( sub . o p p o s i t e ( ) . i s P o s i t i v e ( ) )
return −1;
return 0 ;
}
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−∗/
public abstract c l a s s A n i m a l A b s t r a i t
{
private S t r i n g nom ;
public A n i m a l A b s t r a i t ( S t r i n g nom)
{
setNom (nom ) ;
}
84
{
t h i s . nom = nom ;
}
public S t r i n g getNom ( )
{
return nom ;
}
public abstract S t r i n g p a r l e ( ) ;
}
public c l a s s C h i e n A b s t r a i t extends A n i m a l A b s t r a i t
{
public C h i e n A b s t r a i t ( S t r i n g nom)
{
super (nom ) ;
}
public S t r i n g p a r l e ( )
{
return ”Waf ! ” ;
}
public c l a s s C h a t A b s t r a i t extends A n i m a l A b s t r a i t
{
private S t r i n g nom ;
public C h a t A b s t r a i t ( S t r i n g nom)
{
super (nom ) ;
}
public S t r i n g p a r l e ( )
{
return ” Miaou ! ” ;
}
public c l a s s V a c h e A b s t r a i t extends A n i m a l A b s t r a i t
{
private S t r i n g nom ;
public V a c h e A b s t r a i t ( S t r i n g nom)
{
super (nom ) ;
}
public S t r i n g p a r l e ( )
{
return ”Meuh ! ” ;
}
Arbres syntaxiques
/∗ ∗
Fonction d ’ une v a r i a b l e .
∗/
public abstract c l a s s F u n c t i o n
{
/∗ ∗
Retourne l ’ image de x .
∗/
public abstract double e v a l u a t e ( double x ) ;
/∗ ∗
Retourne l a d&e a c u t e ; r i v&e a c u t e ; e .
∗/
public abstract F u n c t i o n d e r i v a t e ( ) ;
/∗ ∗
Retourne l a f o n c t i o n s i m p l i f i&e a c u t e ; e .
∗/
public abstract F u n c t i o n s i m p l i f y ( ) ;
85
/∗ ∗
S s i l a f o n c t i o n ne c o n t i e n t pas de v a r i a b l e .
∗/
public abstract boolean i s C o n s t a n t ( ) ;
/∗ ∗
S s i l a f o n c t i o n e s t une f e u i l l e valant 0.
∗/
public abstract boolean i s Z e r o ( ) ;
/∗ ∗
Ssi la fonction e s t une f e u i l l e valant 1.
∗/
public abstract boolean i s O n e ( ) ;
/∗ ∗
Retourne l ’ i n t e g r a l e e n t r e a e t b ( a < b ) , c a l c u l&e a c u t e ; e av ec
l a m&e a c u t e ; t h o d e d e s t r a p&e g r a v e ; z e s en
e f f e c t u a n t n b S u b d i v i s i o n s s u b d i v i s i o n s de l ’ i n t e r v a l l e
&a g r a v e ; i n t&e a c u t e ; g r e r .
∗/
public double i n t e g r a t e ( double a , double b , i n t n b S u b d i v i s i o n s )
{
i f (b < a)
return i n t e g r a t e ( b , a , n b S u b d i v i s i o n s ) ;
double e p s = 1 . / n b S u b d i v i s i o n s ;
double i n t e g r a l = ( e v a l u a t e ( a ) + e v a l u a t e ( b ) ) / 2 ;
f o r ( ; a < b ; a += e p s )
{
i n t e g r a l += e v a l u a t e ( a ) ;
}
return i n t e g r a l ∗ e p s ;
}
/∗ ∗
f ( x ) = c , o&u g r a v e ; c e s t une c o n s t a n t e r&e a c u t e ; e l l e .
∗/
public c l a s s C o n s t a n t extends F u n c t i o n
{
private double v a l u e ;
Co n st a n t ( double v a l u e )
{
this . value = value ;
}
public boolean i s Z e r o ( )
{
return v a l u e == 0 ;
}
public boolean i s O n e ( )
{
return v a l u e == 1 ;
}
public boolean i s C o n s t a n t ( )
{
return true ;
}
public F u n c t i o n d e r i v a t e ( )
{
86
return new C o n s t a n t ( 0 ) ;
}
public F u n c t i o n s i m p l i f y ( )
{
return t h i s ;
}
public S t r i n g t o S t r i n g ( )
{
return ” ” + v a l u e ;
}
}
/∗ ∗
f (x) = x .
∗/
public c l a s s V a r i a b l e extends F u n c t i o n
{
Variable ()
{
}
public boolean i s Z e r o ( )
{
return f a l s e ;
}
public boolean i s O n e ( )
{
return f a l s e ;
}
public boolean i s C o n s t a n t ( )
{
return f a l s e ;
}
public F u n c t i o n d e r i v a t e ( )
{
return new C o n s t a n t ( 1 ) ;
}
public F u n c t i o n s i m p l i f y ( )
{
return t h i s ;
}
public S t r i n g t o S t r i n g ( )
{
return ”x” ;
}
}
/∗ ∗
Fonction s ’ exprimant comme une op&e a c u t e ; r a t i o n b i n a i r e e n t r e
deux a u t r e s f o n c t i o n s .
∗/
87
/∗ ∗
Retourne l ’ op&e a c u t e ; r a t e u r b i n a i r e s o u s forme de c a r a c t&e g r a v e ; r e
( ’+ ’ pour une a d d i t i o n ’ − ’ pour une s o u s t r a c t i o n , e t c ) .
∗/
public abstract char toChar ( ) ;
public S t r i n g t o S t r i n g ( )
{
return ” ( ” + l e f t S o n + ” ” + toChar ( ) + ” ” + rightSon + ”) ” ;
}
public boolean i s Z e r o ( )
{
return f a l s e ;
}
public boolean i s O n e ( )
{
return f a l s e ;
}
public boolean i s C o n s t a n t ( )
{
return l e f t S o n . i s C o n s t a n t ( ) && r i g h t S o n . i s C o n s t a n t ( ) ;
}
/∗ ∗
Remplace l e s sous−a r b r e s par l e u r s v e r s i o n s s i m p l i f i&e a c u t e ; es ,
r e t o u r n e une f e u i l l e s i l ’ a r b r e e s t c o n s t a n t .
∗/
protected F u n c t i o n s i m p l i f y S u b T r e e s ( )
{
leftSon = leftSon . simplify ();
rightSon = rightSon . simplify ( ) ;
i f ( isConstant ( ) )
return new C o n s t a n t ( e v a l u a t e ( 0 . 0 ) ) ;
return n u l l ;
}
/∗ ∗
f ( x ) = g ( x ) + h ( x ) , o&u g r a v e ; g e t h s o n t l e s sous−a r b r e s gauche
et droit .
∗/
public c l a s s P l u s extends B i n a r y O p e r a t o r
{
Plus ( Function l e f t S o n , Function rightSon )
{
super ( l e f t S o n , r i g h t S o n ) ;
}
public F u n c t i o n d e r i v a t e ( )
{
return new P l u s ( l e f t S o n . d e r i v a t e ( ) , r i g h t S o n . d e r i v a t e ( ) ) ;
}
public F u n c t i o n s i m p l i f y ( )
{
Function f = simplifySubTrees ( ) ;
i f ( f != n u l l )
return f ;
if ( leftSon . isZero ())
return r i g h t S o n ;
i f ( rightSon . isZero ( ) )
return l e f t S o n ;
return t h i s ;
}
88
}
/∗ ∗
f ( x ) = g ( x ) − h ( x ) , o&u g r a v e ; g e t h s o n t l e s sous−a r b r e s gauche
et droit .
∗/
public F u n c t i o n d e r i v a t e ( )
{
return new Minus ( l e f t S o n . d e r i v a t e ( ) , r i g h t S o n . d e r i v a t e ( ) ) ;
}
public F u n c t i o n s i m p l i f y ( )
{
Function f = simplifySubTrees ( ) ;
i f ( f != n u l l )
return f ;
i f ( rightSon . isZero ( ) )
return l e f t S o n ;
if ( leftSon . isZero ())
return new Times (new C o n s t a n t ( −1) , r i g h t S o n ) ;
return t h i s ;
}
}
/∗ ∗
f ( x ) = g ( x ) ∗ h ( x ) , o&u g r a v e ; g e t h s o n t l e s sous−a r b r e s gauche
et droit .
∗/
public F u n c t i o n d e r i v a t e ( )
{
return new P l u s (new Times ( l e f t S o n . d e r i v a t e ( ) , r i g h t S o n ) ,
new Times ( l e f t S o n , r i g h t S o n . d e r i v a t e ( ) ) ) ;
}
public F u n c t i o n s i m p l i f y ( )
{
Function f = simplifySubTrees ( ) ;
i f ( f != n u l l )
return f ;
i f ( rightSon . isZero () | | leftSon . isZero ( ) )
return new C o n s t a n t ( 0 ) ;
i f ( ri gh tSo n . isOne ( ) )
return l e f t S o n ;
i f ( l e f t S o n . isOne ( ) )
return r i g h t S o n ;
89
return t h i s ;
}
/∗ ∗
f ( x ) = g ( x ) / h ( x ) , o&u g r a v e ; g e t h s o n t l e s sous−a r b r e s gauche
et droit .
∗/
public F u n c t i o n d e r i v a t e ( )
{
return new Div (
new Minus (new Times ( l e f t S o n . d e r i v a t e ( ) , r i g h t S o n ) ,
new Times ( l e f t S o n , r i g h t S o n . d e r i v a t e ( ) ) ) ,
new Times ( r i g h t S o n , r i g h t S o n )
);
}
public F u n c t i o n s i m p l i f y ( )
{
Function f = simplifySubTrees ( ) ;
i f ( f != n u l l )
return f ;
if ( leftSon . isZero ())
return new C o n s t a n t ( 0 ) ;
i f ( ri gh tSo n . isOne ( ) )
return l e f t S o n ;
i f ( l e f t S o n . isOne ( ) )
return r i g h t S o n ;
return t h i s ;
}
}
3.6 Exceptions
Classe inutile
public c l a s s C l a s s e I n u t i l e
{
public void n e P a s I n v o q u e r ( ) throws E x c e p t i o n I n u t i l e
{
throw new E x c e p t i o n I n u t i l e ( ) ;
}
c l a s s E x c e p t i o n I n u t i l e extends E x c e p t i o n
{
public E x c e p t i o n I n u t i l e ( )
90
{
System . o u t . p r i n t l n ( ” Je vous a v a i s d i t de ne pas ” +
” invoquer c e tt e fonction ! ” ) ;
}
public S t r i n g t o S t r i n g ( )
{
return ” Vous a v e z e s s a y e r d ’ i n v o q u e r une methode ” +
” qu ’ i l ne f a l l a i t pas i n v o q u e r ! ” ;
}
}
Pile
public c l a s s O b j e c t S t a c k
{
/∗
L i s t e c o n t e n a n t l e s e l e m e n t s de l a pile .
∗/
private O b j e c t L i s t l;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T a i l l e de l a pile
∗/
private f i n a l i n t taille ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Nombre d ’ e l e m e n t s dans l a liste
∗/
private i n t nbItems ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Constructeur
∗/
ObjectStack ( int t a i l l e )
{
l = null ;
this . t a i l l e = t a i l l e ;
nbItems = 0 ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne v r a i s i e t s e u l e m e n t
si la p i l e est vide
∗/
public boolean e s t V i d e ( )
{
return nbItems == 0 ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne v r a i s i e t s e u l e m e n t s i la pile est
pleine .
∗/
public boolean e s t P l e i n e ( )
{
return nbItems == t a i l l e ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l ’ e l e m e n t s e t r o u v a n t au sommet de
l a p i l e , −1 s i l a p i l e e s t v i d e .
∗/
91
public O b j e c t sommet ( )
{
i f ( ! estVide ( ) )
return l . g e t D a t a ( ) ;
return −1;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Supprime l ’ e l e m e n t s e t r o u v a n t au sommet
de l a p i l e , ne f a i t r i e n s i l a p i l e e s t
vide .
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Ajoute d a t a en hau t de l a p i l e , ne f a i t rien
si la pile est pleine .
∗/
public void e m p i l e ( i n t d a t a )
{
l = new O b j e c t L i s t ( data , l ) ;
nbItems ++;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne une r e p r e s e n t a t i o n de l a pile
au format c h a i n e de c a r a c t e r e s .
∗/
public S t r i n g t o S t r i n g ( )
{
return l . t o S t r i n g ( ) ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T e s t e l e f o n c t i o n n e m e n t de l a pile .
∗/
92
3.7 Interfaces graphiques
3.7.1 Écouteurs d’événement
import j a v a x . s w i n g . ∗ ;
import j a v a . awt . e v e n t . ∗ ;
public void a c t i o n P e r f o r m e d ( A c t i o n E v e n t e )
{
System . o u t . p r i n t l n ( ” Formatage en c o u r s . ” ) ;
}
import j a v a x . s w i n g . ∗ ;
import j a v a . awt . e v e n t . ∗ ;
public FormaterDisqueDurAnonyme ( )
{
s e t T i t l e ( ” G e s t i o n n a i r e du d i s q u e dur ” ) ;
s e t S i z e (100 , 100);
s e t D e f a u l t C l o s e O p e r a t i o n (EXIT ON CLOSE ) ;
JButton f o r m a t e r = new JButton ( ” Formater l e d i s q u e dur ” ) ;
g e t C o n t e n t P a n e ( ) . add ( f o r m a t e r ) ;
f o r m a t e r . a d d A c t i o n L i s t e n e r (new A c t i o n L i s t e n e r ( )
{
public void a c t i o n P e r f o r m e d ( A c t i o n E v e n t e )
{
System . o u t . p r i n t l n ( ” Formatage en c o u r s . ” ) ;
}
}
);
s e t V i s i b l e ( true ) ;
}
import j a v a x . s w i n g . ∗ ;
import j a v a . awt . e v e n t . ∗ ;
93
{
FormaterDisqueDurNonAnonyme f = new FormaterDisqueDurNonAnonyme ( ) ;
}
}
c l a s s A f f i c h a g e F o r m a t a g e implements A c t i o n L i s t e n e r
{
public void a c t i o n P e r f o r m e d ( A c t i o n E v e n t e )
{
System . o u t . p r i n t l n ( ” Formatage en c o u r s . ” ) ;
}
}
Convertisseur Euro/Dollar
import j a v a x . s w i n g . ∗ ;
import j a v a . awt . e v e n t . ∗ ;
import j a v a . awt . ∗ ;
public E u r o D o l l a r ( )
{
g e t C o n t e n t P a n e ( ) . s e t L a y o u t (new GridLayout ( 2 , 2));
d o l l a r T e x t = new J T e x t F i e l d ( ) ;
e u r o T e x t = new J T e x t F i e l d ( ) ;
94
d o l l a r L a b e l = new J L a b e l ( ” D o l l a r s ” ) ;
e u r o L a b e l = new J L a b e l ( ” Euros ” ) ;
g e t C o n t e n t P a n e ( ) . add ( d o l l a r L a b e l ) ;
g e t C o n t e n t P a n e ( ) . add ( e u r o L a b e l ) ;
g e t C o n t e n t P a n e ( ) . add ( d o l l a r T e x t ) ;
g e t C o n t e n t P a n e ( ) . add ( e u r o T e x t ) ;
s e t T i t l e ( ” C o n v e r t i s s e u r Euros / D o l l a r s ” ) ;
s e t S i z e (400 , 50);
s e t D e f a u l t C l o s e O p e r a t i o n (EXIT ON CLOSE ) ;
s e t V i s i b l e ( true ) ;
e u r o T e x t . a d d K e y L i s t e n e r (new K e y L i s t e n e r ( )
{
public void keyTyped ( KeyEvent e ) { }
public void k e y P r e s s e d ( KeyEvent e ) { }
public void k e y R e l e a s e d ( KeyEvent e )
{
c o n v e r t ( euroText , d o l l a r T e x t , O n e D o l l a r I n E u r o ) ;
}
}
);
d o l l a r T e x t . a d d K e y L i s t e n e r (new K e y L i s t e n e r ( )
{
public void keyTyped ( KeyEvent e ) { }
public void k e y P r e s s e d ( KeyEvent e ) { }
public void k e y R e l e a s e d ( KeyEvent e )
{
c o n v e r t ( d o l l a r T e x t , euroText , 1/ O n e D o l l a r I n E u r o ) ;
}
}
);
}
3.8 Collections
3.8.1 Deux objets
public c l a s s DeuxObjets<T extends Comparable<T>>
implements Comparable<DeuxObjets<T>>
{
private T f i r s t ;
private T s e c o n d ;
public DeuxObjets (T f i r s t , T s e c o n d )
{
this . f i r s t = f i r s t ;
this . second = second ;
}
public T g e t P e t i t ( )
{
i f ( f i r s t . compareTo ( s e c o n d ) < 0 )
return f i r s t ;
else
return s e c o n d ;
}
public T getGrand ( )
{
i f ( f i r s t . compareTo ( s e c o n d ) >= 0 )
return f i r s t ;
else
return s e c o n d ;
}
public S t r i n g t o S t r i n g ( )
{
return ” ( ” + f i r s t + ” , ” + s e c o n d + ” ) ” ;
}
95
public s t a t i c void main ( S t r i n g a r g s [ ] )
{
DeuxObjets<I n t e g e r > a = new DeuxObjets<I n t e g e r >(2 , 7 ) ;
System . o u t . p r i n t l n ( a . getGrand ( ) ) ;
}
}
package a l e x a n d r e ;
import j a v a . u t i l . I t e r a t o r ;
private L i s t <T> l ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Constructeur
∗/
Stack ( )
{
l = null ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne v r a i s i e t s e u l e m e n t
si la p i l e est vide
∗/
public boolean e s t V i d e ( )
{
return l == n u l l ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l ’ e l e m e n t s e t r o u v a n t au sommet de
la pile .
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Supprime l ’ e l e m e n t s e t r o u v a n t au sommet
de l a p i l e , ne f a i t r i e n s i l a p i l e e s t
vide .
∗/
public void d e p i l e ( )
{
i f ( ! estVide ( ) )
l = l . getNext ( ) ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Ajoute d a t a en hau t de l a pile .
∗/
96
public void e m p i l e (T d a t a )
{
l = new L i s t <T>(data , l ) ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne un i t e r a t e u r s u r l a pile .
∗/
public I t e r a t o r <T> i t e r a t o r ( )
{
return l . i t e r a t o r ( ) ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne une r e p r e s e n t a t i o n de l a pile
au format c h a i n e de c a r a c t e r e s .
∗/
public S t r i n g t o S t r i n g ( )
{
S t r i n g r e s = ”” ;
f o r (T m : t h i s )
r e s += m + ” ” ;
return r e s ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T e s t e l e f o n c t i o n n e m e n t de l a pile .
∗/
List.java
package a l e x a n d r e ;
import j a v a . u t i l . I t e r a t o r ;
private T d a t a ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Pointeur vers l e maillon suivant
∗/
private L i s t <T> n e x t ;
97
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
C o n s t r u c t e u r i n i t i a l i s a n t l a donnee e t
l e pointeur vers l ’ element s u i v a n t .
∗/
L i s t (T data , L i s t <T> n e x t )
{
this . data = data ;
this . next = next ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
C o n s t r u c t e u r i n i t i a l i s a n t l a donnee e t
mettant l e s u i v a n t a n u l l .
∗/
L i s t (T d a t a )
{
t h i s ( data , n u l l ) ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l a donnee .
∗/
public T g e t D a t a ( )
{
return d a t a ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
M o d i f i e l a donnee
∗/
public void s e t D a t a (T d a t a )
{
this . data = data ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne l e m a i l l o n s u i v a n t .
∗/
public L i s t <T> g e t N e x t ( )
{
return n e x t ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Modifie l e maillon suivant
∗/
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
Retourne un i t e r a t e u r s u r l a liste .
∗/
public I t e r a t o r <T> i t e r a t o r ( )
{
return new M y I t e r a t o r <T>( t h i s ) ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
98
/∗
Retourne une repr Ã
sentation
c s o u s forme de
c h a i n e de l a l i s t e .
∗/
public S t r i n g t o S t r i n g ( )
{
S t r i n g r e s = ”” + data ;
i f ( n e x t != n u l l )
r e s += ” −> ” + n e x t . t o S t r i n g ( ) ;
return r e s ;
}
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
/∗
T e s t e l e f o n c t i o n n e m e n t de l a liste .
∗/
PileVideException.java
package a l e x a n d r e ;
public c l a s s P i l e V i d e E x c e p t i o n extends E x c e p t i o n
{
public S t r i n g t o S t r i n g ( )
{
return ”Can ’ t t o p an empty s t a c k . ” ;
}
}
MyIterator.java
package a l e x a n d r e ;
import j a v a . u t i l . I t e r a t o r ;
M y I t e r a t o r ( L i s t <T> l )
{
this . l = l ;
}
public T n e x t ( )
{
L i s t <T> temp = l ;
l = l . getNext ( ) ;
return temp . g e t D a t a ( ) ;
}
3.8.3 Parcours
Parcours de ArrayList
99
import j a v a . u t i l . A r r a y L i s t ;
import j a v a . u t i l . Random ;
public c l a s s P a r c o u r s A r r a y L i s t
{
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
A r r a y L i s t <I n t e g e r > a = new A r r a y L i s t <I n t e g e r > ( ) ;
Random r = new Random ( ) ;
f o r ( i n t i = 1 ; i <= 40 ; i ++)
a . add ( r . n e x t I n t ( ) ) ;
for ( int i : a )
System . o u t . p r i n t l n ( i ) ;
}
}
Parcours de TreeSet
import j a v a . u t i l . T r e e S e t ;
import j a v a . u t i l . Random ;
public c l a s s P a r c o u r s T r e e S e t
{
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
T r e e S e t<DeuxObjets<I n t e g e r >> a = new T r e e S e t<DeuxObjets<I n t e g e r > >();
Random r = new Random ( ) ;
f o r ( i n t i = 1 ; i <= 40 ; i ++)
a . add (new DeuxObjets<I n t e g e r >( r . n e x t I n t ( ) , r . n e x t I n t ( ) ) ) ;
f o r ( DeuxObjets<I n t e g e r > i : a )
System . o u t . p r i n t l n ( i ) ;
}
}
3.9 Threads
3.9.1 Prise en main
public c l a s s B i n a i r e A l e a t o i r e R u n n a b l e implements Runnable
{
private i n t v a l u e ;
private i n t n b I t e r a t i o n s ;
public B i n a i r e A l e a t o i r e R u n n a b l e ( i n t v a l u e , i n t n b I t e r a t i o n s )
{
this . value = value ;
this . nbIterations = nbIterations ;
}
3.9.2 Synchronisation
Compteur partagé (méthode synchronisée)
public B i n a i r e S y n c h r o n i s e ( i n t v a l u e , Counter c )
{
100
this . value = value ;
this . c = c ;
}
c l a s s Counter
{
private i n t i ;
private f i n a l i n t n b I t e r a t i o n s ;
Counter ( i n t n b I t e r a t i o n s )
{
this . nbIterations = nbIterations ;
i = 1;
}
synchronized boolean s t e p C o u n t e r ( )
{
if ( i > nbIterations )
return f a l s e ;
i ++;
return true ;
}
}
public B i n a i r e S y n c h r o n i s e B i s ( i n t v a l u e , Counter c )
{
this . value = value ;
this . c = c ;
}
c l a s s Counter
{
private i n t i ;
private f i n a l i n t n b I t e r a t i o n s ;
Counter ( i n t n b I t e r a t i o n s )
101
{
this . nbIterations = nbIterations ;
i = 1;
}
boolean s t e p C o u n t e r ( )
{
if ( i > nbIterations )
return f a l s e ;
i ++;
return true ;
}
}
public c l a s s ProducteurConsommateur
{
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
F i l e <I n t e g e r > f = new F i l e <I n t e g e r > ( 3 0 ) ;
Thread p r o d u c t e u r 1 = new P r o d u c t e u r ( f , ” p r o d u c t e u r 1 ” ) ;
Thread p r o d u c t e u r 2 = new P r o d u c t e u r ( f , ” p r o d u c t e u r 2 ” ) ;
Thread consommateur1 = new Consommateur ( f , ” consommateur 1 ” ) ;
Thread consommateur2 = new Consommateur ( f , ” consommateur 2 ” ) ;
producteur1 . s t a r t ( ) ;
producteur2 . s t a r t ( ) ;
consommateur1 . s t a r t ( ) ;
consommateur2 . s t a r t ( ) ;
}
}
c l a s s P r o d u c t e u r extends Thread
{
private F i l e <I n t e g e r > f i l e ;
S t r i n g name ;
102
{
System . o u t . p r i n t l n ( e ) ;
}
}
}
}
}
c l a s s L i s t <T>
{
private T d a t a ;
private L i s t <T> n e x t ;
L i s t (T data , L i s t <T> n e x t )
{
this . data = data ;
this . next = next ;
}
L i s t (T d a t a )
{
t h i s ( data , n u l l ) ;
}
103
public T g e t D a t a ( )
{
return d a t a ;
}
public L i s t <T> g e t N e x t ( )
{
return n e x t ;
}
c l a s s E m p t y F i l e E x c e p t i o n extends E x c e p t i o n
{
public E m p t y F i l e E x c e p t i o n ( )
{
System . o u t . p r i n t l n ( ”Can ’ t r e a d empty f i l e ” ) ;
}
}
c l a s s F u l l F i l e E x c e p t i o n extends E x c e p t i o n
{
public F u l l F i l e E x c e p t i o n ( )
{
System . o u t . p r i n t l n ( ”Can ’ t w r i t e f u l l file ”);
}
}
c l a s s F i l e <T>
{
private L i s t <T> f i r s t = n u l l ;
private L i s t <T> l a s t = n u l l ;
private i n t nbElements = 0 ;
private f i n a l i n t maxNbElements ;
public F i l e ( i n t maxNbElements )
{
t h i s . maxNbElements = maxNbElements ;
}
public T g e t F i r s t ( ) throws E m p t y F i l e E x c e p t i o n
{
return f i r s t . g e t D a t a ( ) ;
}
public boolean i s F u l l ( )
{
return nbElements == maxNbElements ;
}
}
104
import j a v a . u t i l . Random ;
public P h i l o s o p h e s ( )
{
c o u v e r t s = new Couvert [ n b P l a c e s ] ;
f o r ( i n t i = 0 ; i < n b P l a c e s ; i ++)
c o u v e r t s [ i ] = new Couvert ( i ) ;
p l a c e s = new P l a c e [ n b P l a c e s ] ;
f o r ( i n t i = 0 ; i < n b P l a c e s ; i ++)
p l a c e s [ i ] = new P l a c e ( i , c o u v e r t s [ i ] ,
c o u v e r t s [ ( i + 1)% n b P l a c e s ] ) ;
}
class Place
{
private Couvert c o u v e r t G a u c h e ;
private Couvert c o u v e r t D r o i t ;
private Philosophe philosophe ;
private int i n d i c e ;
void o c c u p e r ( P h i l o s o p h e p h i l o s o p h e )
{
this . philosophe = philosophe ;
System . o u t . p r i n t l n ( p h i l o s o p h e + ” a p r i s la ” + this ) ;
}
void l i b e r e r ( )
{
System . o u t . p r i n t l n ( p h i l o s o p h e + ” a l i b e r e la ” + this ) ;
philosophe = null ;
}
void manger ( )
{
synchronized ( c o u v e r t G a u c h e )
{
c o u v e r t G a u c h e . prend ( t h i s ) ;
synchronized ( c o u v e r t D r o i t )
{
c o u v e r t D r o i t . prend ( t h i s ) ;
System . o u t . p r i n t l n ( p h i l o s o p h e +
” commence a manger a ” + t h i s ) ;
try { Thread . s l e e p ( 5 0 0 0 ) ; }
catch ( E x c e p t i o n e ) { }
System . o u t . p r i n t l n ( p h i l o s o p h e +
” a f i n i de manger a ” + t h i s ) ;
105
couvertDroit . repose ( this ) ;
}
couvertGauche . r e p o s e ( this ) ;
}
}
public S t r i n g t o S t r i n g ( )
{
return ” p l a c e ” + i n d i c e + ” ” ;
}
}
c l a s s Couvert
{
private i n t i n d i c e ;
public Couvert ( i n t i n d i c e )
{
this . i n d i c e = i n d i c e ;
}
void prend ( P l a c e p l a c e )
{
System . o u t . p r i n t l n ( t h i s + ” p r i s p a r ” + p l a c e ) ;
}
void r e p o s e ( P l a c e p l a c e )
{
System . o u t . p r i n t l n ( t h i s + ” r e p o s e p a r ” + p l a c e ) ;
}
public S t r i n g t o S t r i n g ( )
{
return ” c o u v e r t ” + i n d i c e + ” ” ;
}
}
c l a s s P h i l o s o p h e extends Thread
{
private P l a c e p l a c e ;
private i n t i n d i c e ;
public S t r i n g t o S t r i n g ( )
{
return ” p h i l o s o p h e ” + i n d i c e + ” ” ;
}
106