Commit | Line | Data |
---|---|---|
bc48a285 JB |
1 | |
2 | class Entiers { | |
3 | private int int_array[]; | |
4 | private int array_size; | |
5 | private int current_size; | |
6 | ||
7 | private void setSize(int size) { | |
8 | array_size = size; | |
9 | } | |
10 | ||
11 | private int getSize() { | |
12 | return array_size; | |
13 | } | |
14 | ||
15 | private void setCurrentSize(int index) { | |
16 | current_size = index; | |
17 | } | |
18 | ||
19 | private int getCurrentSize() { | |
20 | return current_size; | |
21 | } | |
22 | ||
23 | Entiers(int size) { | |
24 | int_array = new int[size]; | |
25 | setSize(size); | |
26 | setCurrentSize(0); | |
27 | } | |
28 | ||
29 | public boolean inserer(int value) { | |
61363e13 | 30 | if (isFull()) { |
bc48a285 JB |
31 | System.out.println("Tableau plein"); |
32 | return false; | |
33 | } | |
61363e13 JB |
34 | if (isEmpty()) { |
35 | int_array[0] = value; | |
36 | current_size++; | |
37 | return true; | |
38 | } else { | |
39 | for (int i = 0; i < getCurrentSize(); i++) { | |
40 | if (int_array[i] == value) { | |
41 | return true; | |
42 | } else if (int_array[i] > value) { | |
43 | for (int j = getCurrentSize(); j > i; j--) { | |
44 | int_array[j] = int_array[j - 1]; | |
45 | } | |
46 | int_array[i] = value; | |
47 | current_size++; | |
48 | return true; | |
49 | } | |
50 | } | |
bc48a285 | 51 | } |
61363e13 JB |
52 | /** |
53 | * The current value to add is > to all elements in the tab. | |
54 | * So add it at the end. | |
55 | */ | |
56 | int_array[getCurrentSize()] = value; | |
bc48a285 JB |
57 | current_size++; |
58 | return true; | |
59 | } | |
60 | ||
61 | private int binarySearch(int first, int last, int value) { | |
62 | if (last < first) | |
5731ae5f | 63 | //FIXME: should not return an integer |
bc48a285 JB |
64 | return -1; |
65 | int middle = (first + last) / 2; | |
66 | if (value == int_array[middle]) | |
67 | return middle; | |
9f6084ea | 68 | else if (value > int_array[middle]) |
bc48a285 | 69 | return binarySearch((middle + 1), last, value); |
527a553c | 70 | return binarySearch(first, (middle - 1), value); |
bc48a285 JB |
71 | } |
72 | ||
73 | public boolean supprimer(int value) { | |
61363e13 | 74 | if (isEmpty()) { |
bc48a285 JB |
75 | System.out.println("Aucune valeur à supprimer"); |
76 | return false; | |
77 | } | |
78 | ||
61363e13 JB |
79 | for (int i = 0; i < getCurrentSize(); i++) { |
80 | if (int_array[i] == value) { | |
81 | // Deleting the element in the tab | |
82 | for (int j = i; j < getCurrentSize() - 1; j++) { | |
83 | int_array[j] = int_array[j + 1]; | |
84 | } | |
85 | current_size--; | |
86 | return true; | |
87 | } | |
88 | } | |
89 | return true; | |
bc48a285 JB |
90 | } |
91 | ||
61363e13 | 92 | private boolean isFull() { |
bc48a285 JB |
93 | return (getCurrentSize() >= getSize()); |
94 | } | |
95 | ||
61363e13 | 96 | private boolean isEmpty() { |
bc48a285 JB |
97 | return (getCurrentSize() == 0); |
98 | } | |
99 | ||
100 | public void afficher() { | |
5731ae5f JB |
101 | String className = this.getClass().getSimpleName(); |
102 | System.out.println("---- " + className + " ----"); | |
30632042 | 103 | for (int i = 0; i < getCurrentSize(); i++) { |
bc48a285 JB |
104 | System.out.println("element " + i + " " + int_array[i]); |
105 | } | |
106 | } | |
107 | ||
108 | public static void main(String[] args) { | |
109 | Entiers integer = new Entiers(5); | |
110 | ||
111 | integer.inserer(1); | |
112 | ||
113 | integer.afficher(); | |
114 | ||
115 | integer.inserer(12); | |
116 | ||
117 | integer.afficher(); | |
118 | ||
119 | integer.inserer(3); | |
120 | ||
121 | integer.afficher(); | |
122 | ||
123 | integer.inserer(0); | |
124 | ||
125 | integer.inserer(1); | |
126 | ||
127 | integer.afficher(); | |
128 | ||
129 | integer.supprimer(12); | |
130 | ||
131 | integer.afficher(); | |
132 | System.out.println("Current size " + integer.getCurrentSize()); | |
133 | ||
134 | integer.supprimer(1); | |
135 | ||
136 | integer.afficher(); | |
137 | System.out.println("Current size " + integer.getCurrentSize()); | |
138 | ||
bc48a285 JB |
139 | } |
140 | } |