X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=exo3%2FEntiers.java;fp=exo3%2FEntiers.java;h=61b330f62f6c9284d89963ab2bbab03cccf545b5;hb=e8b96f4474afea669923602daf94d34be170634f;hp=0000000000000000000000000000000000000000;hpb=b42baaebf13977077ead822d691d0787fe562a15;p=Project_POO.git diff --git a/exo3/Entiers.java b/exo3/Entiers.java new file mode 100644 index 0000000..61b330f --- /dev/null +++ b/exo3/Entiers.java @@ -0,0 +1,108 @@ + +class Entiers { + private int int_array[]; + private int array_size; + private int current_size; + + public void setSize(int size) { + array_size = size; + } + + public int getSize() { + return array_size; + } + + public void setCurrentSize(int index) { + current_size = index; + } + + public int getCurrentSize() { + return current_size; + } + + Entiers(int size) { + int_array = new int[size]; + setSize(size); + setCurrentSize(0); + } + + public boolean inserer(int value) { + if (isFull()) { + System.out.println("Tableau plein"); + return false; + } + if (isEmpty()) { + int_array[0] = value; + current_size++; + return true; + } else { + for (int i = 0; i < getCurrentSize(); i++) { + if (int_array[i] == value) { + return true; + } else if (int_array[i] > value) { + for (int j = getCurrentSize(); j > i; j--) { + int_array[j] = int_array[j - 1]; + } + int_array[i] = value; + current_size++; + return true; + } + } + } + /** + * The current value to add is > to all elements in the tab. + * So add it at the end. + */ + int_array[getCurrentSize()] = value; + current_size++; + return true; + } + + private int binarySearch(int first, int last, int value) { + if (last < first) + //FIXME: should not return an integer + return -1; + int middle = (first + last) / 2; + if (value == int_array[middle]) + return middle; + else if (value > int_array[middle]) + return binarySearch((middle + 1), last, value); + return binarySearch(first, (middle -1), value); + } + + public boolean supprimer(int value) { + if (isEmpty()) { + System.out.println("Aucune valeur à supprimer"); + return false; + } + + for (int i = 0; i < getCurrentSize(); i++) { + if (int_array[i] == value) { + // Deleting the element in the tab + for (int j = i; j < getCurrentSize() - 1; j++) { + int_array[j] = int_array[j + 1]; + } + current_size--; + return true; + } + } + return true; + } + + private boolean isFull() { + return (getCurrentSize() >= getSize()); + } + + private boolean isEmpty() { + return (getCurrentSize() == 0); + } + + public void afficher() { + String className = this.getClass().getSimpleName(); + System.out.println("---- " + className + " ----"); + for (int i = 0; i < getCurrentSize(); i++) { + System.out.println("element " + i + " " + int_array[i]); + } + } + +}