Correction TP Généricité Collection

Page 1 sur 9Lecteur de document UniversityLib

Correction TP Généricité Collection

Computer Science - Object-Oriented Programming · lab

Browse all programmation documents

Correction TP G n ricit collection

Exercice 1 : Pile g n rique en repr sentation cha n e

package PileGen;

public interface PileGen<T>{

public boolean estVide();

public T dernier();

public void depiler();

public void empiler(T o);

}

package PileGen;

public class Noeud <T> {

T info;

Noeud <T> suivant;

}

package PileGen;

class PileListe <T> implements PileGen<T>{

private Noeud <T> sommet;

public PileListe(){

sommet = null;

}

public boolean estVide(){

return (sommet == null);

}

public T dernier(){

return sommet.info;

}

public void empiler(T o){

Noeud<T> n = new Noeud<T>();

n.info = o;

n.suivant = sommet;

sommet = n;

}

public void depiler(){

sommet = sommet.suivant;

}

}

1

package PileGen;

class TestPile{

public static void main(String[]args){

PileListe<Integer> p = new PileListe<Integer>();

for(int i = 0 ; i < 10 ; i++)

p.empiler(new Integer(i));

while(!p.estVide()){

System.out.println(p.dernier());

p.depiler();

}

}}

Exercice 2 : Pile et collection g n rique (ArrayList)

package PileTab;

public class PileTab {

int pos;

int [] tab;

public PileTab(){

tab = new int[4];

this.pos = 0;

}

public void add(int e){

if (this.pos == tab.length){

System.out.println("Ajout impossible de "+e);

//System.exit(-1);

}

else {

tab = e;

this.pos ++;

}

}

public int remove(){

int elem = tab ;

this.pos --;

return elem;

}

public boolean estVide(){

return this.pos == 0;

}

Advertisement

public int size(){

return this.pos;

}

2

public void affiche(){

for (int i = 0; i<this.pos; i++){

System.out.println(this.tab );

}

}

public int getSommet(){

return this.tab ;

}

}

package PileTab;

import java.util.ArrayList;

import java.util.Iterator;

public class TruePileGen<T> {

ArrayList<T> tab;

public TruePileGen(){

tab = new ArrayList<T>();

}

public TruePileGen(ArrayList<T> a){

this.tab = a;

}

public void add(T e){

tab.add(e) ;

}

public T remove(){

T elem = this.tab.get(this.tab.size()-1);

this.tab.remove(this.tab.size()-1);

return elem;

}

public boolean estVide(){

return this.tab.size() == 0;

}

public int size(){

return this.tab.size();

}

public void affiche(){

Iterator<T> ite = this.tab.iterator();

while (ite.hasNext())

System.out.println(ite.next().toString());

/*

for (int i = 0; i<this.tab.size(); i++){

System.out.println(this.tab.get(i).toString());

3

}

*/

/*

for (T t:this.tab) System.out.println(t.toString());

*/

}

public T getSommet(){

return this.tab.get(this.tab.size()-1);

}

}

package PileTab;

import java.awt.Point;

