Université Constantine 2
Dr. CHAOUCHE A.-C.
Faculté des nouvelles technologies
ahmed.chaouche@univ-constantine2.dz
– Cours 4 –
Chapitre 4 : Langage Java (2/2)
Programmation orienté objet
Développement d’applications mobiles
2018/2019. Semestre 1
Université Constantine 2
Etudiants concernés
Faculté/Institut Département Niveau Spécialité
2018/2019. Semestre 1
Dr. CHAOUCHE A.-C.
Faculté des nouvelles technologies
ahmed.chaouche@univ-constantine2.dz
Développement d’applications mobiles
Licence 3 Technologies de l’information (TI)Nouvelles technologies IFA
Licence 3 Science de l’informatique (SCI)Nouvelles technologies IFA
– Cours 4 –
Chapitre 4 : Langage Java (1/2)
Programmation orienté objet
Université Constantine 2 3
Prérequis
Maîtrise de l’algorithmique
Langage JAVA
Objectifs du cours
Utiliser la bibliothèque de Java SE
Etendre la classe Object
Manipuler les collections en Java
Résumé
© Dr. Chaouche A.-C.
Université Constantine 2 4
En Java, toutes les classes héritent de la classe Object
Méthodes de la classe Object
Classe Object en Java
© Dr. Chaouche A.-C.
String toString() // représente l’objet en String
boolean equals(Object obj) // compare deux objets
Object clone() // duplique l’objet
int hashCode() // calcule un code unique
void finalize() // sert au garbage collector
Class<?>getClass() // retourne le nom de la classe
void wait() //
void notify() // servent à la concurrence des threads
void notifyAll() //
Université Constantine 2 5
Classe Object en Java
© Dr. Chaouche A.-C.
Redéfinition de la méthode toString()
A obj1 = new A("a1", "a2", "a3", "a4");
System.out.println(obj1);
Université Constantine 2 6
Classe Object en Java
© Dr. Chaouche A.-C.
Redéfinition de la méthode toString()
A obj1 = new A("a1", "a2", "a3", "a4");
System.out.println(obj1); // -> example.com.A@659e0bfd
Université Constantine 2 7
Classe Object en Java
© Dr. Chaouche A.-C.
Redéfinition de la méthode toString()
A obj1 = new A("a1", "a2", "a3", "a4");
System.out.println(obj1); // -> example.com.A@659e0bfd
System.out.println(obj1); // -> (a1,a2,a3,a4)
public class A {
...
@Override
public String toString() {
return "(" + a1 + "," + a2 + "," + a3 + "," + a4
+ ")";
}
}
Université Constantine 2 8
Classe Object en Java
© Dr. Chaouche A.-C.
Redéfinition de la méthode equals()
A obj1 = new A("a1", "a2", "a3", "a4");
A obj2 = new A("a1", "a2", "a3", "a4");
obj1.equals(obj2);
Université Constantine 2 9
Classe Object en Java
© Dr. Chaouche A.-C.
Redéfinition de la méthode equals()
A obj1 = new A("a1", "a2", "a3", "a4");
A obj2 = new A("a1", "a2", "a3", "a4");
obj1.equals(obj2); // -> false
Université Constantine 2 10
Classe Object en Java
© Dr. Chaouche A.-C.
Redéfinition de la méthode equals()
A obj1 = new A("a1", "a2", "a3", "a4");
A obj2 = new A("a1", "a2", "a3", "a4");
obj1.equals(obj2); // -> false
obj1.equals(obj2); // -> true
public class A {
@Override
public boolean equals(Object o) {
A obj = (A) o;
return (a1.equals(obj.a1) && a2.equals(obj.a2)
&& a3.equals(obj.a3) && a4.equals(obj.a4));
}
}
Université Constantine 2 11
Classe Object en Java
© Dr. Chaouche A.-C.
Redéfinition de la méthode clone()
A obj1 = new A("a1", "a2", "a3", "a4");
A obj2 = obj1;
obj2.a1 = "b1";
System.out.println(obj1);
System.out.println(obj2);
Université Constantine 2 12
Classe Object en Java
© Dr. Chaouche A.-C.
Redéfinition de la méthode clone()
A obj1 = new A("a1", "a2", "a3", "a4");
A obj2 = obj1;
obj2.a1 = "b1";
System.out.println(obj1); // -> (b1,a2,a3,a4)
System.out.println(obj2); // -> (b1,a2,a3,a4)
Université Constantine 2 13
Classe Object en Java
© Dr. Chaouche A.-C.
Redéfinition de la méthode clone()
A obj1 = new A("a1", "a2", "a3", "a4");
A obj2 = obj1;
obj2.a1 = "b1";
System.out.println(obj1);
System.out.println(obj2);
A obj2 = obj1.clone();
obj2.a1 = "b1";
System.out.println(obj1); // -> (a1,a2,a3,a4)
System.out.println(obj2); // -> (b1,a2,a3,a4)
public class A {
@Override
public Object clone() {
return new A(a1, a2, a3, a4);
}
}
Université Constantine 2 14
Structure de données permettant de regrouper un ensemble d’objets
Peut être typée ou non typée
Types de collections :
Interface java.util.Set<T> :
Collection non ordonnée
Pas de doublons
Classes : HashSet<T>, TreeSet<T>
Interface java.util.List<T> :
Collection ordonnée/séquences
L’accès par indice
Autorisation des doublons
Classes : Vector<T>, ArrayList<T>, LinkedList<T>
Les collections en Java
© Dr. Chaouche A.-C.
Université Constantine 2 15
Type de collection qui associe une clé unique K à une valeur V
Chaque clé ne peut correspondre qu’à une valeur au plus
Interface java.util.Map<K,V>
Classes : HashMap<K,V>, TreeMap<K,V>
Les dictionnaires en Java
© Dr. Chaouche A.-C.
Université Constantine 2 16
Hiérarchie des collections
© Dr. Chaouche A.-C.
Université Constantine 2 17
Méthodes de la classe ArrayList<T> (1/2)
© Dr. Chaouche A.-C.
boolean add(T e)
void add(int index, T element)
boolean addAll(Collection<? extends T> c)
T remove(int index)
boolean remove(Object o)
boolean removeAll(Collection<?> c)
T get(int index)
T set(int index, T element)
int indexOf(Object o)
void clear()
boolean contains(Object o)
int size()
boolean isEmpty()
Université Constantine 2 18
Méthodes de la classe ArrayList<T> (2/2)
© Dr. Chaouche A.-C.
boolean addAll(int index, Collection<? extends T> c)
void removeRange(int fromIndex, int toIndex)
void ensureCapacity(int minCapacity)
int lastIndexOf(Object o)
Iterator<T> iterator()
ListIterator<T> listIterator()
boolean retainAll(Collection<?> c)
List<T> subList(int fromIndex, int toIndex)
Object[] toArray()
T[] toArray(T[] a)
void trimToSize()
...
Université Constantine 2 19
La classe Object :
http://blog.paumard.org/cours/java/chap03-object-string-object.html
Les collections :
https://www.jmdoudoux.fr/java/dej/chap-collections.htm#collections-3
Quelques liens utiles
© Dr. Chaouche A.-C.
Université Constantine 2 20
Oracle, «Javadoc - Class Object,» Lien :
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html.
J. Paumard, «Java en ligne - Chapitre 3 : La classe Object,» 2016. Lien :
http://blog.paumard.org/cours/java/chap03-object-string-object.html.
Oracle, «Javadoc - Interface Collection<E>,» Lien :
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html.
Oracle, «Javadoc - Interface Map<K,V>,» Lien :
https://docs.oracle.com/javase/7/docs/api/java/util/Map.html.
Oracle, «Javadoc - Class ArrayList<E>,» Lien :
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html.
Nitin, «Java Interview Reference Guide – Collection Framework,» 18 Juillet 2014.
Lien : https://tutorials.techmytalk.com/2014/07/18/java-interview-reference-
guide-collection-framework/.
Références
© Dr. Chaouche A.-C.
Université Constantine 2 21
Chapitre 5 : Structure d’un projet
Objectifs :
Savoir organiser une application
Comprendre le cycle de vie d’une activité
Associer une vue à l’activité en gérant ses ressources (tailles des écrans
et internationalisation)
Prochain chapitre
© Dr. Chaouche A.-C.

