CS 40 - Lecture 18

David S. Taylor
Lecture 17 Recap

- Privacy Issues
- 100+ years ago: technology already threatened privacy.
- The threat has greatly accelerated in the last 10-15 years
- Do you care? (Different generations have different attitudes.)
Lecture 16 Recap (back to the technical stuff)

- Edge detection: If pixel color differs by a large amount from
neighboring pixels, color black, otherwise, color white
- Mirroring: Find distance from mirror line, then go the same
distance in the other direction
- Processing image parts: Check whether x- or y-value is >
(i.e. to the right or bottom of) a line, or < (i.e. to the left or top
of) a line
Plain Text
- Letters, numbers, special symbols
- No fonts, no colors, one size, no bold, no italic
- Commonly generated from scraping spreadsheets, word processors, emails,
web pages (i.e. copy and paste into Notepad)
- Easy to process
Example: Office Automation

Example: Office Automation
- Need GRE scores from a web page
- Need to paste each student's scores into my spreadsheet
- The formats are different
- A small Java program (180 lines) reduces hours of drudgery to 5 minutes
Reading Plain Text
- TextWorld for you
(download it now)
- Unzip it
- Choose it as a scenario in Greenfoot
- Very similar to PictureWorld
- Very similar to Scanner class in standard Java library, but
you need not worry about “exceptions”
- If you ever have to do this kind of work, either use these classes or
rewrite code to use Scanner
Loop Through Words
public class MyText2 extends Text
{
public int countWords()
{
int count = 0;
for (String word : getWords())
{
count++;
}
return count;
}
}
Lab 1: Running the Program
- Make a subclass
MyText2 of Text
- Edit the code as shown on the previous slide
- Click on Compile
- Right-click on
MyText2
- Select
new MyText2() and drag into white square
- Right-click on text icon and select
pick from the
Text superclass
- Pick
alice30.txt
- Right-click on the text icon and select
countWords
- What happens? ActiveLecture.org
Analyzing Plain Text
- Loop through words:
for (String word : getWords())
- Or loop through lines:
for (String line : getLines())
- Or loop through numbers (skipping everything that isn't a number)
for (double x : getNumbers())
Counting
Lab 2: Count the "the"s

- In Greenfoot, form subclass
MyText3
- Copy/paste/edit countWords → countThes
- Try it out. How many the are in Alice in Wonderland? ActiveLecture.org
- What if we want both the and The (and any
other variants)? (Try using equalsIgnoreCase instead of equals.) ActiveLecture.org
- Submit your code to Google Groups
Finding
Lab 3: The Longest Word in Alice

- Edit the MyText4 class
- Figure out what to do for ...;
- Figure out what to return
- Compile and run. What is the longest word?ActiveLecture.org
- Submit your code to Google Groups
Collecting
Lab 4: Short Words in Alice
- Consider MyText5
- Open it up, and look at the code.
- Make an instance of
MyText5 and drag it into the world.
- Right-click, then select
act
- A dialog will appear (because
act calls
pick). Pick alice30.txt
- A list icon will appear next to the text icon
- Right-click on that and click
Inspect
- Click
Inspect on the dialog
- You should see a count of the short words now
- Click
Inspect in that dialog.
- What do you see now?
- Close dialogs
- Remove duplicates: Invoke the
unique method on the list.
- How many unique short words are there? ActiveLecture.org
Ok, but...

Reminders

- Email a second project progress report this week.