public class Test {

public static void main(String[]args){

/*PileTab pt=new PileTab();

pt.add(1);

pt.add(2);

pt.add(3);

while(!pt.estVide()){

System.out.println(pt.getSommet());

pt.remove();*/

/*System.out.println("1 - Pile d entier non generique :");

PileTab p = new PileTab();

System.out.println("--- Add");

p.add(0);

p.add(1);

p.add(2);

p.add(3);

p.add(4);

Advertisement

System.out.println("--- Pile");

p.affiche();

System.out.println("--- Remove");

System.out.println("On supprime :"+p.remove());

System.out.println("--- Pile");

p.affiche();

System.out.println("--- Get sommet");

System.out.println("Sommet ="+p.getSommet());

System.out.println();

*/

System.out.println("2 - Pile generique

parametree par le type Point :");

TruePileGen<Point>();

TruePileGen<Point> p2 = new

System.out.println("--- Add");

Point a = new Point(10,11);

Point b = new Point(12,13);

Point c = new Point(14,15);

Point d = new Point(16,17);

p2.add(a);

p2.add(b);

p2.add(c);

4

p2.add(d);

System.out.println("--- Pile");

p2.affiche();

System.out.println("--- Remove");

System.out.println("On supprime :"+p2.remove());

System.out.println("--- Pile");

p2.affiche();

System.out.println("--- Get sommet");

System.out.println("Sommet ="+p2.getSommet());

}

}

Exercice 3 : Pile et collection g n rique (LinkedList)

public interface IPile <A> {

boolean estVide();

void empile(A a);

A depile(); // retourne l' l ment en sommet de pile et d pile

int nbElements();

A sommet(); // retourne le sommet de pile mais ne le d pile pas

}

import java.util.LinkedList;

public class CPile <A> implements IPile <A>{

LinkedList <A> l;

public CPile(){

l=new LinkedList<A>();

}

public boolean estVide(){

return l.size()==0;

}

public void empile(A a){

l.addFirst(a);

}

public A depile(){

// retourne l' l ment en sommet de pile et d pile

return l.removeFirst();

}

public int nbElements(){

return l.size();

}

public A sommet(){// retourne le sommet de pile mais ne le d pile

pas

}

return l.peek();

5

public class Test {

public static void main(String[] args) {

// TODO Auto-generated method stub

CPile<Integer> p=new CPile <>();

p.empile(1);

p.empile(2);

p.empile(3);

System.out.println(p.sommet());

System.out.println(p.depile());

Advertisement

System.out.println(p.depile());

System.out.println(p.depile());

}}

Exercice 4 : Compr hension de larchitecture des t ches

package TacheGen;

public interface Tache {

/* Obtenir le nom de la t che. /

String getNom();

/* Obtenir le co t de la t che. /

int getCout();

}

package TacheGen;

public class TacheElementaire implements Tache {

String nom;

int cout;

public TacheElementaire(String nom, int cout) {

this.nom = nom;

this.cout = cout;

}

public String getNom() {

return nom;

}

public int getCout() {

return cout;

}

}

package TacheGen;

6

import java.util.ArrayList;

import java.util.Collection;

public class TacheComplexe implements Tache{

private ArrayList <Tache> tabSousTaches;

String nom;

public TacheComplexe(String nom){

this.nom=nom;

tabSousTaches=new ArrayList <Tache>();

}

public String getNom() {

return this.nom;

}

public void ajouter(Tache t) {

tabSousTaches.add(t);

}

public void supprimer(Tache t) {

tabSousTaches.remove(t);

}

public int getCout() {

int res=0;

for (Tache t : tabSousTaches) {

res += t.getCout();

}

return res;

}

}

package TacheGen;

public class Test {

public static void main(String[] args) {

// TODO Auto-generated method stub

TacheComplexe tc=new TacheComplexe("Tache Complexe 1 : ");

tc.ajouter(new TacheElementaire("a",10));

tc.ajouter(new TacheElementaire("b",20));

tc.ajouter(new TacheElementaire("c",30));

System.out.println("Cout tache complexe 1 : "+tc.getCout());

TacheComplexe tc2=new TacheComplexe("Tache Complexe 2 : ");

tc2.ajouter(tc);

tc.ajouter(new TacheElementaire("d",40));

System.out.println("Cout tache complexe 2 : "+tc.getCout());

}}

7

Exercice : Annuaire

public class Fiche {

int numero;

String nom;

String adresse;

public Fiche(String s, int n, String a){

Advertisement

this.numero = n;

this.nom = s;

this.adresse = a;

}

public Fiche(String s){

this.nom = s;

// Les autres champs sont initialises par defaut a -1

(numero) et null (adresse)

}

}

import java.util.HashMap;

import java.util.Map;

public class Annuaire {

private Map<String,Fiche> contacts ;

public Annuaire(){

this.contacts = new HashMap<String,Fiche>();

}

// Retourne le nombre de contacts = nb de cles dans l annuaire

public int getNbcontacts(){

return this.contacts.size();

}

// Ajout a partir d une fiche

public void addContact(Fiche f){

this.contacts.put(f.nom, f);

}

// Ajout a partir d un nom, numero et adresse

public void addContact(String s, int n, String a){

this.contacts.put(s, new Fiche(s,n,a));

}

// Ajout a partir d un nom (numero et adresse par defaut)

public void addContact(String s){

8

this.contacts.put(s, new Fiche(s));

}

// Retourne le numero de telephone associe a un nom

// + propre : en faire une methode privee et faire une autre

methode qui print un message d erreur si rien trouve

public int getnumero(String name){

int trouve = -1;

for (String k:this.contacts.keySet()){

//if (k.equals(name)){

if (k.equalsIgnoreCase(name)){

trouve = this.contacts.get(k).numero;

}

}

return trouve;

}

// Affichage des contacts dans l annuaire

public void affiche(){

for (Map.Entry<String, Fiche> e:this.contacts.entrySet()){

System.out.println(e.getKey()+" :

numero="+e.getValue().numero+" ; adresse="+e.getValue().adresse);

}

}

}

System.out.println("\n---- Exercice 2.3 sur les HashMap");

Annuaire ann = new Annuaire();

ann.addContact(new Fiche("Hollande", 42, "Elysee, Paris"));

ann.addContact(new Fiche("Casimir", 99, "Ile aux Enfants,

QuelquePart"));

ann.addContact(new Fiche("CapitainFlam", 18, "Espace,

loin"));

System.out.println("-- Nombre de contacts =

"+ann.getNbcontacts());

// Affichage

System.out.println("-- Affichage des contacts :");

ann.affiche();

// Recuperation numero d un contact (ici utilisation d une

methode ignorant la casse : majuscule ignoree

System.out.println("-- Le numero de Casimir est :

"+ann.getnumero("casimir"));

9