Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import exceptions.DernierMotRechercheException;
  2. import exceptions.DicoVideException;
  3. import exceptions.MotDejaPresentException;
  4. import exceptions.MotNonPresentException;
  5.  
  6. import java.util.Iterator;
  7. import java.util.TreeSet;
  8.  
  9. /**
  10. * Implémentation d'un dictionnaire en utilisant une ensemble ordonné.
  11. */
  12.  
  13. public class DicoTreeSet implements iDico {
  14.  
  15. private TreeSet<String> set;
  16.  
  17. public DicoTreeSet() {
  18. set = new TreeSet<>();
  19. }
  20.  
  21. @Override
  22. public boolean dicoVide() {
  23. return set.isEmpty();
  24. }
  25.  
  26. @Override
  27. public boolean appartient(String mot) {
  28. return set.contains(mot);
  29. }
  30.  
  31. @Override
  32. public void ajoutMot(String mot) throws MotDejaPresentException {
  33.  
  34. if (appartient(mot)){
  35. throw new MotDejaPresentException();
  36. }
  37.  
  38. set.add(mot);
  39. }
  40.  
  41. @Override
  42. public int nbMots() {
  43. return set.size();
  44. }
  45.  
  46. @Override
  47. public String premierMot() throws DicoVideException {
  48. if (dicoVide())
  49. throw new DicoVideException();
  50.  
  51. return set.first();
  52. }
  53.  
  54. @Override
  55. public String dernierMot() throws DicoVideException {
  56. if (dicoVide())
  57. throw new DicoVideException();
  58.  
  59. return set.last();
  60. }
  61.  
  62. @Override
  63. public String motSuivant(String mot) throws DernierMotRechercheException, MotNonPresentException {
  64. if (!appartient(mot)){
  65. throw new MotNonPresentException();
  66. }
  67.  
  68. if (set.higher(mot) == null){
  69. throw new DernierMotRechercheException();
  70. }
  71.  
  72.  
  73. return set.higher(mot);
  74. }
  75.  
  76. @Override
  77. public void suppressionMot(String mot) throws MotNonPresentException {
  78. if (!appartient(mot)){
  79. throw new MotNonPresentException();
  80. }
  81.  
  82. set.remove(mot);
  83. }
  84.  
  85. @Override
  86. public Iterator<String> iterator() {
  87. return set.iterator();
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement