Correction md2 fabien orienté object

37
Correction des exercices de programmation objet Fabien Napolitano 19 d´ ecembre 2000 1 Exercices en mode texte Exercice 1.1 : public class Chronometre { private long time ; public Chronometre() {} public void start() { time=System.currentTimeMillis() ; } public long stop() { return System.currentTimeMillis()-time ; } } ————————– Exercice 1.2 : public class Date implements Comparable { // nombre de jours depuis le 1er Janvier 4713 avant JC private int numeroJulien ; // noms des mois de l’annee private static String[] mois={"janvier","fevrier","mars","avril","mai","juin", "juillet","aout","septembre","octobre","novembre","decembre"} ; // noms des jours de la semaine private static String[] jour={"dimanche","lundi","mardi","mercredi","jeudi", "vendredi","samedi"} ; // constructeur avec le numero Julien public Date(int n) { numeroJulien=n ; } // constructeur avec la date sous la forme classique public Date(int jour,int mois,int annee) { int a=(14-mois)/12 ; int y=annee+4800-a ; int m=mois+12*a-3 ; // numeroJulien=jour+(153*m+2)/5+y*365+y/4-y/100+y/400-32045 ; }

description

 

Transcript of Correction md2 fabien orienté object

Page 1: Correction md2 fabien orienté object

Correction des exercices de programmation objet

Fabien Napolitano

19 decembre 2000

1 Exercices en mode texte

Exercice 1.1 :

public class Chronometre {

private long time ;

public Chronometre() {}

public void start() {

time=System.currentTimeMillis() ;

}

public long stop() {

return System.currentTimeMillis()-time ;

}

}

————————–

Exercice 1.2 :

public class Date implements Comparable {

// nombre de jours depuis le 1er Janvier 4713 avant JC

private int numeroJulien ;

// noms des mois de l’annee

private static String[] mois={"janvier","fevrier","mars","avril","mai","juin",

"juillet","aout","septembre","octobre","novembre","decembre"} ;

// noms des jours de la semaine

private static String[] jour={"dimanche","lundi","mardi","mercredi","jeudi",

"vendredi","samedi"} ;

// constructeur avec le numero Julien

public Date(int n) {

numeroJulien=n ;

}

// constructeur avec la date sous la forme classique

public Date(int jour,int mois,int annee) {

int a=(14-mois)/12 ;

int y=annee+4800-a ;

int m=mois+12*a-3 ;

//

numeroJulien=jour+(153*m+2)/5+y*365+y/4-y/100+y/400-32045 ;

}

Page 2: Correction md2 fabien orienté object

public boolean equals(Object obj) {

if (!(obj instanceof Date)) return false ;

Date date=(Date)obj ;

return (numeroJulien==date.numeroJulien) ;

}

public int compareTo(Object obj) {

// provoque une erreur si obj n’est pas de type date

Date date=(Date)obj ;

return (numeroJulien-date.numeroJulien) ;

}

public void next() { // jour suivant

numeroJulien++ ;

}

public void previous() { // jour precedenf

numeroJulien-- ;

}

public int[] getUsualForm() {

int a = numeroJulien + 32044 ;

int b = (4*a+3)/146097 ;

int c = a - (b*146097)/4 ;

int d = (4*c+3)/1461 ;

int e = c - (1461*d)/4 ;

int m = (5*e+2)/153 ;

int[] jma=new int[3] ;

jma[0]=e-(153*m+2)/5+1 ;

jma[1]=m+3-12*(m/10) ;

jma[2]=b*100+d-4800+m/10 ;

return jma ;

}

public int getJour() {

return getUsualForm()[0] ;

}

public int getMois() {

return getUsualForm()[1] ;

}

public int getAnnee() {

return getUsualForm()[2] ;

}

public String nomDuJour() {

// 0 pour dimanche, 1 pour lundi, ....

int n=(numeroJulien-1720977)%7 ;

return jour[n] ;

}

public String nomDuMois() {

return mois[getMois()-1] ;

}

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 2

Page 3: Correction md2 fabien orienté object

public String toString() {

return nomDuJour()+" "+getJour()+" "+

nomDuMois()+" "+getAnnee() ;

}

}

————————–

Exercice 1.3 :

import java.util.Random ;

public class De {

private static Random aleatoire ;

private int value ;

public De() {

// creation d’une instance de Random si ca n’est pas

// deja fait

if (aleatoire==null) {

aleatoire=new Random(System.currentTimeMillis()) ;

}

roll() ;

}

public void roll() {

value=aleatoire.nextInt(6)+1 ;

}

public int getValue() {

return value ;

}

}

————————–

Exercice 1.4 :

public class Random {

private final static int A=48271 ;

private final static int M=2147483647 ; // =2^31-1

private final static int Q=M/A ;

private final static int R=M%A ;

private int etat ; // dernier nombre aleatoire

public Random(int seed) {

etat=seed ;

}

public int nextInt() {

etat=A*(etat%Q)-R*(etat/Q) ;

if (etat<0)

etat+=M ;

return etat ;

}

public int nextInt(int n) {

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 3

Page 4: Correction md2 fabien orienté object

return (nextInt()%n) ;

}

}

————————–

Exercice 1.5 :

————————–

Exercice 1.6 :

public class Caractere {

private static int hauteur=8 ;

private static int largeur=8 ;

private int[] representation ;

public Caractere() {

representation=new int[largeur*hauteur] ;

}

public Caractere(String s) {

representation=new int[largeur*hauteur] ;

for (int i=0;i<representation.length;i++)

if (s.charAt(i)==’ ’)

representation[i]=-1 ;

else

representation[i]=1 ;

}

public int[] getVector() {

return representation ;

}

public boolean equals(Object obj) {

if (!(obj instanceof Caractere)) return false ;

Caractere c=(Caractere)obj ;

for (int i=0;i<representation.length;i++)

if (representation[i]!=c.representation[i]) return false ;

return true ;

}

public String toString() {

StringBuffer sb=new StringBuffer() ;

for (int i=0;i<representation.length;i++) {

// passage a la ligne

if ((i%largeur)==0)

sb.append(’\n’) ;

if (representation[i]==-1)

sb.append(’ ’) ;

else

sb.append(’*’) ;

}

return sb.toString() ;

}

}

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 4

Page 5: Correction md2 fabien orienté object

————————–

public class Hopfield {

private int memory=0 ;

// on suppose que les Caractere sont de taille 8*8

private int[][] coefficients=new int[64][64] ;

// variable aleatoire utile dans la methode evalue

private static Random alea=new Random((int)System.currentTimeMillis()) ;

// le constructeur est implicite

public boolean learn(Caractere c) {

memory++ ;

if (memory>0.15*64) return false ;

int[] tab=c.getVector() ;

for (int i=1;i<64;i++)

for (int j=1;j<64;j++)

if (i!=j)

coefficients[i][j]+=tab[i]*tab[j] ;

return true ;

}

public Caractere evolue(Caractere c) {

int[] tab=c.getVector() ;

int i=alea.nextInt(64) ;

int somme=0 ;

for (int j=1;j<64;j++)

somme+=coefficients[i][j]*tab[j] ;

if (somme>0)

tab[i]=1 ;

if (somme<0)

tab[i]=-1 ;

// agit par effet de bord sur c

return c ;

}

public static void main(String[] arg) {

// Caracteres que l’on apprend au reseau

Caractere a=new Caractere(

" ** "+

" **** "+

" ** ** "+

" ** ** "+

" ****** "+

"********"+

"*** ***"+

"*** ***") ;

Caractere x=new Caractere(

"* *"+

"** **"+

" ** ** "+

" **** "+

" **** "+

" ** ** "+

"** **"+

"* *") ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 5

Page 6: Correction md2 fabien orienté object

Caractere y=new Caractere(

"* *"+

"** **"+

" ** ** "+

" **** "+

" ** "+

" ** "+

" ** "+

" ** ") ;

Caractere z=new Caractere(

"********"+

"********"+

" **"+

" ** "+

" ** "+

"** "+

"********"+

"********") ;

Hopfield reseau=new Hopfield() ;

reseau.learn(a) ;

reseau.learn(x) ;

reseau.learn(y) ;

reseau.learn(z) ;

// caractere a reconnaitre

Caractere aBruite=new Caractere(

" ** "+

" **** "+

" ** ** "+

" * ** "+

" *** ** "+

"********"+

"*** ***"+

"*** *") ;

while (!aBruite.equals(a)) {

System.out.println() ;

aBruite=reseau.evolue(aBruite) ;

System.out.println(aBruite) ;

}

System.out.println(a) ;

}

}

————————–

2 Exercices dur les structures de donnees

Exercice 2.1 :

public class CaseListeChainee {

private Object contenu ;

private CaseListeChainee caseSuivante ;

private CaseListeChainee casePrecedente ;

public CaseListeChainee(Object obj) {

contenu=obj ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 6

Page 7: Correction md2 fabien orienté object

}

public CaseListeChainee(Object obj,CaseListeChainee s) {

contenu=obj ;

if (s!=null)

setCaseSuivante(s) ;

}

public Object getContenu() {

return contenu ;

}

public void setContenu(Object obj) {

contenu=obj ;

}

public CaseListeChainee getCaseSuivante() {

return caseSuivante ;

}

public CaseListeChainee getCasePrecedente() {

return casePrecedente ;

}

public void setCaseSuivante(CaseListeChainee suivante) {

if (caseSuivante!=null)

caseSuivante.casePrecedente=null ;

caseSuivante=suivante ;

if (caseSuivante==null) return ;

if (caseSuivante.casePrecedente!=null)

caseSuivante.casePrecedente.caseSuivante=null ;

caseSuivante.casePrecedente=this ;

}

public void setCasePrecedente(CaseListeChainee precedente) {

if (casePrecedente!=null)

casePrecedente.caseSuivante=null ;

casePrecedente=precedente ;

if (casePrecedente==null) return ;

if (casePrecedente.caseSuivante!=null)

casePrecedente.caseSuivante.casePrecedente=null ;

casePrecedente.caseSuivante=this ;

}

public boolean equals(Object obj) {

if (!(obj instanceof CaseListeChainee)) return false ;

CaseListeChainee c=(CaseListeChainee)obj ;

return (c.getContenu().equals(getContenu())) ;

}

}

————————–

public class ListeChainee {

private CaseListeChainee premiere ;

private CaseListeChainee derniere ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 7

Page 8: Correction md2 fabien orienté object

private int size=0 ;

public ListeChainee() {}

public int size() {

return size ;

}

public boolean isEmpty() {

return size==0 ;

}

public void addFirst(Object obj) {

// l’ancienne premiere devient la deuxieme

premiere=new CaseListeChainee(obj,premiere) ;

// si la liste etait vide la premiere coincide

// maintenant avec la derniere

if (size==0)

derniere=premiere ;

size++ ;

}

public void addLast(Object obj) {

if (size!=0) {

derniere.setCaseSuivante(new CaseListeChainee(obj)) ;

derniere=derniere.getCaseSuivante() ;

}

else {

derniere=new CaseListeChainee(obj) ;

premiere=derniere ;

}

size++ ;

}

public Object getFirst() {

if (!isEmpty())

return premiere.getContenu() ;

return null ;

}

public Object getLast() {

if (!isEmpty())

return derniere.getContenu() ;

return null ;

}

public Object removeFirst() {

Object obj=null ;

if (!isEmpty()) {

obj=premiere.getContenu() ;

size-- ;

if (isEmpty()) {

derniere=null ;

premiere=null ;

}

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 8

Page 9: Correction md2 fabien orienté object

else {

premiere=premiere.getCaseSuivante() ;

premiere.setCasePrecedente(null) ;

}

}

return obj ;

}

public Object removeLast() {

Object obj=null ;

if (!isEmpty()) {

obj=derniere.getContenu() ;

size-- ;

if (isEmpty()) {

derniere=null ;

premiere=null ;

}

else {

derniere=derniere.getCasePrecedente() ;

derniere.setCaseSuivante(null) ;

}

}

return obj ;

}

public Object get(int index) {

if (index>=size) return null ;

CaseListeChainee temp=premiere ;

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

temp=temp.getCaseSuivante() ;

return temp.getContenu() ;

}

public Object set(int index,Object obj) {

if (index>=size) return null ;

CaseListeChainee temp=premiere ;

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

temp=temp.getCaseSuivante() ;

Object old=temp.getContenu() ;

temp.setContenu(obj) ;

return old ;

}

// ajoute l’objet obj a la position index

public void add(int index,Object obj) {

if (index<=0) {

addFirst(obj) ;

return ;

}

if (index>size) {

addLast(obj) ;

return ;

}

CaseListeChainee nouvelle=new CaseListeChainee(obj) ;

CaseListeChainee temp1=premiere ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 9

Page 10: Correction md2 fabien orienté object

CaseListeChainee temp2=temp1.getCaseSuivante() ;

for (int i=0;i<index-1;i++) {

temp1=temp2 ;

temp2=temp2.getCaseSuivante() ;

}

temp1.setCaseSuivante(nouvelle) ;

nouvelle.setCaseSuivante(temp2) ;

size++ ;

}

public String toString() {

StringBuffer sb=new StringBuffer("|") ;

CaseListeChainee temp=premiere ;

while (temp!=null) {

sb.append(temp.getContenu()) ;

sb.append(’|’) ;

temp=temp.getCaseSuivante() ;

}

return sb.toString() ;

}

public boolean equals(Object obj) {

if (!(obj instanceof ListeChainee)) return false ;

ListeChainee list=(ListeChainee)obj ;

if (size()!=list.size()) return false ;

CaseListeChainee temp1=premiere ;

CaseListeChainee temp2=list.premiere ;

while (temp1!=null) {

temp1=temp1.getCaseSuivante() ;

temp2=temp2.getCaseSuivante() ;

if (!temp1.equals(temp2))

return false ;

}

return true ;

}

public boolean contains(Object obj) {

CaseListeChainee temp=premiere ;

while (temp!=null) {

if (obj.equals(temp.getContenu())) return true ;

temp=temp.getCaseSuivante() ;

}

return false ;

}

public Object[] toArray() {

Object[] tab=new Object[size()] ;

CaseListeChainee temp=premiere ;

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

tab[i]=temp.getContenu() ;

temp=temp.getCaseSuivante() ;

}

return tab ;

}

}

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 10

