SlideShare une entreprise Scribd logo
1  sur  37
Télécharger pour lire hors ligne
Correction des exercices de programmation objet
                                     Fabien Napolitano
                                     19 d´cembre 2000
                                         e


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 ;
           }
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 d´cembre 2000 (Version 1.0)
                    e                                                     p. 2
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 d´cembre 2000 (Version 1.0)
                    e                                                          p. 3
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 d´cembre 2000 (Version 1.0)
                    e                                                                   p. 4
————————–
    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 d´cembre 2000 (Version 1.0)
                    e                                                                 p. 5
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 donn´es
                                         e

Exercice 2.1 :
    public class CaseListeChainee {
           private Object contenu ;
           private CaseListeChainee caseSuivante ;
           private CaseListeChainee casePrecedente ;

            public CaseListeChainee(Object obj) {
                   contenu=obj ;


F. Napolitano – 19 d´cembre 2000 (Version 1.0)
                    e                                         p. 6
}

            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 d´cembre 2000 (Version 1.0)
                    e                                                         p. 7
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 d´cembre 2000 (Version 1.0)
                    e                                                             p. 8
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 d´cembre 2000 (Version 1.0)
                    e                                                        p. 9
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 d´cembre 2000 (Version 1.0)
                    e                                                        p. 10
————————–

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 d´cembre 2000 (Version 1.0)
                    e                                     p. 11
}

            // 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 d´cembre 2000 (Version 1.0)
                    e                                                    p. 12
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 d´cembre 2000 (Version 1.0)
                    e                                        p. 13
}

            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 d´cembre 2000 (Version 1.0)
                    e                                                            p. 14
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 gagn´. A quoi pensiez vous ?") ;
                                                    e
                  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 d´cembre 2000 (Version 1.0)
                    e                                                                 p. 15
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 donn´e dans l’´nonc´ fonctionne tr`s bien avec Java 2 mais pas du tout avec Java
                                 e        e    e             e
1. Etant donn´ que les ordinateurs du cryo utilisent par d´faut Java 1 voici une programmation simplifi´e
               e                                          e                                               e
de cette classe fonctionnant avec Java 1 (n´ammoins la classe originale est bien meilleure si vous avez Java
                                            e
2). La classe Viewer reste inchang´e.
                                   e
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 d´cembre 2000 (Version 1.0)
                    e                                                                                 p. 16
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 r´el
                                                   e
                   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 d´cembre 2000 (Version 1.0)
                    e                                            p. 17
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 {
           // d´limitations de la partie etudi´e
               e                          ´   e
           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 d´cembre 2000 (Version 1.0)
                    e                                                                  p. 18
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’it´ration au bout duquel la suite diverge
                                      e
            // (x_n>seuil). Si la suite n’a pas diverg´e apr`s maxIt´rations
                                                      e     e       e
            // 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 d´cembre 2000 (Version 1.0)
                    e                                                          p. 19
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 {
           // d´limitations de la partie etudi´e
               e                          ´   e
           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 d´cembre 2000 (Version 1.0)
                    e                                                                 p. 20
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’it´ration au bout duquel la suite diverge
                                      e
            // (x_n>seuil). Si la suite n’a pas diverg´e apr`s maxIt´rations
                                                      e     e       e
            // 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 d´cembre 2000 (Version 1.0)
                    e                                                          p. 21
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 d´cembre 2000 (Version 1.0)
                    e                                                                p. 22
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 d´cembre 2000 (Version 1.0)
                    e                                                                  p. 23
}

            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 d´cembre 2000 (Version 1.0)
                    e                                                                  p. 24
diffusion.deplace() ;
                    }
            }
    }
                                            ————————–


