CS 40 - Lecture 18

Cay S. Horstmann
Lecture 17 Recap

- 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
- A true story
- I get GRE scores from a web page
- I need to paste each student's scores into my spreadsheet
- The formats are different
- I wrote a small Java program (180 lines)
- Reduces hours of drudgery to 5 minutes
Reading Plain Text
- I made a TextWorld for you
(download it now)
- 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 my 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
- Submit your solution in eCampus
Finding
Lab 3: The Longest Word in Alice

- Load MyText4.java
- Put it into TextWorld in the usual way
- Figure out what to do for ...;
- Figure out what to return
- Compile and run. What is the longest word?ActiveLecture.org
- Submit your solution in eCampus
Collecting
Lab 4: Short Words in Alice
- Load MyText5.java
- In Greenfoot, make a subclass in the usual way, and paste in 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
explore
- What happens?
- Remove duplicates: Invoke the
unique
method.
- How many unique short words are there? ActiveLecture.org
Ok, but...

Reminders

- Did you email me your project progress report?