PilaDeEnteros.java
1 /**
2 * PilaDeEnteros.java
3 *
4 * @author Luis Alejandro Bernal Romero
5 * Implementa el concepto de Pila para solo enteros.
6 * Es una ejemplo de caracter didáctico lo que explica su falta de generalidad y eficiencia
7 */
8 public class PilaDeEnteros {
9 //Constantes
10 public static final int MAX = 100;
11
12 // Excepciones
13 public class UnderFlowException extends Exception{
14
15 }
16
17 public class OverFlowException extends Exception{
18
19 }
20
21 // Atributos
22 private int[] arr; // Arreglo que contiene los elementos
23 private int i; // Número de elementos
24
25 // Métodos
26 public PilaDeEnteros(){
27 i = 0;
28 arr = new int[MAX];
29 }
30
31 public void apilar(int n) throws OverFlowException{
32 if(i + 1 >= MAX){
33 throw new OverFlowException();
34 }
35 arr[i] = n;
36 i++;
37 }
38
39 public int desempilar() throws UnderFlowException{
40 if(i - 1 < 0){
41 throw new UnderFlowException();
42 }
43 i--;
44 return arr[i];
45 }
46 }
