-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrypFrequencyTable.java
More file actions
111 lines (92 loc) · 2.86 KB
/
CrypFrequencyTable.java
File metadata and controls
111 lines (92 loc) · 2.86 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
package crypSwing;
import java.util.Arrays;
/**
* This class represents a table of ciphertext letters and their frequency in a puzzle.
*
* @author Aaron Bitman
* @version 1.0 06/21/19
* @version 2.0 01/20/21
*/
public class CrypFrequencyTable {
private static final int LETTERS_IN_ALPHABET = 26;
private CrypFreqTableEntry frequencyTable[] = new CrypFreqTableEntry[LETTERS_IN_ALPHABET];
private String freqAlpha, freqByPop;
/**
* This constructor populates the table from a puzzle
* @param puzzle The puzzle in String format
*/
CrypFrequencyTable(String puzzle) {
final int CAPITAL_LETTER_OFFSET = 65;
int puzzleLength = puzzle.length();
char currentCharacter;
int index; //used for several purposes
for (index = 0; index < LETTERS_IN_ALPHABET; index++)
frequencyTable[index] = new CrypFreqTableEntry((char)(index + CAPITAL_LETTER_OFFSET));
for (index = 0; index < puzzleLength; index++) {
currentCharacter = puzzle.charAt(index);
if (currentCharacter >= 'A' && currentCharacter <= 'Z')
frequencyTable[(int) currentCharacter - CAPITAL_LETTER_OFFSET].increment();
}
freqAlpha = setFreqAlpha();
Arrays.sort(frequencyTable, CrypFreqTableEntry.freqTableEntryComparator);
freqByPop = setFreqByPop();
}
/**
* Sets the frequency table in alphabetical order.
* @return a String listing the frequency in alphabetical order.
*/
private String setFreqAlpha () {
String temp = "";
int index;
int occurrences;
for (index=0; index < LETTERS_IN_ALPHABET; index++) {
occurrences = frequencyTable[index].getOccurrences();
if (occurrences > 0) {
if (!temp.equals(""))
temp += ", ";
temp += frequencyTable[index].getLetter() + ": " + occurrences;
}
}
return temp;
}
/**
* Sets the frequency table in order from most frequent to least.
* @return a String listing the frequency in reverse order of frequency.
*/
private String setFreqByPop() {
String temp = "";
CrypFreqTableEntry entry;
int index = 0;
int count;
int previousCount = Integer.MAX_VALUE;
do {
entry = frequencyTable[index];
count = entry.getOccurrences();
if (count < previousCount) {
if (count == 0)
return temp;
previousCount = count;
if (temp != "")
temp += ", ";
temp += count + ": ";
}
temp += entry.getLetter();
index++;
} while (index < LETTERS_IN_ALPHABET);
return temp;
}
/**
* Gets the frequency table in order from most frequent to least.
* @return a String listing the frequency in reverse order of frequency.
*/
public String getFreqByPop() {
return freqByPop;
}
/**
* Gets the frequency table in alphabetical order.
* @return a String listing the frequency in alphabetical order.
*/
public String getFreqAlpha() {
return freqAlpha;
}
}