Initial commit for the first exercice on generic stack.
[Project_POO.git] / exo1 / Pile.java
1 import java.lang.reflect.Array;
2
3 /**
4 *
5 */
6 public class Pile<E> {
7 private E[] array;
8 private int array_size;
9 private int stack_head_index;
10
11 /**
12 * set the size of the internal array
13 * @param int size the size of the array
14 */
15 public void setSize(int size) {
16 array_size = size;
17 }
18
19 /**
20 * get the size of the internal array
21 * @return the integer size of the internal array
22 */
23 public int getSize() {
24 return array_size;
25 }
26
27 /**
28 * set the stack head index
29 * @param int index the stack head index
30 */
31 public void setHeadIndex(int index) {
32 stack_head_index = index;
33 }
34
35 /**
36 * get the stack head current index
37 * @return the integer stack head index
38 */
39 private int getHeadIndex() {
40 return stack_head_index;
41 }
42
43 /**
44 * [Pile description]
45 * @param int size [description]
46 */
47 @SuppressWarnings("unchecked")
48 Pile(int size) {
49 array = (E[])new Object[size];
50 setSize(size);
51 setHeadIndex(0);
52 }
53
54 /**
55 * [Pile description]
56 * @param int size [description]
57 */
58 @SuppressWarnings("unchecked")
59 Pile(Class<E[]> cl, int size) {
60 array = (E[])Array.newInstance(cl, size);
61 setSize(size);
62 setHeadIndex(0);
63 }
64
65 /**
66 * [empiler description]
67 * @param int value [description]
68 */
69 public void empiler(E value) {
70 if (!plein()) {
71 array[stack_head_index] = value;
72 stack_head_index++;
73 } else {
74 System.out.println("La pile est pleine");
75 }
76 }
77
78 /**
79 * [depiler description]
80 * @return [description]
81 */
82 public E depiler() {
83 if (!vide()) {
84 stack_head_index--;
85 return array[stack_head_index];
86 } else {
87 return null;
88 }
89 }
90
91 /**
92 * [plein description]
93 * @return [description]
94 */
95 private boolean plein() {
96 return (getHeadIndex() >= getSize());
97 }
98
99 /**
100 * [vide description]
101 * @return [description]
102 */
103 private boolean vide() {
104 return (getHeadIndex() == 0);
105 }
106
107 /**
108 * [afficher description]
109 */
110 public void afficher() {
111 for (int i = 0; i < getHeadIndex(); i++) {
112 System.out.println("element " + i + " " + array[i]);
113 }
114 }
115
116 /**
117 * The main() function
118 * @param String[] args main() function arguments array
119 */
120 public static void main(String[] args) {
121 Pile<Integer> stack = new Pile<Integer>(5);
122
123 stack.empiler(3);
124 stack.empiler(5);
125 stack.empiler(4);
126 stack.empiler(7);
127 stack.empiler(8);
128
129 stack.afficher();
130
131 System.out.println("Stack index " + stack.getHeadIndex());
132 System.out.println("Stack head value " + stack.depiler());
133 System.out.println("Stack index " + stack.getHeadIndex());
134 System.out.println("Stack head value " + stack.depiler());
135 System.out.println("Stack index " + stack.getHeadIndex());
136 System.out.println("Stack head value " + stack.depiler());
137 System.out.println("Stack index " + stack.getHeadIndex());
138 System.out.println("Stack head value " + stack.depiler());
139 System.out.println("Stack index " + stack.getHeadIndex());
140 System.out.println("Stack head value " + stack.depiler());
141 System.out.println("Stack index " + stack.getHeadIndex());
142 System.out.println("Stack head value " + stack.depiler());
143 System.out.println("Stack index " + stack.getHeadIndex());
144
145 stack.afficher();
146
147 Pile<String> stackStr = new Pile<String>(5);
148
149 stackStr.empiler("Bonjour");
150 stackStr.empiler("Salut");
151 stackStr.empiler("Hello");
152 stackStr.empiler("Hi");
153 stackStr.empiler("Hugh");
154
155 stackStr.afficher();
156
157 System.out.println("Stack index " + stackStr.getHeadIndex());
158 System.out.println("Stack head value " + stackStr.depiler());
159 System.out.println("Stack index " + stackStr.getHeadIndex());
160 System.out.println("Stack head value " + stackStr.depiler());
161 System.out.println("Stack index " + stackStr.getHeadIndex());
162 System.out.println("Stack head value " + stackStr.depiler());
163 System.out.println("Stack index " + stackStr.getHeadIndex());
164 System.out.println("Stack head value " + stackStr.depiler());
165 System.out.println("Stack index " + stackStr.getHeadIndex());
166 System.out.println("Stack head value " + stackStr.depiler());
167 System.out.println("Stack index " + stackStr.getHeadIndex());
168 System.out.println("Stack head value " + stackStr.depiler());
169 System.out.println("Stack index " + stackStr.getHeadIndex());
170
171 stackStr.afficher();
172 }
173 }