Page 11: Correction md2 fabien orienté object

————————–

Exercice 2.2 :

Cf. section 5.

Exercice 2.3 :

public class Pile {

private ListeChainee contenu ;

public Pile() {

contenu=new ListeChainee() ;

}

public void push(Object obj) {

contenu.addFirst(obj) ;

}

public Object peek() {

return contenu.getFirst() ;

}

public Object pop() {

return contenu.removeFirst() ;

}

public boolean empty() {

return contenu.isEmpty() ;

}

public int size() {

return contenu.size() ;

}

public String toString() {

return contenu.toString() ;

}

}

————————–

public class CalculatriceRPN {

private Pile nombres ;

// registre

// (utiles pour simplifier la programmation)

private int r1 ;

private int r2 ;

public CalculatriceRPN() {

nombres=new Pile() ;

}

public void enter(int n) {

// place n au sommet de la pile

nombres.push(new Integer(n)) ;

// affiche la pile

System.out.println(this) ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 11

Page 12: Correction md2 fabien orienté object

}

// met les deux valeurs au sommet de la pile dans r1 et r2

// renvoie true si tout c’est bien passe

private boolean setRegister() {

if (nombres.size()<2) {

System.out.println("??? error ???") ;

return false ;

}

r1=((Integer)nombres.pop()).intValue() ;

r2=((Integer)nombres.pop()).intValue() ;

return true ;

}

// operations arithmetiques

public void add() {

if (setRegister())

enter(r1+r2) ;

}

public void sub() {

if (setRegister())

enter(r1-r2) ;

}

public void times() {

if (setRegister())

enter(r1*r2) ;

}

public void quotient() {

if (setRegister())

enter(r1/r2) ;

}

public void reste() {

if (setRegister())

enter(r1%r2) ;

}

// affiche le contenu de la pile

public String toString() {

return nombres.toString() ;

}

// methode utile pour execute

private static boolean isDigit(char c) {

return (’0’<=c && c<=’9’) ;

}

// execution automatique d’une suite d’instructions

// en notation polonaise inversee

public void execute(String s) {

int n=0 ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 12

Page 13: Correction md2 fabien orienté object

char c ;

boolean chiffre=false ;

for (int i=0;i<s.length();i++) {

c=s.charAt(i) ;

if (isDigit(c)) {

n*=10 ;

n+=c-’0’ ;

chiffre=true ;

}

else {

if (chiffre) {

chiffre=false ;

enter(n) ;

n=0 ;

}

if (c==’+’) add() ;

if (c==’-’) sub() ;

if (c==’*’) times() ;

if (c==’/’) quotient() ;

if (c==’%’) reste() ;

}

}

}

}

