nfp121 Final 2013 Corrige
nfp121 Final 2013 Corrige
nfp121 Final 2013 Corrige
Documents autorisés
import java.util.*;
public class TacheComplexe implements Tache {
private Collection<Tache> sousTaches;
private String nom;
public TacheComplexe(String nom) { this.nom = nom;
sousTaches = new ArrayList<Tache>();
}
public void ajouter(Tache tache) { sousTaches.add(tache);
}
public void supprimer(Tache tache) { sousTaches.remove(tache);
}
public String getNom() { return nom;
}
public int getCout() { int result = 0;
for (Tache t : sousTaches) { result += t.getCout(); }
return result;
}
public String toString() { String S=nom + ": [";
for (Tache t : sousTaches) { S += t.toString() + ","; }
S += "]";
return S;
}
}
1
b) (2½ pt)
Design Pattern: Decorator
2
Exercice 2 (3 pts)
3
b) (4 pts)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class ChatSwing extends JFrame implements Observer{
private JTextArea messages = new JTextArea(12, 20);
private JLabel nom = new JLabel();
private JTextField texte = new JTextField(15);
private JButton bOK = new JButton("OK");
private JPanel p1 = new JPanel();
private JPanel p2 = new JPanel();
private Modele modele;
public ChatSwing(String pseudo, Modele modeleText) {
super("Chat de " + pseudo);
modele = modeleText;
nom.setText(pseudo);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
p1.setLayout( new FlowLayout());
p2.setLayout(new FlowLayout());
p1.add(messages); c.add (p1, BorderLayout.NORTH);
p2.add(nom); p2.add(texte);
bOK.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent event){
String msg = messages.getText() +nom.getText()+ ">" + texte.getText()+"\n";
modele.setMessage(msg);
texte.setText("");
}
});
p2.add(bOK); c.add (p2, BorderLayout.SOUTH);
modele.addObserver(this);
this.pack(); this.setVisible(true);
}
public void update(Observable t, Object o)
{ messages.setText(modele.getMessage());
}
}
Import java.util.*;
public class Modele extends Observable {
private String message;
public String getMessage(){return message;}
public void setMessage(String message){
this.message = message;
setChanged();
notifyObservers();
}
}