-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrequencyTable.java
More file actions
173 lines (151 loc) · 5.42 KB
/
FrequencyTable.java
File metadata and controls
173 lines (151 loc) · 5.42 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
164
165
166
167
168
169
170
171
172
173
import java.util.ArrayList;
import java.util.Scanner;
/**
* The FrequencyTable class represents a table of frequencies of characters for a specific string
* @author Hudson Hadley
*/
public class FrequencyTable {
/**
* The list of characters (the index of these correspond to the frequencies array list
*/
private ArrayList<Character> characters;
/**
* The list of frequencies (the index of these correspond to the characters array list
*/
private ArrayList<Integer> frequencies;
/**
* A constructor which creates an empty table
*/
public FrequencyTable() {
this.characters = new ArrayList<>();
this.frequencies = new ArrayList<>();
}
/**
* A constructor which creates a table based on a string
* @param input the string we want to make a frequency table off of
*/
public FrequencyTable(String input) {
this.characters = new ArrayList<>();
this.frequencies = new ArrayList<>();
add(input);
}
/**
* A constructor which creates a table based on a list of chars and frequencies
* @param characters the characters in the table
* @param frequencies the times each character shows up
*/
public FrequencyTable(ArrayList<Character> characters, ArrayList<Integer> frequencies) {
this.characters = new ArrayList<>(characters);
this.frequencies = new ArrayList<>(frequencies);
}
/**
* Adds a character and updates the frequency of it (if it hasn't been seen yet, an element is added to both array
* lists)
* @param c the character we want to add
*/
public void add(char c) {
try { // Try to increment
increment(c);
} catch (IllegalArgumentException iae) { // If the character is not in the list, add it
characters.add(c);
frequencies.add(1);
}
}
/**
* Adds a string of characters and updates the frequency of each
* @param s the string we want to add
*/
public void add(String s) {
Scanner stringScanner = new Scanner(s);
stringScanner.useDelimiter("");
// Go through every character (whitespace included)
while (stringScanner.hasNext()) {
char character = stringScanner.next().charAt(0);
// We can't handle characters that can't be packed into a byte, so ignore those
add(character);
}
stringScanner.close();
}
/**
* Increments the index corresponding to a character by 1
* @param c the character we want to increment
* @throws IllegalArgumentException if the character is not in the array list
*/
private void increment(char c) throws IllegalArgumentException {
int index = getIndex(c);
frequencies.set(index, frequencies.get(index) + 1); // Increment by one
}
/**
* @param c the character we want the index of
* @return the index of the character c
* @throws IllegalArgumentException if the character is not found
*/
private int getIndex(char c) {
for (int i = 0; i < characters.size(); i++) {
if (characters.get(i).equals(c))
return i;
}
// If we reach the end and haven't found it, we have not recorded it
throw new IllegalArgumentException("Character not found");
}
/**
* @param c the character we want the frequency of
* @return the frequency of character c
* @throws IllegalArgumentException if c is not found
*/
public int getFrequency(char c) throws IllegalArgumentException {
return frequencies.get(getIndex(c));
}
/**
* @param index the index of the character we want the frequency of
* @return the frequency of characters.get(index)
* @throws IndexOutOfBoundsException if the index is out of bounds
*/
public int getFrequency(int index) throws IndexOutOfBoundsException {
if (index < 0 || index >= frequencies.size())
throw new IndexOutOfBoundsException("Invalid index");
return frequencies.get(index);
}
/**
* @param index the index of the character we want
* @return the character at characters.get(index)
* @throws IndexOutOfBoundsException if the index is out of bounds
*/
public char getChar(int index) throws IndexOutOfBoundsException {
if (index < 0 || index >= characters.size())
throw new IndexOutOfBoundsException("Invalid index");
return characters.get(index);
}
/**
* @return the size of the frequency table (i.e. how many characters we have tracked)
*/
public int getSize() {
return characters.size();
}
/**
* @return the greatest frequency in the table
*/
public int getMaxFrequency() {
int max = 0;
for (int frequency: frequencies) {
if (frequency > max)
max = frequency;
}
return max;
}
/**
* Converts the frequency table into a string
* @return A String representation of the frequency table
*/
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < characters.size(); i++) {
stringBuilder.append(characters.get(i));
stringBuilder.append(": ");
stringBuilder.append(frequencies.get(i));
stringBuilder.append("\n");
}
return stringBuilder.toString();
}
}