63 ? 16 xxxxxxxxxxxxxxxx 64 @ 0 65 A 18 xxxxxxxxxxxxxxxxxx 66 B 0 67 C 3 xxx 68 D 1 x 69 E 6 xxxxxx 70 F 0 71 G 0 72 H 4 xxxx 73 I 88 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxIn the above, each row refers to a single letter. The first thing in each row -- a number -- is the ASCII value of the letter. The second thing is the letter itself. The third is the number of times that letters appears in "Green Egs and Ham" and the fourth is one 'x' for each time the letter appears. ('I' appears frequently thanks to the name "Sam-I-Am".)
Next, copy a file containing "Green Eggs and Ham" into the directory you just created. The file is available on Unix in the file
/home/gtowell/Public/CS113/HW4/ham.txtNote that the "/" at the beginning of this file name is important. Be sure to get everything in this file name or the copy will fail. Use cp or scp to copy this file to your Unix account or laptop, respectively.
1 import java.io.FileReader; 2 import java.io.IOException; 3 4 public class ReadOne { 5 public static void main(String[] args) { 6 try { 7 FileReader r = new FileReader("ham.txt"); 8 if (r.ready()) { 9 int int_read = r.read(); 10 char char_read = (char) int_read; 11 System.out.println(int_read + " " + char_read); 12 } 13 } 14 catch (IOException e) { 15 System.out.println("Problem: " + e); 16 } 17 } 18 }(Line numbers are included for the discussion below, they are not part of the code)
The Java FileReader class does exactly what you might expect, it reads files; to get it stated as on line 7, we give it the name of the file to be read. Then, just prior to actually reading anything, we ask the file reader, on line 8, if there is anything to read. If there is something to read, on line 9 we actually read it. The read returns the ASCII value of the character, so on line 10 we convert that number into an actual character so that on line 11 we can print both the actual character and its ASCII value.
Java requires that anything that might have problems of a particular form be between "try" and "catch". For example, line 7 might have a problem if the file being opened did not exist. Similarly, line 9 might have a problem if the file disappeared between opening and line 9. If one of these bad things happen, the program immediately goes down to the catch (line 14) and executes whatever code is in the catch block (line 15). You should almost always have a print statement within the catch block. Sometimes it makes sense to have other things as well. So, all of this code that does the actions you actually want is in a block between the try (line 6) and the catch (line 14). We will discuss "try" and "catch" in class. The easiest thing to do will be to follow the pattern in the ReadOne program. That is make "try {" the first thing in the main method andcatch (IOException e) { System.out.println("Problem: " + e); return; }the last.
With the counts collected, all you have to do is create the output. Some suggestions about that.
63 16 64 0 65 18 66 0 67 3 68 1 69 6 70 0 71 0 72 4 73 88where the first column is the position in the array (which is also an ASCII value) and the second column is the count.
63 ? 16 64 @ 0 65 A 18 66 B 0 67 C 3 68 D 1 69 E 6 70 F 0 71 G 0 72 H 4 73 I 88To get the character given the location in the array, consider line 10 in the above sample reader program.
You should have at least 3 files in your HW4 directory: LetterCount.java, ham.txt and Readme. (You might also have .class files.)
cd cd CS113 mkdir HW4Once you have made the HW4 directory in Unix, open a terminal on you own computer and in that terminal use "cd" to navigate to the directory containing your work for this assignment. Assuming you use the same directory structure on your own computer and in the lab, this process can be accomplished with the following commands
cd cd CS113 cd HW4Then use the scp command to copy each of the files you want to submit from your computer to the lab. For example:
scp Readme UNIX_NAME@goldengate.cs.brynmawr.edu:CS113/HW4/ReadmeAs always, when you read "UNIX_NAME" put in your UNIX user name. Also, with each scp command you will need to enter your UNIX password.
cd cd CS113 /home/gtowell/bin/submit -c 113 -d HW4 -p 4In response to the submit command you should see a series of messages ending with:
Submitting archive... Submission complete! Submission timestamp is 2023-08-08-15-30-28-EDT.