CS 106 Winter 2016
Lab 01: Input and output
Question 1 Reading text
Download the file shakespeare.txt (Shakespeare's Sonnet #130) and add it to the data folder of an empty sketch. Now write a sketch as follows:
- Write empty setup() and draw() functions, as you would when beginning any sketch. This sketch doesn't need to do anything on the screen, so we're going to leave draw() empty, and put all the remaining code into setup().
- In setup(), use the built-in function loadStrings() to read the file shakespeare.txt into the sketch, and store the result in a local variable.
- Use println() to write the first line of the poem to the console.
- Write the text "Number of lines:" to the console. Then, on a new line, write out the number of lines in the poem.
- Write the text "Longest line:" to the console. Then, on a new line, write out the length of the longest line. The length of a string is the number of characters in it. You can find it by calling the length() method on the string.
- Write the text "Number of words:" to the console. Then, on a new line, write out the number of words in the poem. Use the built-in function splitTokens() to break each line into words, then count up the total number of words. Don't worry about punctuation.
- Optional: If you've got time, experiment with displaying the text of the poem for speed-reading purposes. Show each word for a brief period (a few frames) at the center of the screen, possibly followed by a blank screen for one frame. (If you do this, you'll have to put code into the draw() function, of course.)
If all goes well, you should see output like this in the Console window:
My mistress’ eyes are nothing like the sun; Number of lines: 14 Longest line: 48 Number of words: 123
Of course, you shouldn't just print these numbers out explicitly! You have to compute the stats from the input file. Note that to compute the longest line and the number of words, you'll probably need at least one loop.
Save this sketch as TextStats. Put it in a folder that will hold all your Lab 01 sketches.
Question 2 Writing text
- Create a new sketch with a setup() function that sets the sketch window to size 500×500.
- Add code so that when the user presses the mouse button, a small circle is added to the sketch at the location of the press. The user can click as many times as they want, and all the circles should stay visible. There's a nice, quick way to do this: leave the draw() function empty, and then write a mousePressed() hook that draws a circle.
- Now add code that records the locations of the mouse clicks to an external file called clicks.txt. In setup(), use the built-in function createWriter() to make a global variable that writes to the file. Every time you draw a circle, also print the x and y coordinates of the click to the file, not to the console.
- Finally, add a keyPressed() hook that flushes and closes the file, and then calls the built-in function exit() to close the sketch.
For example, the screenshot on the left might produce the clicks.txt file on the right.
126 206 168 125 211 393 429 387 425 185 307 118 63 391 291 293
The resulting sketch will be similar to the example shown in the online Processing documentation for the createWriter() function. But that example draws a dot every frame; you must draw a circle only in response to mouse clicks.
Save this sketch as RecordClicks. Put it in a folder that will hold all your Lab 01 sketches.
Question 3 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.
- Add a mousePressed() hook that adds a copy of the cow SVG at the location of the user's mouse press. All previous cows should remain in the pasture.
- Add a keyPressed() hook so that when the user pressed the spacebar, a screenshot of the sketch window is saved to the file output.png. Use the built-in function save().
- 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 in a folder that will hold all your Lab 01 sketches.
Submission
Remember to review the Code Style Guide and use Processing's built-in auto format tool. Then review the How To Submit document. At the top of all of your source files, be sure to include a comment with your name and student ID number. When you're ready, zip all the sketches created above (TextStats, RecordClicks, and OutToPasture) into a single archive called L01.zip and upload that file to LEARN.