5    Exercices trait´s en cours
                    e
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 d´cembre 2000 (Version 1.0)
                    e                                   p. 25
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 d´cembre 2000 (Version 1.0)
                    e                                             p. 26
}
    }
                                            ————————–
    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 d´cembre 2000 (Version 1.0)
                    e                                   p. 27
}
    }

    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 d´cembre 2000 (Version 1.0)
                    e                                                                p. 28
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 d´cembre 2000 (Version 1.0)
                    e                                                  p. 29
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 d´cembre 2000 (Version 1.0)
                    e                                                    p. 30
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 analys´e
                                                       e
    // 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 d´cembre 2000 (Version 1.0)
                    e                                                                p. 31
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 d´cembre 2000 (Version 1.0)
                    e                                                          p. 32
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 appel´ laclos.txt
                                          e
           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 g´n´tique
                e e

    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 d´cembre 2000 (Version 1.0)
                    e                                                                  p. 33
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 d´cembre 2000 (Version 1.0)
                    e                                    p. 34
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 d´cembre 2000 (Version 1.0)
                    e                                               p. 35
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 d´cembre 2000 (Version 1.0)
                    e                                              p. 36
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 d´cembre 2000 (Version 1.0)
                    e                                     p. 37

Contenu connexe

Tendances

Cours algorithmique et complexite complet
Cours algorithmique et complexite completCours algorithmique et complexite complet
Cours algorithmique et complexite complet
Chahrawoods Dmz
 
Chapitre i introduction et motivations
Chapitre i introduction et motivationsChapitre i introduction et motivations
Chapitre i introduction et motivations
Sana Aroussi
 
Chapitre iii récursivité et paradigme diviser pour régner
Chapitre iii récursivité et paradigme diviser pour régnerChapitre iii récursivité et paradigme diviser pour régner
Chapitre iii récursivité et paradigme diviser pour régner
Sana Aroussi
 

Tendances (20)

Arbre et algorithme de recherche
Arbre et algorithme de rechercheArbre et algorithme de recherche
Arbre et algorithme de recherche
 
Part1
Part1Part1
Part1
 
Programmation fonctionnelle
Programmation fonctionnelleProgrammation fonctionnelle
Programmation fonctionnelle
 
Support tutoriel : Créer votre jeu en HTML5
Support tutoriel : Créer votre jeu en HTML5Support tutoriel : Créer votre jeu en HTML5
Support tutoriel : Créer votre jeu en HTML5
 
Chapitre8: Collections et Enumerations En Java
Chapitre8: Collections et Enumerations En JavaChapitre8: Collections et Enumerations En Java
Chapitre8: Collections et Enumerations En Java
 
Développement informatique : Chaines de caractères et expressions regulières
Développement informatique : Chaines de caractères et expressions regulièresDéveloppement informatique : Chaines de caractères et expressions regulières
Développement informatique : Chaines de caractères et expressions regulières
 
Récursivité
RécursivitéRécursivité
Récursivité
 
Scala : programmation fonctionnelle
Scala : programmation fonctionnelleScala : programmation fonctionnelle
Scala : programmation fonctionnelle
 
Python avancé : Ensemble, dictionnaire et base de données
Python avancé : Ensemble, dictionnaire et base de donnéesPython avancé : Ensemble, dictionnaire et base de données
Python avancé : Ensemble, dictionnaire et base de données
 
Plongement de mots :Modèle basé sur les réseaux de neurones
Plongement de mots :Modèle basé sur les réseaux de neuronesPlongement de mots :Modèle basé sur les réseaux de neurones
Plongement de mots :Modèle basé sur les réseaux de neurones
 
Cours algorithmique et complexite complet
Cours algorithmique et complexite completCours algorithmique et complexite complet
Cours algorithmique et complexite complet
 
Chapitre i introduction et motivations
Chapitre i introduction et motivationsChapitre i introduction et motivations
Chapitre i introduction et motivations
 
DCT1 4SI
DCT1  4SIDCT1  4SI
DCT1 4SI
 
Les fonctions lambdas en C++11 et C++14
Les fonctions lambdas en C++11 et C++14Les fonctions lambdas en C++11 et C++14
Les fonctions lambdas en C++11 et C++14
 