————————–

Exercice 2.4 :

————————–

Exercice 2.5 :

public class ArbreBinaire {

private Object racine ;

private ArbreBinaire gauche ;

private ArbreBinaire droite ;

public ArbreBinaire(Object etiquette) {

racine=etiquette ;

}

public Object getRacine() {

return racine ;

}

public ArbreBinaire getLeft() {

return gauche ;

}

public ArbreBinaire getRight() {

return droite ;

}

public void setLeft(ArbreBinaire b) {

gauche=b ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 13

Page 14: Correction md2 fabien orienté object

}

public void setRight(ArbreBinaire b) {

droite=b ;

}

public int size() {

int n=1 ;

if (getLeft()!=null) {

n+=getLeft().size() ;

}

if (getRight()!=null) {

n+=getRight().size() ;

}

return n ;

}

public int getHeight() {

int hauteur=0 ;

if (getLeft()!=null) {

hauteur=getLeft().getHeight()+1 ;

}

if (getRight()!=null) {

hauteur=Math.max(hauteur,getRight().getHeight()+1) ;

}

return hauteur ;

}

// remplace l’arbre appelant par l’arbre b

public void setTo(ArbreBinaire b) {

racine=b.racine ;

gauche=b.gauche ;

droite=b.droite ;

}

// methode necessaire pour toString()

private String toString(String decalage) {

String s=decalage+getRacine().toString()+"\n" ;

decalage+=’ ’ ;

if (getLeft()!=null)

s+=getLeft().toString(decalage) ;

if (getRight()!=null)

s+=getRight().toString(decalage) ;

return s ;

}

public String toString() {

return toString(new String()) ;

}

}

