
Computer Science 2311
Labs 13 and 14
Write a program complete with full documentation to: (1) read a series of input lines as text;
(2) count the number of occurrences of each letter in the input; and (3) produce as output the
following information.
• the echoed input
• the number of occurrences of each letter in the input, one letter per line
• the letter which occurred the most
• the letter which occurred the least
See the last page for a sample input file and the corresponding output.
PROGRAM DETAILS
• Initial source file: You may download a template for the source file from the 2310 home
page. The name of your source file should be LetCount.java
• Indexing an array with a character: As we have studied in class, array indices always start
with 0 in Java. However, in this program, it is logically useful to think of arrays as being
indexed by letters. For example, count[‘C’] could logically represent the number of times
C appears in the input. However, in order to map ‘C’ to a valid Java array index, I have
defined in the source file template a method addr that takes as input the character code, and
produces as output the correct array index. This mapping will produce 1 for the letter ‘A’,
2 for the letter ‘B’, etc. Thus within the program, count[addr(‘C’)] will represent the
number of times the letter C appears in the output. For the sample input on the last page,
count[addr('P')] should equal 10 at the end of the execution.
•
Determining if a character is a letter: We will assume that there are no lower case letters.
Use the appropriate static method or methods from the Character class (see page 376).
•
I/O information. I would suggest that you put your input data in a file and use I/O
redirection. You should be able to use the hasNext() method to determine when you’ve
reached the end of input and use nextLine() to store an input line into a String object from
where you can check each character and update your count array as appropriate.
•
Required methods (Documentation should be completed as each function is written).
Besides your main method which should declare needed variables and provide the control
loop for reading and processing the input you must define the following methods as a
minimum that your main method will use. All should be defined as public static in your
LetCount.java file.
1. A method that takes as parameters the count array and your String object containing the
most recently read line and goes character by character updating the count of each letter.
2. A method that takes the final counts and returns the letter that occurs the most
frequently.