Résumer arithmétique
Résumer arithmétiqueRésumer arithmétique
Résumer arithmétique
 
Chapitre 1 rappel
Chapitre 1 rappelChapitre 1 rappel
Chapitre 1 rappel
 
Chapitre 3 NP-complétude
Chapitre 3 NP-complétudeChapitre 3 NP-complétude
Chapitre 3 NP-complétude
 
Chapitre iii récursivité et paradigme diviser pour régner
Chapitre iii récursivité et paradigme diviser pour régnerChapitre iii récursivité et paradigme diviser pour régner
Chapitre iii récursivité et paradigme diviser pour régner
 
Introduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El HassaniIntroduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El Hassani
 
Python avancé : Qualité de code et convention de codage
Python avancé : Qualité de code et convention de codagePython avancé : Qualité de code et convention de codage
Python avancé : Qualité de code et convention de codage
 

En vedette

Evaluation Question 5
Evaluation Question 5Evaluation Question 5
Evaluation Question 5
rachelblack10
 
Ultimate Power Profits
Ultimate Power ProfitsUltimate Power Profits
Ultimate Power Profits
Paul Morris
 
Deconstruction od magazine front covers
Deconstruction od magazine front coversDeconstruction od magazine front covers
Deconstruction od magazine front covers
Niamh1996
 
Deconstruction of 3 music magazine covers
Deconstruction of 3 music magazine covers Deconstruction of 3 music magazine covers
Deconstruction of 3 music magazine covers
rachelblack10
 
Wifisky wireless adapter(ralink 3070)
Wifisky wireless adapter(ralink 3070)Wifisky wireless adapter(ralink 3070)
Wifisky wireless adapter(ralink 3070)
sabrishafie
 
Deconstruction of double page spread for music magazine
Deconstruction of double page spread for music magazineDeconstruction of double page spread for music magazine
Deconstruction of double page spread for music magazine
rachelblack10
 
Adjetives by David Hernandez
Adjetives by David HernandezAdjetives by David Hernandez
Adjetives by David Hernandez
dvd_h
 

En vedette (20)

SocEat
SocEatSocEat
SocEat
 
Evaluation Question 5
Evaluation Question 5Evaluation Question 5
Evaluation Question 5
 
Ultimate Power Profits
Ultimate Power ProfitsUltimate Power Profits
Ultimate Power Profits
 
Piet Mondrian
Piet MondrianPiet Mondrian
Piet Mondrian
 
Mission-Driven Interaction Design
Mission-Driven Interaction DesignMission-Driven Interaction Design
Mission-Driven Interaction Design
 
Question 7
Question 7 Question 7
Question 7
 
Deconstruction od magazine front covers
Deconstruction od magazine front coversDeconstruction od magazine front covers
Deconstruction od magazine front covers
 
Mothers day Motivation
Mothers day MotivationMothers day Motivation
Mothers day Motivation
 
Deconstruction of 3 music magazine covers
Deconstruction of 3 music magazine covers Deconstruction of 3 music magazine covers
Deconstruction of 3 music magazine covers
 
Athens Startup Weekend - pspace
Athens Startup Weekend - pspaceAthens Startup Weekend - pspace
Athens Startup Weekend - pspace
 
Beethoven r&m
Beethoven r&mBeethoven r&m
Beethoven r&m
 
K2
K2K2
K2
 
Wifisky wireless adapter(ralink 3070)
Wifisky wireless adapter(ralink 3070)Wifisky wireless adapter(ralink 3070)
Wifisky wireless adapter(ralink 3070)
 
PT TOSANDA DWI SAPURWA
PT TOSANDA DWI SAPURWAPT TOSANDA DWI SAPURWA
PT TOSANDA DWI SAPURWA
 
K3
K3K3
K3
 
