Assignment 2


Objectives and example

 

A2.0 Audio:
 

We have two objectives with this assignment:

  1. Explore the CS learn it by yourself ethic.
  2. Our usual "let's learn to solve a problem".

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.

Now work through the solution
 

And now, on to your assignment. Good luck!


Assignment problems

 

  1. HTML is a formatting language used to build webpages. It's quite strange at first but is surprisingly easy to learn. Your goal here is to learn basic HTML. You can avail of any tutorial you can find or use this one. For this assignment, create a simple homepage with at least the following elements:
    • Your picture. (You can include more.)
    • A brief bio.
    • Use a table in some way.
    • Include links to some useful webpages, favorite sites etc.
    • Include some audio. For this purpose you will need to look up how to use the "audio" tags in HTML.
    The homepage should be called index.html. To view your homepage (or any HTML file), go to your browser, to the File menu, then to Open-File, and open the file you created. You may have to resize your images using the "height" and "width" attributes. The zip file for this assignment needs to include all the relevant files (including index.html, your picture etc).

  2. Write a program called LineAverages.java that will do the following:
    • The data, in a file, will consist of real numbers. Each line can have any number of numbers, as in
          1.1 2.2 3.3
          2.8
          3.06 3.08 
          0.3 0.6 0.7 0.8 0.5 0.2
          
    • Your program should print the average on each line, along with the data. Thus, for the above data, the output should be:
          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
          
    Test your program with different files of your own making, along with data1.txt and data2.txt. This is a bit challenging, so let's provide some hints. First, go back to the module on I/O and see that you can read a file line-by-line where each line is read in as a string. So, you'll know how to get, for example, "1.1 2.2 3.3" as a string. Next, you know can you can use a (different) Scanner on a string to extract numbers inside a string (again, see the examples in the module). What you'll need to do is put these ideas together.

  3. The following sample shows how you can read a text file one char at a time:
            // 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.

  4. This question and the next are linked. Start by downloading, compiling and executing FunctionExamples.java. You will also need DrawTool.java. Notice that the red curve is rising while the blue one first rises then falls. A curve that never descends (the red example) or never rises is called monotonic. A curve that has exactly one "hump" is called unimodal, like the blue one. A unimodal curve can have its hump up, like a hill (the blue curve), or have its unimodality "down" like a valley (not shown in the examples). Your goal is: given a sequence of numbers, determine whether the sequence is monotonic or unimodal, or neither.

    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.

  5. What's interesting is that the same definitions can be applied to a sequence of letters, that is, a word. Thus, the word below is monotonic increasing because
    	b   <   e   <   l   <  	o   <   w
        
    Similarly, 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.

  6. For this question, you will merely write plain text (in assignment2.pdf ) to explore computers in the movies:
    • Which famous movie made in the 1960's made the computer a villain? What was the iconic name of the computer?
    • List 5 movies with computers playing significant role.
    • You've heard the term pixel. What is a pixel?
    • Then, read this article to learn how computer animation works.
 

A2.6 Audio/Video:



© 2017, Rahul Simha