NFP121

Programmation avancée

Session d’Avril 2013-durée : 2 heures

Tous documents papiers autorisés

     CEP-HTO/FOD Nationale

Sommaire :

            Question 1 (6 points) : un diplôme, des unités d’enseignements

            Question 2 (4 points) : interpréter une formation

Question 3 (6 points) : visiter une formation

Question 4 (4 points) : une interface graphique

 

 

Question 1 : Un diplôme, des unités d’enseignements

 

Une institution de formation, tout au long de la vie, propose des formations. Une formation peut correspondre à une simple unité d’enseignement ou à une formation diplômante composée de plusieurs unités d’enseignements.

 

L’architecture retenue des classes pour cette question est la suivante (en notation BlueJ/UML) :

 

 

 

L’interface FormationI  contient les définitions des méthodes suivantes :

 

public interface FormationI {
    
    public String getCode();
    public String getIntitule();
    public int getNbCredits();
    public boolean ajouterUniteEnseignement(UniteEnseignement ue);
        
}

 

Ci-dessous, une méthode de tests extraite de la classe de tests unitaires TestFormationSimple : toutes vos réponses à la question 1 doivent impérativement passer ces tests.

 

    public void testSimple() {
        LicenceSTIC licence = new LicenceSTIC();
        IngenieurAISL ingenieur = new IngenieurAISL();
        NFP121 ueNFP1211 = new NFP121();
        RSX116 ueRSX1161 = new RSX116();

        assertEquals(licence.getCode(), "LG025");
        assertEquals("Licence STIC, mention informatique générale", licence.getIntitule());
        assertEquals(licence.nombreUnitesEnseignements(), 0);
        assertEquals(licence.getNbCredits(), 0);
        assertFalse(licence.equals(ingenieur)); /* equals */
      

        assertEquals(ueNFP1211.getCode(), "NFP121");
        assertEquals(ueNFP1211.getIntitule(), "Programmation avancée");
        assertEquals(ueNFP1211.getNbCredits(), 6);
        assertFalse(ueNFP1211.equals(ueRSX1161)); /* equals */
 

        assertTrue(licence.ajouterUniteEnseignement(ueNFP1211));
        assertFalse(licence.ajouterUniteEnseignement(ueNFP1211));
        assertTrue(licence.ajouterUniteEnseignement(ueRSX1161));
        assertEquals(licence.getNbCredits(), 12);

        try {
            ueNFP1211.ajouterUniteEnseignement(ueRSX1161);
            fail();
        catch(Exception e) {
            assertTrue(e instanceof RuntimeException);
        }
    }

 

Question 1-1)

Ecrire une implémentation complète de la classe abstraite UniteEnseignement.

 

Question 1-2)

Compléter l’implémentation de la classe NFP121.

 

public final class NFP121 /* à compléter */ {

    public NFP121() {
        
/* à compléter */
    }
    
    public boolean equals(Object o) {
        
/* à compléter */
    }
}

 

Question 1-3)

Ecrire une implémentation complète de la classe abstraite Diplome.

 

Question 1-4)

Compléter l’implémentation de la classe LicenceSTIC.

 

public class LicenceSTIC /* à compléter */ {

    public LicenceSTIC() {
        
/* à compléter */
    }
    
    public boolean equals(Object o) {
        
/* à compléter */
    }
}

 

Question 2 : Interpréter une formation

 

Une représentation ASCII d’une formation est attendue. Pour cela, l’interface FormationI est modifiée de la façon suivante :

 

public interface FormationI {
    
    public String getCode();
    public String getIntitule();
    public int getNbCredits();
    public boolean ajouterUniteEnseignement(UniteEnseignement ue);
    
    public String interpreter();
    
}

 

Soit le programme suivant qui permet d’afficher au format ASCII une instance de Formation :

 

public class ExempleFormation {

    public static void main() {
        
        LicenceSTIC licence = new LicenceSTIC();        
        licence.ajouterUniteEnseignement(new NFP121());
        licence.ajouterUniteEnseignement(new RSX116());
        licence.ajouterUniteEnseignement(new NSX102());
        
        System.out.println(licence.interpreter());

    }

}

 