Deconstruction of double page spread for music magazine
Deconstruction of double page spread for music magazineDeconstruction of double page spread for music magazine
Deconstruction of double page spread for music magazine
 
Adjetives by David Hernandez
Adjetives by David HernandezAdjetives by David Hernandez
Adjetives by David Hernandez
 
Designing The Listening Experience: The Making of NPR One
Designing The Listening Experience: The Making of NPR OneDesigning The Listening Experience: The Making of NPR One
Designing The Listening Experience: The Making of NPR One
 
Brosur paintind dan sandblasting equipments
Brosur paintind dan sandblasting equipments Brosur paintind dan sandblasting equipments
Brosur paintind dan sandblasting equipments
 
absorspi minyak jelantah dengan serat ampas tebu
 absorspi minyak jelantah dengan serat ampas tebu absorspi minyak jelantah dengan serat ampas tebu
absorspi minyak jelantah dengan serat ampas tebu
 

Similaire à Correction md2 fabien orienté object

Lect14 dev2
Lect14 dev2Lect14 dev2
Lect14 dev2
moisko
 

Similaire à Correction md2 fabien orienté object (17)

Chap 2--POO avec JAVA.pdf
Chap 2--POO avec JAVA.pdfChap 2--POO avec JAVA.pdf
Chap 2--POO avec JAVA.pdf
 
Corrige tp java
Corrige tp javaCorrige tp java
Corrige tp java
 
Corrigé langage c
Corrigé langage cCorrigé langage c
Corrigé langage c
 
Corrigés exercices langage C
Corrigés exercices langage CCorrigés exercices langage C
Corrigés exercices langage C
 
C4 fonctions
C4 fonctionsC4 fonctions
C4 fonctions
 
Examen 2011 exo 4
Examen 2011 exo 4Examen 2011 exo 4
Examen 2011 exo 4
 
Lect14 dev2
Lect14 dev2Lect14 dev2
Lect14 dev2
 
résumé POO java .pdf
résumé POO java .pdfrésumé POO java .pdf
résumé POO java .pdf
 
correctionTD2JAVA.pdf
correctionTD2JAVA.pdfcorrectionTD2JAVA.pdf
correctionTD2JAVA.pdf
 
Programmation orientée objet : Object, classe et encapsulation
Programmation orientée objet : Object, classe et encapsulationProgrammation orientée objet : Object, classe et encapsulation
Programmation orientée objet : Object, classe et encapsulation
 
C++11
C++11C++11
C++11
 
Memojava 100604104941-phpapp02
Memojava 100604104941-phpapp02Memojava 100604104941-phpapp02
Memojava 100604104941-phpapp02
 
La programmation fonctionnelle avec le langage OCaml
La programmation fonctionnelle avec le langage OCamlLa programmation fonctionnelle avec le langage OCaml
La programmation fonctionnelle avec le langage OCaml
 
C++11 en 12 exemples simples
C++11 en 12 exemples simplesC++11 en 12 exemples simples
C++11 en 12 exemples simples
 
Cours de C++, en français, 2002 - Cours 2.1
Cours de C++, en français, 2002 - Cours 2.1Cours de C++, en français, 2002 - Cours 2.1
Cours de C++, en français, 2002 - Cours 2.1
 
Formation POO & Java (2ème séance)
Formation POO & Java (2ème séance)Formation POO & Java (2ème séance)
Formation POO & Java (2ème séance)
 
Patron observer
Patron observerPatron observer
Patron observer
 

