Partiel de Conception et Programmation par objets
Transcription
Partiel de Conception et Programmation par objets
Partiel de Conception et Programmation par objets P. Laroque - UCP - M1 SIGE juin 2011 durée 2h, tous documents autorisés 1 Exercice UML (statique) Représenter la notion de fichier et de répertoire (dans un système de fichiers arborescent) à l’aide d’un diagramme de classes UML. Tout fichier devra pouvoir être créé (dans un répertoire donné), détruit, déplacé (dans un autre répertoire), et on devra évidemment pouvoir créer des sous-répertoires (à une profondeur quelconque). On écrira également le pseudo-code de la méthode permettant de calculer la place disque prise par un répertoire (et l’ensemble de ses fichiers) 2 Conversion de types entiers (env. 13 pts) Il s’agit d’écrire un programme permettant de convertir des valeurs entières entre une représentation décimale, binaire, hexadécimale ou sous forme de caractère (lorsque la valeur s’y prête). L’applet à définir a l’allure suivante : 1 Dès qu’un des champs est modifié par l’utilisateur, les autres doivent automatiquement se mettre à jour. Par exemple, si en partant de l’état ci-dessus, l’utilisateur entre “56” dans le champ “Hex”, on doit obtenir le résultat ci-dessous : On pourra avec profit utiliser, en plus des méthodes vues en cours, les méthodes suivantes : – Integer.valueOf(String n, int base) : renvoie un entier (Integer, pas int) correspondant à la chaı̂ne de caractères n considérée comme un entier codé en base base. Par exemple, Integer.valueOf(‘‘1001’’, 2) renvoie (un Integer qui vaut) 9. – Integer.toBinaryString(int i) : renvoie une chaı̂ne de caractères représentant l’entier i en base 2. Par exemple, Integer.toBinaryString(13) renvoie “1101”. – Integer.toHexString(int i) : renvoie une chaı̂ne de caractères représentant l’entier i en base 16. Par exemple, Integer.toHexString(13) renvoie “d”. – Integer.intValue(Integer i) : renvoie la valeur de i en tant qu’entier (int). – Enfin, les conversions int ↔ char se font par cast : char c = (char) 3; Barême indicatif – respect de l’aspect de la fenêtre : sur 3 – réactions aux événements : sur 3 – conversions entre types : sur 3 – connaissance des différences applet / application : sur 2 – niveau de fonctionnement du programme : sur 2 2.1 Correction indicative // $Id: Types.java 1905 2011-01-10 14:18:29Z phil $ import import import import javax.swing.*; java.awt.*; java.awt.event.*; java.util.*; 2 import java.io.*; /** c c * Application illustrant le m~ Acanisme d’interpr~ Atation des bits c c * suivants les diff~ Arents types num~ Ariques * @author [email protected] * @version $Id: Types.java 1905 2011-01-10 14:18:29Z phil $ */ public class Types extends JApplet { /** * Les boutons de la fen~ Aa tre */ protected JLabel lChar, lInt, lFloat, lBin, lHex; /** * Les champs texte de la fen~ Aa tre */ protected JTextField tChar, tInt, tFloat, tBin, tHex; ////////////////////////////////////////////////////////////// /** c * Constructeur : d~ Afinition et placement des composants, c (~ c * affectation des responsabilit~ As Acouteurs) */ public Types() { lChar lInt lFloat lBin lHex = = = = = new new new new new JLabel("Char JLabel("Int JLabel("Float JLabel("Bin JLabel("Hex "); "); "); "); "); tChar = new JTextField(32); tChar.setText(""); tChar.addActionListener(new CharHandler()); tInt = new JTextField(32); tInt.setText(""); tInt.addActionListener(new IntHandler()); tFloat = new JTextField(32); tFloat.setText(""); 3 tFloat.addActionListener(new FloatHandler()); tBin = new JTextField(32); tBin.setText(""); tBin.addActionListener(new BinHandler()); tHex = new JTextField(32); tHex.setText(""); tHex.addActionListener(new HexHandler()); } /** Initialisation de l’applet. Placement des composants */ public void init() { getContentPane().setLayout(new GridLayout(5,1)); JPanel p1,p2,p3,p4,p5; p1 = new JPanel(); // FlowLayout par defaut p2 = new JPanel(); p3 = new JPanel(); p4 = new JPanel(); p5 = new JPanel(); p1.add(lChar); p1.add(tChar); p2.add(lInt); p2.add(tInt); p3.add(lFloat); p3.add(tFloat); p4.add(lBin); p4.add(tBin); p5.add(lHex); p5.add(tHex); getContentPane().add(p1); getContentPane().add(p2); getContentPane().add(p3); getContentPane().add(p4); getContentPane().add(p5); setVisible(true); // placement g->d et h->b } c /** Met ~ A jour le champ "caract~ A¨re" en fonction de l’entier pass~ A. * Si la cvaleur enti~ A¨re ne correspond pas ~ A un code ASCII, affiche * une erreur. */ 4 void printChar(int i) { if (i>=0 && i < 256) tChar.setText(new Character((char)i).toString()); else tChar.setText("** ERROR **"); } c /** Gestion de la repr~ Asentation binaire */ class BinHandler implements ActionListener { public void actionPerformed(ActionEvent e) { try { String lu = tBin.getText(); Integer IVal = Integer.valueOf(lu,2); int iVal = IVal.intValue(); tInt.setText(IVal.toString()); float fVal = Float.intBitsToFloat(iVal); tFloat.setText(Float.toString(fVal)); String hVal = Integer.toHexString(iVal); tHex.setText(hVal); printChar(iVal); } catch(Exception nfe) { tBin.setText("** ERROR **"); } } } c c /** Gestion de la repr~ Asentation hexad~ Acimale */ class HexHandler implements ActionListener { public void actionPerformed(ActionEvent e) { try { String lu = tHex.getText(); Integer IVal = Integer.valueOf(lu,16); int iVal = IVal.intValue(); tInt.setText(IVal.toString()); tBin.setText(Integer.toBinaryString(iVal)); float fVal = Float.intBitsToFloat(iVal); tFloat.setText(Float.toString(fVal)); printChar(iVal); } catch(Exception nfe) { tHex.setText("** ERROR **"); } 5 } } c c /** Gestion de la repr~ Asentation d~ Acimale */ class IntHandler implements ActionListener { public void actionPerformed(ActionEvent e) { try { String lu = tInt.getText(); int iVal = Integer.parseInt(lu); String bVal = Integer.toBinaryString(iVal); String hVal = Integer.toHexString(iVal); tBin.setText(bVal); tHex.setText(hVal); int bits = Integer.valueOf(bVal,2).intValue(); float fVal = Float.intBitsToFloat(bits); tFloat.setText(Float.toString(fVal)); printChar(iVal); } catch(Exception nfe) { tInt.setText("** ERROR **"); } } } ~sentation c /** Gestion de la reprA sous forme de caract~ A¨re * (quand c’est possible) */ class CharHandler implements ActionListener { public void actionPerformed(ActionEvent e) { try { String lu = tChar.getText(); if (lu.length()>1) throw new Exception(); int iVal = new Character(lu.charAt(0)).hashCode(); tInt.setText(Integer.toString(iVal)); String bVal = Integer.toBinaryString(iVal); String hVal = Integer.toHexString(iVal); tBin.setText(bVal); tHex.setText(hVal); // int bits = Integer.valueOf(bVal,2).intValue(); } catch(Exception nfe) { tChar.setText("** ERROR **"); } 6 } } c /** Gestion de la repr~ Asentation sous forme de flottant */ class FloatHandler implements ActionListener { public void actionPerformed(ActionEvent e) { try { String lu = tFloat.getText(); float fVal = Float.parseFloat(lu); int iVal = Float.floatToRawIntBits(fVal); tInt.setText(Integer.toString(iVal)); String bVal = Integer.toBinaryString(iVal); String hVal = Integer.toHexString(iVal); tBin.setText(bVal); tHex.setText(hVal); printChar(iVal); } catch(Exception nfe) { nfe.printStackTrace(); tFloat.setText("** ERROR **"); } } } } 7