CS 106 Winter 2017
Lab 02: Input and output
Question 1 Singles
As in Lab 01, you will practice writing a small, self-contained function in a "dummy" sketch (i.e., a sketch that doesn't really do anything). Create a folder called L02. In that folder, create a new sketch called Singles.
In the sketch, write a function called compact(). The function takes a String parameter as input and returns a new String in which all of the whitespace has been filtered out, leaving the words in the string jammed together. For example, we might expect compact( "The winter of our discontent" ) to return the string "Thewinterofourdiscontent".
The simplest way to do this is to break the string apart using the built-in function splitTokens() and add all the tokens up (in a loop) into a new string that is returned as the answer. But if you're adventurous, there's a clever one-line solution (i.e., the only thing in the body of the function is a single return statement) that uses the built-in function join(). See if you can figure it out!
You are encouraged, but not required, to test your compact() function by writing a setup() function that calls it and checks whether the correct answer is returned.
Question 2 Out to pasture
- Download the following two files and add them to an initially
empty sketch:
- cow.svg (thanks to vectorportal.com)
- pasture.jpg (thanks to Malcolm Carlaw)
- Add a setup() function that sets the size of the sketch to 640×480. Then load the cow SVG and pasture photo into global variables, and draw the pasture photo to the sketch window.
- Add an empty draw() function. You won't ever need to put anything into it.
- Add a mousePressed() hook that adds a copy of the cow SVG at the location of the user's mouse press. The cows can all be the same size (they don't have to be scaled, as in the sample image above). All previous cows should remain in the pasture.
- Add a keyPressed() hook so that when the user presses the spacebar, a screenshot of the sketch window is saved to the file output.png.
- Optional: If you've got time, modify the sketch so that cows are drawn smaller towards the top of the sketch, and larger towards the bottom, as in the screenshot above. Make sure they're scaled uniformly (not stretched horizontally or vertically).
Save this sketch as OutToPasture. Put it the L02 folder.
Question 3 Grep
In the early days of Unix, there was a command called grep that was used to search for a particular word or phrase in a text file. It was the ancestor of the "Find in page..." feature in your web browser or "Spotlight Search" on the Mac desktop. For example, if the file input.txt contains the first few lines of the book The Brothers Karamazov:
Alexey Fyodorovitch Karamazov was the third son of Fyodor Pavlovitch Karamazov, a land owner well known in our district in his own day, and still remembered among us owing to his gloomy and tragic death, which happened thirteen years ago, and which I shall describe in its proper place. For the present I will only say that this “landowner”—for so we used to call him, although he hardly spent a day of his life on his own estate—was a strange type, yet one pretty frequently to be met with, a type abject and vicious and at the
Then performing a grep for the word "land" would output the lines on which that word appears:
Karamazov, a land owner well known in our district in his own day, “landowner”—for so we used to call him, although he hardly spent a
Note that grep doesn't care that in one case "land" is part of the longer word "landowner"—it doesn't try to be smart about what constitutes a word, and naively matches patterns of letters.
In Processing, we can use the built-in contains() method of the String class to see whether a given string contains another as a substring:
String s = "This is just a test string. Testing, testing"; if( s.contains( "ring" ) ) { // The pattern was found! How do you want to respond? }
You will write a sketch that behaves like grep. Given an input text and a search phrase, both stored in external files, you will find all the lines in the text that contain the search phrase and output them to a third file. Proceed as follows:
Create a new sketch called Grep. Give it a setup() function. This is the only function you'll need to write for this question. All the steps here will refer to code you must add to the body of setup().
Add code to load these two text files into the sketch and store their contents in two different arrays of strings, declared as local variables inside setup() (don't use any global variables for this question!).
Create a PrintWriter object that will write into a file called output.txt.
Now loop over the array corresponding to the lines of text in input.txt. For each line that contains the search word (which is stored in the other array), write that line out to output.txt. Use the contains() method to check this, as demonstrated above.
When the loop is complete, flush and close the PrintWriter.
As a final flourish, include a call to exit() as the last step in setup(). The sketch has done everything it's going to do, so there's no point keeping it running.
When you run a correct sketch, the sketch folder should contain a file output.txt that holds lines from Brothers Karamazov containing the word "land" (the two lines above and a few others). You should not draw anything in the sketch window, or write any text to the console; all output goes to the file. You are encouraged to test your sketch by substituting in other text in input.txt and other words in word.txt. We may do the same during marking.
Put your Grep sketch in your L02 folder.
Submission
When you are ready to submit, please follow these steps.
If necessary, review the Code Style Guide and use Processing's built-in auto format tool. You do not need to use the precise coding style outlined in the guide, but whatever style you use, your code must be clear, concise, consistent, and commented.
If necessary, review the How To Submit document for a reminder on how to submit to LEARN.
Make sure to include a comment at the top of all source files containing your name and student ID number.
Create a zip file called L02.zip containing the entire L02 folder and all its subfolders.
Upload L02.zip to LEARN. Remember that you can (and should!) submit as many times as you like. That way, if there's a catastrophe, you and the course staff will still have access to a recent version of your code.