Après exécution, ce programme engendre la sortie suivante sur la fenêtre terminal :

 

 

 

Question 2)

Proposer une implémentation de nouvelle méthode interpreter()pour les classes abstraites UniteEnseignement et Diplome.

 


Question 3 : Visiter une formation

 

Le patron visiteur permet de visiter une structure composite et d’exécuter une opération particulière en fonction du nœud visité.

 

 

Les arbres XML engendrés par le visiteur VisiteurToXML doivent respecter la DTD suivante :

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE formation SYSTEM "formation.dtd">

<!ELEMENT diplome (UE)*>

<!ATTLIST classe code CDATA #REQUIRED>

<!ATTLIST classe intitule CDATA #REQUIRED>

<!ATTLIST classe nbCredits CDATA #REQUIRED>

<!ELEMENT UE (code,intitule,nbCredits)>

<!ATTLIST code type CDATA #REQUIRED>

<!ATTLIST intitule type CDATA #REQUIRED>

<!ATTLIST nbCredits type CDATA #REQUIRED>

 

Soit l’interface générique Visiteur  :

 

public interface Visiteur<T> {

    visite(UniteEnseignement ue);
    T visite(Diplome diplome);
}

 

Afin de visiter une Formation, l’interface FormationI est modifiée de la façon suivante :

 

public interface FormationI {
    public String getCode();
    public String getIntitule();
    public int getNbCredits();
    public boolean ajouterUniteEnseignement(UniteEnseignement ue);
    
    public <T> T accepter(Visiteur<T> v);
}

 

Les quelques lignes suivantes sont extraites de la classe de ExempleXML.

 

    public static void main() {

        LicenceSTIC licence = new LicenceSTIC();
        licence.ajouterUniteEnseignement(new NFP121());
        licence.ajouterUniteEnseignement(new RSX116());
        licence.ajouterUniteEnseignement(new NSX102());        


        Element racine = licence.accepter(
new VisiteurToXML());
        Document document = new Document(racine);
        XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());        
        try {
            out.output(document, System.out);
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

Ci-dessous le document XML généré par la visite :

 

<?xml version="1.0" encoding="UTF-8"?>

<Diplome code="LG025" intitule="Licence STIC, mention informatique générale" nbCredits="18">

  <UE code="RSX116" intitule="Réseaux mobiles et réseaux sans fil" nbCredits="6" />

  <UE code="NSX102" intitule="Conception de logiciels intranet : patrons et canevas" nbCredits="6" />

  <UE code="NFP121" intitule="Programmation avancée" nbCredits="6" />

</Diplome>

 

Question 3-1)

Ecrire une implémentation complète de la classe VisiteurToXML.

 

 

En exercices dirigés, la question 3-1 est remplacée par

Ecrire une implémentation complète de la classe VisiteurToString.

 

 

 

Question 4 : Une interface graphique

 

Soit l’interface graphique suivante :

 

 

 

Ci-dessous, l’implémentation de cette interface graphique : à chaque clique sur le bouton toXML, la visite d’une Formation est obtenue puis affichée dans la zone de texte resultat.

 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.jdom.*;
import org.jdom.output.*;
import java.io.ByteArrayOutputStream;

public class IHM extends JFrame {

    private JTextArea resultat = new JTextArea(""20,60);
    private JButton visiter = new JButton("toXML");
    private FormationI formation;
    private ByteArrayOutputStream baos = new ByteArrayOutputStream();

    public IHM() {
        this.setTitle("Formation");
        Container container = this.getContentPane();
        container.setLayout(new BorderLayout());
        container.add(resultat, BorderLayout.NORTH);
        container.add(visiter, BorderLayout.SOUTH);

        this.formation = new LicenceSTIC();
        this.formation.ajouterUniteEnseignement(new NFP121());
        this.formation.ajouterUniteEnseignement(new RSX116());
        this.formation.ajouterUniteEnseignement(new NSX102());

        this.visiter.addActionListener(

/* à compléter */

/* à compléter */

/* à compléter */

        );
        


        try{
            out.output(document, baos);
        }catch(Exception e){
            e.printStackTrace();
        }
        this.pack();
        this.setVisible(true);
    }

