-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFor JavaTutor.java
More file actions
164 lines (125 loc) · 3.95 KB
/
For JavaTutor.java
File metadata and controls
164 lines (125 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
public class PrincipalFila{
public static void main (String [] args ){
System.out.println("===Teste Empilha===");
try{
FilaSimples fila1 = new FilaAprimorada(3);
fila1.enfileira(10);
fila1.enfileira(20);
fila1.enfileira(30);
//fila1.enfileira(40);
System.out.println(fila1.desenfileira());
System.out.println(fila1.desenfileira());
System.out.println(fila1.desenfileira());
System.out.println();
}catch (DesenfileraException e){
System.out.println (e);
}
catch (EnfileirarException e){
System.out.println (e);
//System.out.println("Limite eh " + e.getLimite());
//System.out.println("Item eh " + e.getItem());
}
System.out.println("===Teste Desempilha===");
try{
FilaSimples fila2 = new FilaAprimorada(2);
fila2.enfileira(100);
fila2.enfileira(200);
System.out.println(fila2.desenfileira());
System.out.println(fila2.desenfileira());
System.out.println(fila2.desenfileira());
System.out.println();
}catch (DesenfileraException e){
System.out.println (e);
}catch (EnfileirarException e){
System.out.println (e);
//System.out.println("Limite eh " + e.getLimite());
//System.out.println("Item eh " + e.getItem());
}
}
}
class FilaSimples {
private int inicio, fim;
private Object[] itens;
public FilaSimples(int tamanhoMax) {
this.itens = new Object[tamanhoMax];
this.inicio = 0;
this.fim = 0;
}
public void enfileira(Object novoItem) {
itens[fim] = novoItem;
fim = (fim + 1) % getTamanhoMax();
}
public Object desenfileira() {
Object item = itens[inicio];
itens[inicio] = null;
inicio = (inicio + 1) % getTamanhoMax();
return item;
}
int getInicio() {
return this.inicio;
}
int getFim() {
return this.fim;
}
int getTamanhoMax() {
if (itens != null)
return this.itens.length;
else
return -1;
}
}
abstract class FilaException extends RuntimeException{}
class FilaAprimorada extends FilaSimples {
int num_elementos;
int tamanhoMax;
public FilaAprimorada(int tamanhoMax){
super (tamanhoMax);
this.tamanhoMax = tamanhoMax;
}
@Override
public void enfileira(Object novoItem){
num_elementos += 1;
if(num_elementos> getTamanhoMax() )
throw new EnfileirarException(novoItem, getTamanhoMax());
super.enfileira(novoItem);
}
@Override
public Object desenfileira() {
if (num_elementos == 0)
throw new DesenfileraException();
num_elementos -= 1;
return super.desenfileira();
}
}
class EnfileirarException extends FilaException{
Object item;
int tamanho;
public EnfileirarException(Object item, int tamanho){
//super ("A fila já está cheia!");
this.item = item;
this.tamanho = tamanho;
}
@Override
public String toString(){
String texto;
texto = "A fila já está cheia! n/";
texto += "Limite eh " + this.tamanho;
texto += "Item eh " + this.item;
return texto;
}
public Object getItem(){
return item;
}
public Object getTamanho(){
return this.tamanho;
}
}
class DesenfileraException extends FilaException {
public DesenfileraException (){
//super ("A fila já está desenfileirada");
}
@Override
public String toString(){
return "A fila ja esta desenfileirada";
}
}