Applications Android - cours 5 : Classe Object et Collections en Java

  • 1.
    Université Constantine 2 Dr.CHAOUCHE A.-C. Faculté des nouvelles technologies ahmed.chaouche@univ-constantine2.dz – Cours 4 – Chapitre 4 : Langage Java (2/2) Programmation orienté objet Développement d’applications mobiles 2018/2019. Semestre 1
  • 2.
    Université Constantine 2 Etudiantsconcernés Faculté/Institut Département Niveau Spécialité 2018/2019. Semestre 1 Dr. CHAOUCHE A.-C. Faculté des nouvelles technologies ahmed.chaouche@univ-constantine2.dz Développement d’applications mobiles Licence 3 Technologies de l’information (TI)Nouvelles technologies IFA Licence 3 Science de l’informatique (SCI)Nouvelles technologies IFA – Cours 4 – Chapitre 4 : Langage Java (1/2) Programmation orienté objet
  • 3.
    Université Constantine 23 Prérequis Maîtrise de l’algorithmique Langage JAVA Objectifs du cours Utiliser la bibliothèque de Java SE Etendre la classe Object Manipuler les collections en Java Résumé © Dr. Chaouche A.-C.
  • 4.
    Université Constantine 24 En Java, toutes les classes héritent de la classe Object Méthodes de la classe Object Classe Object en Java © Dr. Chaouche A.-C. String toString() // représente l’objet en String boolean equals(Object obj) // compare deux objets Object clone() // duplique l’objet int hashCode() // calcule un code unique void finalize() // sert au garbage collector Class<?>getClass() // retourne le nom de la classe void wait() // void notify() // servent à la concurrence des threads void notifyAll() //
  • 5.
    Université Constantine 25 Classe Object en Java © Dr. Chaouche A.-C. Redéfinition de la méthode toString() A obj1 = new A("a1", "a2", "a3", "a4"); System.out.println(obj1);
  • 6.
    Université Constantine 26 Classe Object en Java © Dr. Chaouche A.-C. Redéfinition de la méthode toString() A obj1 = new A("a1", "a2", "a3", "a4"); System.out.println(obj1); // -> example.com.A@659e0bfd
  • 7.
    Université Constantine 27 Classe Object en Java © Dr. Chaouche A.-C. Redéfinition de la méthode toString() A obj1 = new A("a1", "a2", "a3", "a4"); System.out.println(obj1); // -> example.com.A@659e0bfd System.out.println(obj1); // -> (a1,a2,a3,a4) public class A { ... @Override public String toString() { return "(" + a1 + "," + a2 + "," + a3 + "," + a4 + ")"; } }
  • 8.
    Université Constantine 28 Classe Object en Java © Dr. Chaouche A.-C. Redéfinition de la méthode equals() A obj1 = new A("a1", "a2", "a3", "a4"); A obj2 = new A("a1", "a2", "a3", "a4"); obj1.equals(obj2);
  • 9.
    Université Constantine 29 Classe Object en Java © Dr. Chaouche A.-C. Redéfinition de la méthode equals() A obj1 = new A("a1", "a2", "a3", "a4"); A obj2 = new A("a1", "a2", "a3", "a4"); obj1.equals(obj2); // -> false
  • 10.
    Université Constantine 210 Classe Object en Java © Dr. Chaouche A.-C. Redéfinition de la méthode equals() A obj1 = new A("a1", "a2", "a3", "a4"); A obj2 = new A("a1", "a2", "a3", "a4"); obj1.equals(obj2); // -> false obj1.equals(obj2); // -> true public class A { @Override public boolean equals(Object o) { A obj = (A) o; return (a1.equals(obj.a1) && a2.equals(obj.a2) && a3.equals(obj.a3) && a4.equals(obj.a4)); } }
  • 11.
    Université Constantine 211 Classe Object en Java © Dr. Chaouche A.-C. Redéfinition de la méthode clone() A obj1 = new A("a1", "a2", "a3", "a4"); A obj2 = obj1; obj2.a1 = "b1"; System.out.println(obj1); System.out.println(obj2);
  • 12.
    Université Constantine 212 Classe Object en Java © Dr. Chaouche A.-C. Redéfinition de la méthode clone() A obj1 = new A("a1", "a2", "a3", "a4"); A obj2 = obj1; obj2.a1 = "b1"; System.out.println(obj1); // -> (b1,a2,a3,a4) System.out.println(obj2); // -> (b1,a2,a3,a4)
  • 13.
    Université Constantine 213 Classe Object en Java © Dr. Chaouche A.-C. Redéfinition de la méthode clone() A obj1 = new A("a1", "a2", "a3", "a4"); A obj2 = obj1; obj2.a1 = "b1"; System.out.println(obj1); System.out.println(obj2); A obj2 = obj1.clone(); obj2.a1 = "b1"; System.out.println(obj1); // -> (a1,a2,a3,a4) System.out.println(obj2); // -> (b1,a2,a3,a4) public class A { @Override public Object clone() { return new A(a1, a2, a3, a4); } }
  • 14.
    Université Constantine 214 Structure de données permettant de regrouper un ensemble d’objets Peut être typée ou non typée Types de collections : Interface java.util.Set<T> : Collection non ordonnée Pas de doublons Classes : HashSet<T>, TreeSet<T> Interface java.util.List<T> : Collection ordonnée/séquences L’accès par indice Autorisation des doublons Classes : Vector<T>, ArrayList<T>, LinkedList<T> Les collections en Java © Dr. Chaouche A.-C.
  • 15.
    Université Constantine 215 Type de collection qui associe une clé unique K à une valeur V Chaque clé ne peut correspondre qu’à une valeur au plus Interface java.util.Map<K,V> Classes : HashMap<K,V>, TreeMap<K,V> Les dictionnaires en Java © Dr. Chaouche A.-C.
  • 16.
    Université Constantine 216 Hiérarchie des collections © Dr. Chaouche A.-C.
  • 17.
    Université Constantine 217 Méthodes de la classe ArrayList<T> (1/2) © Dr. Chaouche A.-C. boolean add(T e) void add(int index, T element) boolean addAll(Collection<? extends T> c) T remove(int index) boolean remove(Object o) boolean removeAll(Collection<?> c) T get(int index) T set(int index, T element) int indexOf(Object o) void clear() boolean contains(Object o) int size() boolean isEmpty()
  • 18.
    Université Constantine 218 Méthodes de la classe ArrayList<T> (2/2) © Dr. Chaouche A.-C. boolean addAll(int index, Collection<? extends T> c) void removeRange(int fromIndex, int toIndex) void ensureCapacity(int minCapacity) int lastIndexOf(Object o) Iterator<T> iterator() ListIterator<T> listIterator() boolean retainAll(Collection<?> c) List<T> subList(int fromIndex, int toIndex) Object[] toArray() T[] toArray(T[] a) void trimToSize() ...
  • 19.
    Université Constantine 219 La classe Object : http://blog.paumard.org/cours/java/chap03-object-string-object.html Les collections : https://www.jmdoudoux.fr/java/dej/chap-collections.htm#collections-3 Quelques liens utiles © Dr. Chaouche A.-C.
  • 20.
    Université Constantine 220 Oracle, «Javadoc - Class Object,» Lien : https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html. J. Paumard, «Java en ligne - Chapitre 3 : La classe Object,» 2016. Lien : http://blog.paumard.org/cours/java/chap03-object-string-object.html. Oracle, «Javadoc - Interface Collection<E>,» Lien : https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html. Oracle, «Javadoc - Interface Map<K,V>,» Lien : https://docs.oracle.com/javase/7/docs/api/java/util/Map.html. Oracle, «Javadoc - Class ArrayList<E>,» Lien : https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html. Nitin, «Java Interview Reference Guide – Collection Framework,» 18 Juillet 2014. Lien : https://tutorials.techmytalk.com/2014/07/18/java-interview-reference- guide-collection-framework/. Références © Dr. Chaouche A.-C.
  • 21.
    Université Constantine 221 Chapitre 5 : Structure d’un projet Objectifs : Savoir organiser une application Comprendre le cycle de vie d’une activité Associer une vue à l’activité en gérant ses ressources (tailles des écrans et internationalisation) Prochain chapitre © Dr. Chaouche A.-C.