Computer Science 202
Introduction to Programming
Fall 2012, The College of Saint Rose
Lecture 16: More Advanced Loops
Date: Tuesday, October 23, 2012
Agenda
- Announcements
- Flowcharts for Lab 6 should be wrapped up
- Java for Lab 6 due tomorrow night
- In-class exerise: completing the "Longest Word" example by
convering the flowchart to Java
We will follow the same steps we used to build the flowchart. But
having the flowchart available will make it more clear how to
structure the program. It is still essential that we implement the
program and test it incrementally. If we make a mistake (and we
will), we will know it has something to do with the last incremental
change we made since the most recent successful attempt.
Here are those steps:
- Read in one word.
- Read in words inside a loop (forever).
- Read in words until someone enters the empty string (
""
).
- Add a variable that keeps track of the number of words read in.
Initialize it before your main loop, increment it in an appropriate
place inside your main loop, and print it out after the loop.
- Add a variable to keep track of the longest word. Update it in
your main loop any time someone enters a word longer than the
longest seen previously. Print it out at the end. Don't worry yet
about handling ties.
- Now worry about that case. When you detect a tie for the
longest word, you should append the new word to the string
containing the longest word(s). Note that we will now need to have
a separate variable to remember the length of the longest word,
since the string containing the longest word(s) may now contain
multiple words.
- Add a check at the end to make sure you only print the longest
word(s) if at least one word was entered before
""
was
entered. If not, print a separate message to that effect.
When finished, save and submit your Java program under "Lecture 16
In-Class Exercise" on Blackboard. If you worked with a partner,
make sure both names are included in the "Comments" in Blackboard so
both partners receive credit. Again, this submission will earn you 10
lecture assignment points.
Use a similar process when converting your "Grade Summary" flowchart
to Java.
- Programming Project 2: Hot and Cold discussion/demo
- More for loops
Lecture Assignment 16
Due at the start of class, Thursday, October 25.
Please submit answers to these questions
in Blackboard by the start of class. We will discuss these questions at
the start of class, so no late submissions are accepted.
- Gaddis Checkpoint 4.14, p. 211 (2 points)
- Gaddis Find the Error Question 2, p. 239 (2 points)
- Gaddis Algorithm Workbench Question 3, p. 239 (2 points)
- Gaddis Algorithm Workbench Question 9, p. 240 (2 points)
- Gaddis Algorithm Workbench Question 10, p. 240 (2 points)
Examples