Correction md2 fabien orienté object

  • 1. Correction des exercices de programmation objet Fabien Napolitano 19 d´cembre 2000 e 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 ; }
  • 2. 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 d´cembre 2000 (Version 1.0) e p. 2
  • 3. 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 d´cembre 2000 (Version 1.0) e p. 3
  • 4. 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 d´cembre 2000 (Version 1.0) e p. 4
  • 5. ————————– 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 d´cembre 2000 (Version 1.0) e p. 5
  • 6. 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 donn´es e Exercice 2.1 : public class CaseListeChainee { private Object contenu ; private CaseListeChainee caseSuivante ; private CaseListeChainee casePrecedente ; public CaseListeChainee(Object obj) { contenu=obj ; F. Napolitano – 19 d´cembre 2000 (Version 1.0) e p. 6
  • 7. } 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 d´cembre 2000 (Version 1.0) e p. 7
  • 8. 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 d´cembre 2000 (Version 1.0) e p. 8
  • 9. 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 d´cembre 2000 (Version 1.0) e p. 9
  • 10. 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 d´cembre 2000 (Version 1.0) e p. 10
  • 11. ————————– 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 d´cembre 2000 (Version 1.0) e p. 11
  • 12. } // 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 d´cembre 2000 (Version 1.0) e p. 12
  • 13. 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 d´cembre 2000 (Version 1.0) e p. 13
  • 14. } 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 d´cembre 2000 (Version 1.0) e p. 14
  • 15. 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 gagn´. A quoi pensiez vous ?") ; e 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 d´cembre 2000 (Version 1.0) e p. 15
  • 16. 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 donn´e dans l’´nonc´ fonctionne tr`s bien avec Java 2 mais pas du tout avec Java e e e e 1. Etant donn´ que les ordinateurs du cryo utilisent par d´faut Java 1 voici une programmation simplifi´e e e e de cette classe fonctionnant avec Java 1 (n´ammoins la classe originale est bien meilleure si vous avez Java e 2). La classe Viewer reste inchang´e. e 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 d´cembre 2000 (Version 1.0) e p. 16
  • 17. 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 r´el e 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 d´cembre 2000 (Version 1.0) e p. 17
  • 18. 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 { // d´limitations de la partie etudi´e e ´ e 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 d´cembre 2000 (Version 1.0) e p. 18
  • 19. 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’it´ration au bout duquel la suite diverge e // (x_n>seuil). Si la suite n’a pas diverg´e apr`s maxIt´rations e e e // 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 d´cembre 2000 (Version 1.0) e p. 19
  • 20. 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 { // d´limitations de la partie etudi´e e ´ e 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 d´cembre 2000 (Version 1.0) e p. 20
  • 21. 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’it´ration au bout duquel la suite diverge e // (x_n>seuil). Si la suite n’a pas diverg´e apr`s maxIt´rations e e e // 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 d´cembre 2000 (Version 1.0) e p. 21
  • 22. 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 d´cembre 2000 (Version 1.0) e p. 22
  • 23. 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 d´cembre 2000 (Version 1.0) e p. 23
  • 24. } 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 d´cembre 2000 (Version 1.0) e p. 24
  • 25. diffusion.deplace() ; } } } ————————– 5 Exercices trait´s en cours e 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 d´cembre 2000 (Version 1.0) e p. 25
  • 26. 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 d´cembre 2000 (Version 1.0) e p. 26
  • 27. } } ————————– 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 d´cembre 2000 (Version 1.0) e p. 27
  • 28. } } 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 d´cembre 2000 (Version 1.0) e p. 28
  • 29. 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 d´cembre 2000 (Version 1.0) e p. 29
  • 30. 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 d´cembre 2000 (Version 1.0) e p. 30
  • 31. 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 analys´e e // 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 d´cembre 2000 (Version 1.0) e p. 31
  • 32. 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 d´cembre 2000 (Version 1.0) e p. 32
  • 33. 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 appel´ laclos.txt e 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 g´n´tique e e 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 d´cembre 2000 (Version 1.0) e p. 33
  • 34. 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 d´cembre 2000 (Version 1.0) e p. 34
  • 35. 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 d´cembre 2000 (Version 1.0) e p. 35
  • 36. 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 d´cembre 2000 (Version 1.0) e p. 36
  • 37. 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 d´cembre 2000 (Version 1.0) e p. 37