————————–

Exercice 2.6 :

import dauphine.util.* ; // utile pour la saisie des reponses

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 14

Page 15: Correction md2 fabien orienté object

public class Animals {

private ArbreBinaire cerveau ;

public Animals() {

// question initiale

cerveau=questionTree("est ce un mamifere ?","ours","boa") ;

}

public static ArbreBinaire questionTree(String question,String oui,String non) {

ArbreBinaire q=new ArbreBinaire(question) ;

ArbreBinaire o=new ArbreBinaire(oui) ;

ArbreBinaire n=new ArbreBinaire(non) ;

q.setLeft(o) ;

q.setRight(n) ;

return q ;

}

public static boolean saisieOuiOuNon() {

String reponse ;

do {

System.out.print("oui/non:") ;

reponse=StandardInput.readString() ;

}

while (reponse.charAt(0)!=’o’ && reponse.charAt(0)!=’n’) ;

if (reponse.charAt(0)==’o’) return true ;

else return false ;

}

public void play() {

ArbreBinaire questionsReponses=cerveau ;

while (questionsReponses.size()>1) {

System.out.println(questionsReponses.getRacine()) ;

if (saisieOuiOuNon()) // la reponse est oui

questionsReponses=questionsReponses.getLeft() ;

else

questionsReponses=questionsReponses.getRight() ;

}

String reponseOrdinateur=(String)questionsReponses.getRacine() ;

System.out.println("vous pensez a "+reponseOrdinateur+" ?") ;

if (saisieOuiOuNon()) {

System.out.println("J’AI GAGNE!!!") ;

return ;

}

System.out.println("vous avez gagne. A quoi pensiez vous ?") ;

String reponseCorrecte=StandardInput.readLine() ;

System.out.println("donnez une question dont la re-

ponse sera oui pour "+

reponseCorrecte+" et non pour "+reponseOrdinateur) ;

String question=StandardInput.readLine() ;

questionsReponses.setTo(questionTree(question,reponseCorrecte,

reponseOrdinateur)) ;

}

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 15

Page 16: Correction md2 fabien orienté object

public static void main(String[] arg) {

Animals a=new Animals() ;

StandardInput.startConsole() ;

do {

a.play() ;

System.out.println("voulez vous rejouer ?") ;

}

while (saisieOuiOuNon()) ;

}

}

————————–

3 Remarque sur la classe BufferCanvas

La classe BufferCanvas donnee dans l’enonce fonctionne tres bien avec Java 2 mais pas du tout avec Java1. Etant donne que les ordinateurs du cryo utilisent par defaut Java 1 voici une programmation simplifieede cette classe fonctionnant avec Java 1 (neammoins la classe originale est bien meilleure si vous avez Java2). La classe Viewer reste inchangee.

import java.awt.Graphics ;

import java.awt.Image ;

import java.awt.Color ;

import java.awt.Canvas ;

// classe BufferCanvas pour java version 1.1

// l’image n’est plus bufferisee

public class BufferCanvas extends Canvas {

private Graphics graph ;

public BufferCanvas(int largeur,int hauteur) {}

public void initialize() {

graph=this.getGraphics() ;

setColor(Color.black) ;

fillRect(0,0,getWidth(),getHeight()) ;

}

public void update(Graphics g) {

paint(g) ;

}

public void setColor(Color c) {

graph.setColor(c) ;

}

public void drawPixel(int x,int y) {

graph.drawLine(x,y,x,y) ;

}

public void drawString(String s,int x,int y) {

graph.drawString(s,x,y) ;

}

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 16

Page 17: Correction md2 fabien orienté object

public void drawLine(int x1,int y1,int x2,int y2) {

graph.drawLine(x1,y1,x2,y2) ;

}

public void fillRect(int x1,int y1,int largeur,int hauteur) {

graph.fillRect(x1,y1,largeur,hauteur) ;

}

public void paint(Graphics g) {

}

public void repaint() {

Graphics g=this.getGraphics() ;

paint(g) ;

}

}

————————–

4 Exercices utilisant les graphiques

Exercice 4.1 :

public class Complexe {

private double re ; // partie reelle

private double im ; // partie imaginaire

// constante de classe

public final static Complexe I=new Complexe(0,1) ;

public Complexe() {} // zero

public Complexe(double d) { // nombre reel

re=d ;

im=0 ;

}

public Complexe(double r,double i) {

re=r ;

im=i ;

}

// renvoie la partie reelle

public double getRe() {

return re ;

}

// renvoie la partie imaginaire

public double getIm() {

return im ;

}

// modifie la partie reelle

public void setRe(double d) {

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 17

Page 18: Correction md2 fabien orienté object

re=d ;

}

// modifie la partie imaginaire

public void setIm(double d) {

im=d ;

}

public Complexe add(Complexe c) { // renvoie this+c

return new Complexe(re+c.re,im+c.im) ;

}

public Complexe times(Complexe c) { // renvoie this*c

return new Complexe(re*c.re-im*c.im,re*c.im+im*c.re) ;

}

public double module() {

return Math.sqrt(re*re+im*im) ;

}

public boolean equals(Object obj) {

if (obj instanceof Complexe) {

Complexe c=(Complexe)obj ;

return (c.re==re && c.im==im) ;

}

else return false ;

}

public String toString() {

return re+"+"+im+"I" ;

}

public static void main(String[] arg) {

Complexe c=I ;

System.out.println(c) ;

}

}

————————–

import java.awt.* ;

