CPE 103 Lab Activity

Lookup Tables

Given a string of text, count the number of occurrences of each vowel, consonant, punctuation (on the top row of the keyboard), numeric digit, and all other symbols. (See table below).

Here is the class skeleton:
public class KeyCounts
{
    
    /**
     * Constructor for objects of class KeyCounts
     * @param String of ascii characters
     */
    public KeyCounts(String keys)

    /** Access the frequencies 
     * @returns array the number of items counted in each category
     *  [DIGITS, VOWELS, CONSONANTS, PUNCTUATION, OTHER]
     */
    public int[] getCount()
    
    /** Calculate the total number of non-OTHER keys counted.
     *  The implementation should iterate over the array 
     *  returned from getCount().
     * @return sum of non-OTHER frequencies
     */ 
    public int getTotal()
}

You are to create TWO implementations.

Part 1

Create an implementation that uses this lookup table of 128 items and this enum definition:
       enum Categories { DIGIT, VOWEL, CONSONANT, PUNCTUATION, OTHER }

Hint: You don't need any if or switch statements.

Write a JUnit test class to verify the correctness of your implementation.

Part 2

One disadvantage of the previous implementation that it's difficult to verify that the lookup table was built correctly.
Since this particular problem is analyzing characters, an alternate solution would be to put all characters of a particular type into a string and then use indexOf() to see if a given character exists in that string.

Hint: Consider an array of strings, and then iterating over it.

The JUnit tests you wrote for part 1 should also work for part2.

Submit your work as described in the syllabus.

FAQ
Q: Part 1 directions said not to use "if" statement, does that apply to part 2 as well?
A: No. You may use a single-alternative "if", but not multiple-alternative or multiple if's.

Q: For part 2, do we need to create a string for OTHER that contains all other characters?
A: You could, but it would be a bit difficult because most of them don't have keyboard equivalents, you'd have to declare them as hex values, and there would be a lot of them.
So an acceptable workaround would be to assume that if the character doesn't fall into one of the first four categories, then it must be OTHER.

Q: Is the implementation for part 2 in a separate class?
A: Yes, that's probably the easiest way to organize your solution.


ASCII Table


Dec
Char
Category

0
CTRL-@

1
CTRL-A

2
CTRL-B

3...26
CTRL-C...CTRL-Z

27
CTRL-[

28
CTRL-

29
CTRL-]

30
CTRL-^

31
CTRL-_

32
 
33
!
PUNCTUATION
34
"

35
#
PUNCTUATION
36
$
PUNCTUATION
37
%
PUNCTUATION
38
&
PUNCTUATION
39
'

40
(
PUNCTUATION
41
)
PUNCTUATION
42
*
PUNCTUATION
43
+
PUNCTUATION
44
,

45
-
PUNCTUATION
46
.

47
/

48
0
DIGIT
49
1
DIGIT
50
2
DIGIT
51
3
DIGIT
52
4
DIGIT
53
5
DIGIT
54
6
DIGIT
55
7
DIGIT
56
8
DIGIT
57
9
DIGIT
58
:

59
;

60
<

61
=
PUNCTUATION
62
>

63
?

64
@
PUNCTUATION
65
A
VOWEL
66
B
CONSONANT
67
C
CONSONANT
68
D
CONSONANT
69
E
VOWEL
70
F
CONSONANT
71
G
CONSONANT
72
H
CONSONANT
73
I
VOWEL
74
J
CONSONANT
75
K
CONSONANT
76
L
CONSONANT
77
M
CONSONANT
78
N
CONSONANT
79
O
VOWEL
80
P
CONSONANT
81
Q
CONSONANT
82
R
CONSONANT
83
S
CONSONANT
84
T
CONSONANT
85
U
VOWEL
86
V
CONSONANT
87
W
CONSONANT
88
X
CONSONANT
89
Y
CONSONANT
90
Z
CONSONANT
91
[

92
\

93
]

94
^
PUNCTUATION
95
_  Underscore
PUNCTUATION
96
`  Back quote
PUNCTUATION
97
a
VOWEL
98
b
CONSONANT
99
c
CONSONANT
100
d
CONSONANT
101
e
VOWEL
102
f
CONSONANT
103
g
CONSONANT
104
h
CONSONANT
105
i
VOWEL
106
j
CONSONANT
107
k
CONSONANT
108
l
CONSONANT
109
m
CONSONANT
110
n
CONSONANT
111
o
VOWEL
112
p
CONSONANT
113
q
CONSONANT
114
r
CONSONANT
115
s
CONSONANT
116
t
CONSONANT
117
u
VOWEL
118
v
CONSONANT
119
w
CONSONANT
120
x
CONSONANT
121
y
CONSONANT
122
z
CONSONANT
123
{

124
|  Vertical bar

125
}

126
~
PUNCTUATION
127
DEL (Delete)