-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalavra.java
More file actions
75 lines (63 loc) · 1.72 KB
/
Copy pathPalavra.java
File metadata and controls
75 lines (63 loc) · 1.72 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
public class Palavra implements Comparable<Palavra>
{
private String texto;
public Palavra (String texto) throws Exception
{
if(texto==null||texto == "")
throw new Exception("Digite um valor válido.");
this.texto = (String)texto;
}
public int getQuantidade (char letra)
{
int conte = 0;
for(int i = 0; i < texto.length(); i++){
if(texto.charAt(i)==letra)
conte++;
}
return conte;
}
public int getPosicaoDaIezimaOcorrencia (int i, char letra) throws Exception
{
byte iezima = -1;
for(byte j = 0; j < texto.length(); j++){
if(texto.charAt(j)==letra)
iezima++;
if(iezima == i)
return j;
}
throw new Exception("Não foi encontrada a iézima posição");
}
public int getTamanho ()
{
return this.texto.length();
}
@Override
public String toString ()
{
return this.texto;
}
@Override
public boolean equals (Object obj)
{
if(this==obj) return true;
if(obj==null) return false;
if(this.getClass() != obj.getClass()) return false;
Palavra dat = (Palavra)obj;
if(!this.texto.equals(dat.texto)) return false;
//if(this.texto != dat.texto) return false;
else return true;
}
@Override
public int hashCode ()
{
int ret = 8;
ret = 7 * ret + String.valueOf(this.texto).hashCode();
if (ret<0) ret=-ret;
return ret;
}
public int compareTo (Palavra palavra)
{
if(this.texto != palavra.texto) return -1;
return this.texto.compareTo(palavra.texto);
}
}