public class Mandelbrot {

// delimitations de la partie etudiee

private Complexe hautGauche=new Complexe(-2.15,1.5) ; // coin haut gauche

private Complexe basDroite=new Complexe(1,-1.5) ; // coin bas droite

// nombre d’iterations maximum

private int maxIterations=32 ;

// seuil pour la divergence

private double seuil=2 ;

// viewer

private Viewer vue ;

private double largeur ;

private double hauteur ;

// complexes utilises dans le calcul

Complexe c=new Complexe() ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 18

Page 19: Correction md2 fabien orienté object

Complexe x=new Complexe() ;

public Mandelbrot(Viewer v) {

vue=v ;

largeur=vue.getLargeur() ;

hauteur=vue.getHauteur() ;

}

public void setHautGauche(Complexe hg) {

hautGauche=hg ;

}

public void setBasDroite(Complexe bd) {

basDroite=bd ;

}

// calcul le nombre complexe correspondant aux

// coordonnees (i,j) sur le Viewer.

// le resultat est place dans c

private void calculParametre(int i,int j) {

c.setRe(hautGauche.getRe()+(i/largeur)*

(basDroite.getRe()-hautGauche.getRe())) ;

c.setIm(hautGauche.getIm()+(j/hauteur)*

(basDroite.getIm()-hautGauche.getIm())) ;

}

// remplace x par x^2+c

// le calcul n’utilise pas les methodes add et times de Complexe

// pour aller plus vite

private void nextX() {

double re=x.getRe() ;

double im=x.getIm() ;

x.setRe(re*re-im*im+c.getRe()) ;

x.setIm(2*re*im+c.getIm()) ;

}

// renvoie le nombre d’iteration au bout duquel la suite diverge

// (x_n>seuil). Si la suite n’a pas divergee apres maxIterations

// on renvoie -1

public int getDivergence() {

// x0=0

x.setRe(0) ;

x.setIm(0) ;

for (int i=0;i<maxIterations;i++) {

// x->x^2+c

nextX() ;

if (x.module()>seuil)

return i ;

}

return -1 ;

}

public void dessine() {

int iter ;

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

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 19

Page 20: Correction md2 fabien orienté object

for (int j=0;j<hauteur;j++) {

calculParametre(i,j) ;

iter=getDivergence() ;

vue.setColor(getColor(iter)) ;

vue.drawPixel(i,j) ;

}

vue.repaint() ;

}

private Color getColor(int iter) {

if (iter==-1) return Color.black ;

int red=110-(int)(iter*110.0/maxIterations) ;

int green=red ;

int blue=255 ;

return new Color(red,green,blue) ;

}

public static void main(String[] arg) {

Viewer vue=new Viewer(300,300) ;

Mandelbrot m=new Mandelbrot(vue) ;

m.dessine() ;

}

}

————————–

Exercice 4.2 :

import java.awt.* ;

public class Julia {

// delimitations de la partie etudiee

private Complexe hautGauche=new Complexe(-1.7,1.5) ; // coin haut gauche

private Complexe basDroite=new Complexe(1.7,-1.5) ; // coin bas droite

// nombre d’iterations maximum

private int maxIterations=128 ;

// seuil pour la divergence

private double seuil=2 ;

// viewer

private Viewer vue ;

private double largeur ;

private double hauteur ;

// complexes utilises dans le calcul

Complexe c=new Complexe() ;

Complexe x=new Complexe() ;

Complexe parametre=new Complexe(-0.7795,0.134) ;

public Julia(Viewer v) {

vue=v ;

largeur=vue.getLargeur() ;

hauteur=vue.getHauteur() ;

}

public void setHautGauche(Complexe hg) {

hautGauche=hg ;

}

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 20

Page 21: Correction md2 fabien orienté object

public void setBasDroite(Complexe bd) {

basDroite=bd ;

}

// calcul le nombre complexe correspondant aux

// coordonnees (i,j) sur le Viewer.

// le resultat est place dans c

private void calculPointCourant(int i,int j) {

c.setRe(hautGauche.getRe()+(i/largeur)*

(basDroite.getRe()-hautGauche.getRe())) ;

c.setIm(hautGauche.getIm()+(j/hauteur)*

(basDroite.getIm()-hautGauche.getIm())) ;

}

// remplace x par x^2+c

// le calcul n’utilise pas les methodes add et times de Complexe

// pour aller plus vite

private void nextX() {

double re=x.getRe() ;

double im=x.getIm() ;

x.setRe(re*re-im*im+parametre.getRe()) ;

x.setIm(2*re*im+parametre.getIm()) ;

}

// renvoie le nombre d’iteration au bout duquel la suite diverge

// (x_n>seuil). Si la suite n’a pas divergee apres maxIterations

// on renvoie -1

public int getDivergence() {

// x0=c

x.setRe(c.getRe()) ;

x.setIm(c.getIm()) ;

for (int i=0;i<maxIterations;i++) {

// x->x^2+c

nextX() ;

if (x.module()>seuil)

return i ;

}

return -1 ;

}

public void dessine() {

int iter ;

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

for (int j=0;j<hauteur;j++) {

calculPointCourant(i,j) ;

iter=getDivergence() ;

vue.setColor(getColor(iter)) ;

vue.drawPixel(i,j) ;

}

vue.repaint() ;

}

private Color getColor(int iter) {

if (iter==-1) return Color.black ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 21

Page 22: Correction md2 fabien orienté object

int red=255-(int)(iter*255.0/maxIterations) ;

int green=red ;

int blue=255 ;

return new Color(red,green,blue) ;

}

public static void main(String[] arg) {

Viewer vue=new Viewer(400,400) ;

Julia m=new Julia(vue) ;

m.dessine() ;

}

}

————————–

Exercice 4.3 :

————————–

Exercice 4.4 :

import java.awt.*;

import java.util.Random ;

public class PointBrownien {

public int x ;

public int y ;

public static Random alea=new Random((int)System.currentTimeMillis()) ;

public PointBrownien() {

}

public PointBrownien(int a,int b) {

x=a ;

y=b ;

}

public void deplace() {

if (alea.nextDouble()<0.5)

x-- ;

else

x++ ;

if (alea.nextDouble()<0.5)

y-- ;

else

y++ ;

}

public boolean equals(Object obj) {

if (obj instanceof PointBrownien) {

PointBrownien p=(PointBrownien)obj ;

return (p.x==x && p.y==y) ;

}

return false ;

}

public String toString() {

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 22

Page 23: Correction md2 fabien orienté object

return "("+x+","+y+")" ;

}

public static void main(String[] arg) {

int size=500 ;

Viewer v=new Viewer(size,size) ;

v.setColor(new Color(alea.nextInt(255),alea.nextInt(255),

alea.nextInt(255))) ;

PointBrownien p=new PointBrownien(alea.nextInt(size),

alea.nextInt(size)) ;

int n=0 ;

// le point change de couleur et est replace au hazard toutes les

// 1000 iterations

while (true) {

n++ ;

v.drawPixel(p.x,p.y) ;

p.deplace() ;

if (n%1000==0) {

v.setColor(new Color(alea.nextInt(255),alea.nextInt(255),

alea.nextInt(255))) ;

p.x=alea.nextInt(size);

p.y=alea.nextInt(size) ;

}

}

}

}

————————–

Exercice 4.5 :

import java.awt.* ;

public class Boite {

private Viewer vue ;

private boolean[][] contenu ;

private int largeur,hauteur ;

public Boite(Viewer v) {

vue=v ;

vue.setColor(Color.red) ;

largeur=v.getLargeur() ;

hauteur=v.getHauteur() ;

contenu=new boolean[largeur][hauteur] ;

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

for (int j=0;j<largeur;j++)

contenu[i][j]=false ;

fixe(largeur/2,hauteur/2) ;

}

public void fixe(int i,int j) {

contenu[i][j]=true ;

vue.drawPixel(i,j) ;

}

public boolean estFixe(int i,int j) {

return contenu[i][j] ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 23

Page 24: Correction md2 fabien orienté object

}

public int getLargeur() {

return largeur ;

}

public int getHauteur() {

return hauteur ;

}

}

————————–

import java.awt.* ;

import java.util.Random ;

public class Particule {

private PointBrownien pos ;

private static Boite boite=new Boite(new Viewer(200,200)) ;

private static Random alea=new Random((int)System.currentTimeMillis()) ;

public Particule() {

pos=new PointBrownien() ;

}

public void deplace() {

pos.deplace() ;

// univers torique

if (pos.x<=5 || pos.x>=boite.getLargeur()-5 || pos.y<=5 ||

pos.y>boite.getHauteur()-5)

jump() ;

if (estAgrege()) {

boite.fixe(pos.x,pos.y) ;

jump() ;

}

}

// replace la particule au hazard dans la boite

public void jump() {

pos.x=alea.nextInt(boite.getLargeur()-10)+5 ;

pos.y=alea.nextInt(boite.getHauteur()-10)+5 ;

}

public boolean estAgrege() {

for (int i=-1;i<=1;i++)

for (int j=-1;j<=1;j++) {

if (i==0 && j==0) continue ;

if (boite.estFixe(pos.x+i,pos.y+j))

return true ;

}

return false ;

}

public static void main(String[] arg) {

Particule diffusion=new Particule() ;

while (true) {

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 24

Page 25: Correction md2 fabien orienté object

diffusion.deplace() ;

}

}

}

————————–

5 Exercices traites en cours

Exercice 5.1 :

L’interface Fonction

public interface Fonction {

public double eval(double x) ;

public double max(double a,double b) ;

public double min(double a,double b) ;

}

————————–

public class Lineaire implements Fonction {

private double A,double B ;

public Lineaire(double A,double B) {

this.A=A ; this.B=B ;

}

public double eval(double x) {

return A*x+b ;

}

public double min(double a,double b) {

return Math.min(A*a+B,A*b+B) ;

}

public double max(double a,double b) {

return Math.max(A*a+B,A*b+B) ;

}

}

————————–

public class Sinus implements Fonction {

public double eval(double x) {

return Math.sin(x) ;

}

public double max(double a,double b) {

if (a>b) return max(b,a) ;

if ((b-a)>2*Math.PI) return 1 ;

while (a<-3.0/2.0*Math.PI) {

a+=2*Math.PI ;

b+=2*Math.PI ;

}

while (a>=Math.PI/2.0) {

a-=2*Math.PI ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 25

Page 26: Correction md2 fabien orienté object

b-=2*Math.PI ;

}

if (b>Math.PI/2.0) return 1 ;

return Math.max(Math.sin(a),Math.sin(b)) ;

}

public double min(double a,double b) {

if (a>b) return min(b,a) ;

if ((b-a)>2*Math.PI) return -1 ;

while (a<-Math.PI/2.0) {

a+=2*Math.PI ;

b+=2*Math.PI ;

}

while (a>=3.0*Math.PI/2.0) {

a-=2*Math.PI ;

b-=2*Math.PI ;

}

if (b>3*Math.PI/2.0) return -1 ;

return Math.min(Math.sin(a),Math.sin(b)) ;

}

}

————————–

public class Cosinus implements Fonction {

public double eval(double x) {

return Math.cos(x) ;

}

public double max(double a,double b) {

return (new Sinus()).max(a+Math.PI/2.0,b+Math.PI/2.0) ;

}

public double min(double a,double b) {

return (new Sinus()).min(a+Math.PI/2.0,b+Math.PI/2.0) ;

}

}

————————–

public class Somme implements Fonction {

private Fonction f,g ;

public Somme(Fonction f,Fonction g) {

f=this.f ; g=this.g ;

}

public double eval(double x) {

return f.eval(x)+g.eval(x) ;

}

public double max(double a,double b) {

return f.max(a,b)+g.max(a,b) ;

}

public double min(double a,double b) {

return f.min(a,b)+g.min(a,b) ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 26

Page 27: Correction md2 fabien orienté object

}

}

————————–

public class Produit implements Fonction {

private Fonction f,g ;

public Produit(Fonction f,Fonction g) {

this.f=f ;

this.g=g ;

}

public double eval(double x) {

return f.eval(x)*g.eval(x) ;

}

public double max(double a,double b) {

double fmax=f.max() ;

double fmin=f.min() ;

double gmax=g.max() ;

double gmin=g.min() ;

double maxmax=fmax*gmax ;

double maxmin=fmax*gmin ;

double minmax=fmin*gmax ;

double minmin=fmin*gmin ;

double max1=Math.max(maxmax,maxmin) ;

double max2=Math.max(minmax,minmin) ;

return Math.max(max1,max2) ;

}

public double min(double a,double b) {

double fmax=f.max() ;

double fmin=f.min() ;

double gmax=g.max() ;

double gmin=g.min() ;

double maxmax=fmax*gmax ;

double maxmin=fmax*gmin ;

double minmax=fmin*gmax ;

double minmin=fmin*gmin ;

double min1=Math.min(maxmax,maxmin) ;

double min2=Math.min(minmax,minmin) ;

return Math.max(min1,min2) ;

}

}

————————–

