A2.0 Audio:
We have two objectives with this assignment:
We haven't explained the first one before.
Now for our demo problem. For this problem, the goal is to read a plain text file but to read it character by character. That is, we want to be able to do something like this:
// Set up the file for reading char by char: openFile ("somerandomfile.txt"); // Get the first one so that we can test entry into loop: char c = getNextChar (); // As long as getNextChar() doesn't signal the end, stay in loop: while (c != '#') { System.out.print (c); c = getNextChar (); } System.out.println ();Thus, we want to write the methods openFile() and getNextChar(). But that's not really explaining what the challenge is. Consider:
A2.1 Exercise: At this point do not write any code. Try to sketch out a solution in pseudocode. See how far you get before reading through the solution.
And now, on to your assignment. Good luck!
1.1 2.2 3.3 2.8 3.06 3.08 0.3 0.6 0.7 0.8 0.5 0.2
Average=2.2 for 1.1 2.2 3.3 Average=2.8 for 2.8 Average=3.07 for 3.06 3.08 Average=0.5 for 0.3 0.5 0.7 0.1 0.9
// Set it up for char-by-char reading: IOTool.openFileByChar (filename); // Each char is returned as an integer: int k = IOTool.getNextChar (); while (k >= 0) { char c = (char) k; // Do something with this char k = IOTool.getNextChar (); }In LetterFrequency.java write code to perform a frequency count of the letters from a to z occuring in a particular file. (Ignore punctuation and anything that's not 'a' through 'z' lowercase.) Then print out, for each letter, the percentage of occurences. Thus, if 12% of the letters consists of e's, then you would print 12% next to 'e'. Test your program with sample files of your making before testing it on Shakespeare's MacBeth, for which some of the letter frequencies should turn out to be:
a: 7.0 b: 1.0 c: 2.0 d: 4.0 e: 12.0 ...You will also need IOTool.java. Think about converting each char to its equivalent integer, and performing a calculation to let you index a suitable array.
What does a sequence of numbers have to do with functions? Since the x-values already go left to right (monotonic), we only focus on only the y-values. So, for example if the data is:
double[] y = {2, 3, 4, 5, 7, 6};we can see that the values increase up through 7, and then decrease after that. Which means this data is unimodal. If on the other hand, the data were
double[] y = {7, 5, 4.5, 3.9, 2.2, 1};it's monotonic (decreasing).
Suppose the data is in an array y:
double[] y = ... (some method produces the data) ... // Your goal: determine whether y is monotonic or unimodal: boolean isMono = isMonotonic (y); System.out.println ("isMonotonic=" + isMono); boolean isUni = isUnimodal (y); System.out.println ("isUnimodal=" + isUni);Thus, your goal is to write code for the methods isMonotonic() and isUnimodal() that take the array y to determine if the data in y is monotonic or unimodal.
Before you rush off and starting typing in code into methods with those names, let's step back and simplify your task. First, suppose you wrote two methods with the following structure:
static boolean isMonotonicIncr (double[] y, int i, int j) { // Check to see if the y-values in the index range i through j of // the array y are monotonically increasing (never descending). } static boolean isMonotonicDecr (double[] y, int i, int j) { // Check to see if the y-values in the index range i through j of // the array y are monotonically decreasing (never increasing). }For example, if the data happened to be:
double[] y = {2, 3, 4, 5, 7, 6};and if i=0, j=4 then we can see that
double[] y = {2, 3, 4, 5, 7, 6};the range of values from positions 0 to 4 in the array are in fact monotonically increasing. Likewise, if i=4, j=5 we can see that
double[] y = {2, 3, 4, 5, 7, 6};the corresponding segment of the array is monotonically decreasing. Does this tell you how to discover whether an array is unimodal?
Download FunctionAnalysis.java and add the methods needed. You can see that the line after the first comment produces the array. Change the function to function2 etc to test with other data.
b < e < l < o < wSimilarly, the word beyond is unimodal. With modest modifications, the code in the previous problem can be applied to an array of chars. In this part of the assignment, identify the longest monotonic and unimodal words. Download WordAnalysis.java to get started. You will also need WordTool.java and wordsWithPOSAndPron.txt.
A2.6 Audio/Video: