Code cleanup : fix C style array declaration.
[Project_POO.git] / exo4 / Entiers.java
... / ...
CommitLineData
1
2@ClassPreamble (
3 author = "Jérôme Benoit",
4 date = "05/03/2009"
5)
6class Entiers extends Structure {
7 private int[] int_array;
8 private int array_size;
9 private int current_size;
10
11 public void setSize(int size) {
12 array_size = size;
13 }
14
15 public int getSize() {
16 return array_size;
17 }
18
19 public void setCurrentSize(int index) {
20 current_size = index;
21 }
22
23 public int getCurrentSize() {
24 return current_size;
25 }
26
27 Entiers(int size) {
28 int_array = new int[size];
29 setSize(size);
30 setCurrentSize(0);
31 }
32
33 public boolean inserer(int value) {
34 if (isFull()) {
35 System.out.println("Tableau plein");
36 return false;
37 }
38 if (isEmpty()) {
39 int_array[0] = value;
40 current_size++;
41 return true;
42 } else {
43 for (int i = 0; i < getCurrentSize(); i++) {
44 if (int_array[i] == value) {
45 return true;
46 } else if (int_array[i] > value) {
47 for (int j = getCurrentSize(); j > i; j--) {
48 int_array[j] = int_array[j - 1];
49 }
50 int_array[i] = value;
51 current_size++;
52 return true;
53 }
54 }
55 }
56 /**
57 * The current value to add is > to all elements in the tab.
58 * So add it at the end.
59 */
60 int_array[getCurrentSize()] = value;
61 current_size++;
62 return true;
63 }
64
65 private int binarySearch(int first, int last, int value) {
66 if (last < first)
67 //FIXME: should not return an integer
68 return -1;
69 int middle = (first + last) / 2;
70 if (value == int_array[middle])
71 return middle;
72 else if (value > int_array[middle])
73 return binarySearch((middle + 1), last, value);
74 return binarySearch(first, (middle - 1), value);
75 }
76
77 public boolean supprimer(int value) {
78 if (isEmpty()) {
79 System.out.println("Aucune valeur à supprimer");
80 return false;
81 }
82 for (int i = 0; i < getCurrentSize(); i++) {
83 if (int_array[i] == value) {
84 // Deleting the element in the tab
85 for (int j = i; j < getCurrentSize() - 1; j++) {
86 int_array[j] = int_array[j + 1];
87 }
88 current_size--;
89 return true;
90 }
91 }
92 return true;
93 }
94
95 private boolean isFull() {
96 return (getCurrentSize() >= getSize());
97 }
98
99 private boolean isEmpty() {
100 return (getCurrentSize() == 0);
101 }
102
103 public void afficher() {
104 String className = this.getClass().getSimpleName();
105 System.out.println("---- " + className + " ----");
106 for (int i = 0; i < getCurrentSize(); i++) {
107 System.out.println("element " + i + " : " + int_array[i]);
108 }
109 }
110
111 public void compacter(int nElements) {
112 if (current_size - nElements > 0) {
113 // Remove the last nElements
114 current_size -= nElements;
115 } else {
116 current_size = 0;
117 }
118 }
119}