public class Intervale {

private double a,b ;

public Intervale(double x,double y) {

if (x<=y) {

a=x ; b=y ;

}

else {

a=y ; b=x ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 27

Page 28: Correction md2 fabien orienté object

}

}

public double inf() {

return a ;

}

public double sup() {

return b ;

}

public double milieu() {

return (a+b)/2.0 ;

}

public boolean contientZero() {

return (a<=0 && b>=0) ;

}

public Intervale moitieInferieure() {

return new Intervale(a,(a+b)/2.0) ;

}

public Intervale moitieSuperieure() {

return new Intervale((a+b)/2.0,b) ;

}

public Intervale image(Fonction f) {

return new Intervale(f.min(a,b),f.max(a,b)) ;

}

}

————————–

import java.util.* ;

public class AlgoFonction {

public static double[] calculZeros(Fonction f,double a,double b,double precision) {

if (b<a) return calculZeros(f,b,a,precision) ;

Intervale ab=new Intervale(a,b) ;

ArrayList inter=new ArrayList() ;

inter.add(ab) ;

double epsilon=b-a ;

while (epsilon>precision && !inter.isEmpty()) {

epsilon*=0.5 ;

ArrayList temp=new ArrayList() ;

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

Intervale inf=((Intervale)inter.get(i)).moitieInferieure() ;

if (inf.image(f).contientZero())

temp.add(inf) ;

Intervale sup=((Intervale)inter.get(i)).moitieSuperieure() ;

if (sup.image(f).contientZero())

temp.add(sup) ;

}

inter=temp ;

}

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 28

Page 29: Correction md2 fabien orienté object

double[] tab=new double[inter.size()] ;

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

tab[i]=((Intervale)inter.get(i)).milieu() ;

System.out.println(tab[i]) ;

}

return tab ;

}

}

————————–

public interface FonctionDerivable extends Fonction {

public FonctionDerivable derive() ;

}

————————–

public class Exponentielle implements FonctionDerivable {

public double eval(double x) {

return Math.exp(x) ;

}

public double max(double a,double b) {

if (a<b) return Math.exp(b) ;

return Math.exp(a) ;

}

public double min(double a,double b) {

if (a<b) return Math.exp(a) ;

return Math.exp(b) ;

}

public Fonction derive() {

return new Exponentielle() ;

}

}

————————–

public class SommeDerivable implements FonctionDerivable {

private FonctionDerivable f,g ;

public SommeDerivable(FonctionDerivable f,FonctionDerivable g) {

f=this.f ; g=this.g ;

}

public double eval(double x) {

return f.eval(x)+g.eval(x) ;

}

public double max(double a,double b) {

return f.max(a,b)+g.max(a,b) ;

}

public double min(double a,double b) {

return f.min(a,b)+g.min(a,b) ;

}

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 29

Page 30: Correction md2 fabien orienté object

public FonctionDerivable derive() {

return new SommeDerivable(f.derive(),g.derive()) ;

}

}

————————–

public class ProduitDerivable implements FonctionDerivable {

private FonctionDerivable f,g ;

public ProduitDerivable(FonctionDerivable f,FonctionDerivable g) {

this.f=f ;

this.g=g ;

}

public double eval(double x) {

return f.eval(x)*g.eval(x) ;

}

public double max(double a,double b) {

double fmax=f.max(a,b) ;

double fmin=f.min(a,b) ;

double gmax=g.max(a,b) ;

double gmin=g.min(a,b) ;

double maxmax=fmax*gmax ;

double maxmin=fmax*gmin ;

double minmax=fmin*gmax ;

double minmin=fmin*gmin ;

double max1=Math.max(maxmax,maxmin) ;

double max2=Math.max(minmax,minmin) ;

return Math.max(max1,max2) ;

}

public double min(double a,double b) {

double fmax=f.max(a,b) ;

double fmin=f.min(a,b) ;

double gmax=g.max(a,b) ;

double gmin=g.min(a,b) ;

double maxmax=fmax*gmax ;

double maxmin=fmax*gmin ;

double minmax=fmin*gmax ;

double minmin=fmin*gmin ;

double min1=Math.min(maxmax,maxmin) ;

double min2=Math.min(minmax,minmin) ;

return Math.max(min1,min2) ;

}

public FonctionDerivable derive() {

return new SommeDerivable(

new ProduitDerivable(f.derive(),g),

new ProduitDerivable(f,g.derive())

) ;

}

}

————————–

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 30

Page 31: Correction md2 fabien orienté object

Exercice 5.2 :

Analyseur de texte

import java.util.* ;

public class Analyseur {

// analyseur statistique de text

// stats

List listMots ;

List occurences ;

int[] longueurPhrase ;

int nMots ; // nombre de mots dans la phrase analysee

// text

String text ;

int longueur ;

int pos ;

public Analyseur(String text,List listMots,List occurences) {

this.text=text ;

// remise a zero des listes de stockage

listMots.clear() ;

this.listMots=listMots ;

occurences.clear() ;

this.occurences=occurences ;

//

longueur=text.length() ;

pos=0 ;

//

longueurPhrase=new int[20] ;

nMots=0 ;

}

private static boolean isLettreOuChiffre(char c) {

return (’a’ <= c && c<=’z’) || (’A’<=c && c<=’Z’) || (’0’ <= c && c<=’9’) ;

}

private static boolean isSeparateur(char c) {

return (c==’,’ || c==’;’ || c==’:’) ;

}

private static boolean isPoint(char c) {

return (c==’!’ || c==’?’ || c==’!’ || c==’.’) ;

}

private String nextWord() { // mot suivant a partir de pos

char c ;

StringBuffer mot=new StringBuffer() ;

while (pos<longueur) {

c=text.charAt(pos) ;

if (isLettreOuChiffre(c)) {

pos++ ;

mot.append(c) ;

}

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 31

Page 32: Correction md2 fabien orienté object

else {

break ;

}

}

nMots++ ;

return mot.toString() ;

}

private void toNextWord() { // place pos a la position du prochain mot

char c ;

int k ;

while (pos<longueur) {

c=text.charAt(pos) ;

if (isLettreOuChiffre(c)) break ;

if (isPoint(c)) {

k=longueurPhrase[Math.min(19,nMots)] ;

longueurPhrase[Math.min(19,nMots)]=k+1 ;

nMots=0 ;

}

pos++ ;

}

}

public void analyse() {

String temp ;

int k ;

int i ;

while (pos<longueur) {

temp=nextWord() ;

k=listMots.indexOf(temp) ;

if (k==-1) {

listMots.add(temp) ;

occurences.add(new Integer(1)) ;

}

else {

i=((Integer)occurences.get(k)).intValue()+1 ;

occurences.set(k,new Integer(i)) ;

}

toNextWord() ;

}

}

public String toString() {

StringBuffer sb=new StringBuffer() ;

int n=listMots.size() ;

sb.append("Liste des mots: ("+listMots.size()+")"+"\n") ;

sb.append("***************\n") ;

for (int i=0;i<n;i++) {

if (((Integer)occurences.get(i)).intValue()==1) continue ;

sb.append(listMots.get(i)) ;

sb.append(" ") ;

sb.append(occurences.get(i)) ;

sb.append(’\n’) ;

}

sb.append("Stat mots par phrase:\n") ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 32

Page 33: Correction md2 fabien orienté object

sb.append("*********************\n") ;

for (int i=0;i<longueurPhrase.length;i++) {

sb.append(i+" "+longueurPhrase[i]+"\n") ;

}

return sb.toString() ;

}

public static void main(String[] arg) {

// le texte a analyser est prise en argument de

// la commande d’execution du programme:

// par exemple on tape:

// java Analyseur laclos.txt

// pour analyser le texte appele laclos.txt

Analyseur stat ;

if (arg.length==0)

stat=new Analyseur(" ",new LinkedList(),new ArrayList()) ;

else

stat=new Analyseur(File.readFile(arg[0]),

new LinkedList(),new ArrayList()) ;

Chronometre c=new Chronometre() ;

c.start() ;

stat.analyse() ;

System.out.println(c.stop()) ;

System.out.println(stat) ;

}

}