    public static void main() {
        new IHM();    
    
}

 

Question 4)

Compléter l’implémentation de la classe IHM afin de produire le fonctionnement décrit ci-dessus.

 

 

En exercices dirigés, la question 4 est remplacée par

Complétez l'implémentation de la classe IHM en utilisant la classe VisiteurToString,

         le bouton visiter devient JButton visiter = new JButton("toString").

 

 

    

 

-------- fin ---------

 

Nota bene : Une idée de la solution sera en ligne à cette URL : http://jfod.cnam.fr/NFP121/annales/2013_avril/

 


Annexes

java.util
Interface Set<E>

Type Parameters:

E - the type of elements maintained by this set

All Superinterfaces:

Collection<E>, Iterable<E>

All Known Subinterfaces:

NavigableSet<E>, SortedSet<E>

All Known Implementing Classes:

AbstractSet, ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, JobStateReasons, LinkedHashSet, TreeSet


public interface Set<E>
extends Collection<E>

Method Summary

 boolean

add(E e)
          Adds the specified element to this set if it is not already present (optional operation).

 boolean

addAll(Collection<? extends E> c)
          Adds all of the elements in the specified collection to this set if they're not already present (optional operation).

 void

clear()
          Removes all of the elements from this set (optional operation).

 boolean

contains(Object o)
          Returns
true if this set contains the specified element.

 boolean

containsAll(Collection<?> c)
          Returns
true if this set contains all of the elements of the specified collection.

 boolean

equals(Object o)
          Compares the specified object with this set for equality.

 int

hashCode()
          Returns the hash code value for this set.

 boolean

isEmpty()
          Returns
true if this set contains no elements.

 Iterator<E>

iterator()
          Returns an iterator over the elements in this set.

 boolean

remove(Object o)
          Removes the specified element from this set if it is present (optional operation).

 boolean

removeAll(Collection<?> c)
          Removes from this set all of its elements that are contained in the specified collection (optional operation).

 boolean

retainAll(Collection<?> c)
          Retains only the elements in this set that are contained in the specified collection (optional operation).

 int

size()
          Returns the number of elements in this set (its cardinality).

 Object[]

toArray()
          Returns an array containing all of the elements in this set.

<T> T[]

toArray(T[] a)
          Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.

 java.lang
Interface Iterable<T>

Implementing this interface allows an object to be the target of the "foreach" statement.


Method Summary

 Iterator<T>

iterator()
          Returns an iterator over a set of elements of type T.

 java.util
Interface Iterator<E>


Method Summary

 boolean

hasNext()
          Returns
true if the iteration has more elements.

 E

next()
          Returns the next element in the iteration.

 void

remove()
          Removes from the underlying collection the last element returned by the iterator (optional operation).

java.awt.event 
Interface ActionListener

public interface ActionListener
extends EventListener

The listener interface for receiving action events. The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method. When the action event occurs, that object'sactionPerformed method is invoked.


Method Summary

 void

actionPerformed(ActionEvent e) 
          Invoked when an action occurs.

 

org.jdom
Class Element partielle

java.lang.Object
  extended by org.jdom.Content
      extended by org.jdom.Element

All Implemented Interfaces:

java.io.Serializable, java.lang.Cloneable, Parent


public class Element

extends Content

implements Parent

An XML element. Methods allow the user to get and manipulate its child elements and content, directly access the element's textual content, manipulate its attributes, and manage namespaces.

Constructor Summary

 

Element(java.lang.String name)
          Create a new element with the supplied (local) name and no namespace.

 

Method Summary

 Element

addContent(java.lang.String str)
          This adds text content to this element.

 Element

setAttribute(java.lang.String name, java.lang.String value)
           This sets an attribute value for this element.