————————–

Exercice 5.3 :

Algorithme genetique

public interface Gene {

public void mutation() ;

public Gene dolly() ;

}

————————–

public interface Genome extends Comparable {

public int size() ;

public Gene getGene(int i) ;

public void setGene(int i,Gene g) ;

public void vie() ;

public Genome dolly() ;

}

————————–

import java.util.* ;

import java.util.Random ;

/* classe representant une population d’individus avec leurs codes genetiques */

public class Population {

private static Random alea=new Random() ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 33

Page 34: Correction md2 fabien orienté object

private List generation ;

public Population() {

generation=new ArrayList() ;

}

public void add(Genome g) {

generation.add(g) ;

}

public int size() {

return generation.size() ;

}

public Genome getGenome(int i) {

return (Genome)generation.get(i) ;

}

// methode de croisement

// (agit par effet de bord)

public static void croisement(Genome f,Genome g) {

int j=alea.nextInt(f.size()) ;

Gene temp ;

for (int i=j;i<f.size();i++) {

temp=f.getGene(i) ;

f.setGene(i,g.getGene(i)) ;

g.setGene(i,temp) ;

}

}

// methode de mutation

// (agit par effet de bord)

public static void mutation(Genome g) {

for (int i=0;i<g.size();i++)

g.getGene(i).mutation() ;

}

// passage a la generation suivante

public void nextGeneration() {

// evaluation

for (int i=0;i<generation.size();i++)

getGenome(i).vie() ;

// trie

Collections.sort(generation) ;

// selection

List next=new ArrayList() ;

int n=generation.size()/2 ;

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

next.add(getGenome(i)) ;

// reproduction

Genome f,g ;

for (int i=0;i<n/2;i++) {

f=getGenome(alea.nextInt(n)).dolly() ;

g=getGenome(alea.nextInt(n)).dolly() ;

croisement(f,g) ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 34

Page 35: Correction md2 fabien orienté object

mutation(f) ;

mutation(g) ;

next.add(f) ;

next.add(g) ;

}

generation=next ;

}

}

————————–

import java.util.Random ;

public class Lettre implements Gene {

private static Random alea=new Random() ;

private char c ;

double mutationRate=0.02 ;

public Lettre() {

int n=alea.nextInt(’z’-’a’+1) ;

if (n==0)

c=’ ’ ;

else

c=(char)(’a’-1+n) ;

}

private Lettre(char c) {

this.c=c ;

}

public void mutation() {

if (alea.nextDouble()<mutationRate) {

int n=alea.nextInt(’z’-’a’+1) ;

if (n==0)

c=’ ’ ;

else

c=(char)(’a’-1+n) ;

}

}

public Gene dolly() {

return new Lettre(c) ;

}

public char getVal() {

return c ;

}

}

————————–

import java.util.* ;

// classe permettant de deviner une phrase par algo genetique

public class Devine implements Genome {

// devine une phrase de 20 lettres maximum

private static String phrase=new String() ;

private List genes ;

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 35

Page 36: Correction md2 fabien orienté object

private int score=0 ;

public Devine() {

genes=new ArrayList(20) ;

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

genes.add(new Lettre()) ;

}

// constructeur de recopie

public Devine(Devine d) {

genes=new ArrayList(20) ;

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

genes.add(d.getGene(i).dolly()) ;

}

public static void setPhrase(String s) {

phrase=s ;

}

public int size() {

return genes.size() ;

}

public Gene getGene(int i) {

return (Gene)genes.get(i) ;

}

public void setGene(int i,Gene g) {

genes.set(i,g) ;

}

public void vie() {

score=0 ;

for (int i=0;i<Math.min(20,phrase.length());i++)

if (((Lettre)getGene(i)).getVal()==phrase.charAt(i))

score++ ;

System.out.println(this) ;

}

public Genome dolly() {

return new Devine(this) ;

}

public int compareTo(Object o) {

if (!(o instanceof Devine))

return Integer.MIN_VALUE ;

return ((Devine)o).score-score ;

}

public String toString() {

String s=score+" " ;

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

s+=((Lettre)getGene(i)).getVal() ;

return s ;

}

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 36

Page 37: Correction md2 fabien orienté object

public static void main(String[] arg) {

String s="phrase a deviner" ;

Devine.setPhrase(s) ;

Population p=new Population() ;

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

p.add(new Devine()) ;

for (int i=0;i<150;i++) {

System.out.println("\n \n \n"+i+" \n \n") ;

p.nextGeneration() ;

}

}

}

————————–

F. Napolitano – 19 decembre 2000 (Version 1.0) p. 37