University of Washington, CSE 142
Lab 5: Strings, cumulative algorithms, while loops, fencepost loops
Except where otherwise noted, the contents of this document are
Copyright 2013 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp, Stuart Reges and Whitaker Brand
Basic lab instructions
-
Mouse over highlighted words if you're not sure what they mean!
-
Talk to your classmates for
help.
-
You may want to bring your textbook to future labs
to look up syntax and examples.
-
Stuck? Confused? Have a question? Ask a TA for
help, or look at the book or past .
-
Complete as much of the lab as you can within the allotted time. You don't need to keep working on these exercises after you leave.
-
Feel free to complete problems in any order.
-
Make sure you've signed in on the sign-in sheet before you leave!
Today's lab
Goals for today:
- use
String
s to represent and manipulate text data
- practice cumulative algorithms for complex computations
- use
while
loops for indefinite repetition
- exposure to fencepost and sentinel loop patterns
- Where you see this icon, you can click it to check the problem in Practice-It!
String
methods
Method name
|
Type
|
Returns....
|
charAt(index)
|
char
|
the character at the given index
|
indexOf(str)
|
int
|
the index where the start of the given String appears in this
string (or -1 if not found)
|
length()
|
int
|
the number of characters in this String
|
replace(str1, str2)
|
String
|
a new string with all occurrences of str1 changed
to str2
|
substring(index1, index2)
or substring(index1)
|
String |
the characters in this string from index1 (inclusive)
to index2 (exclusive); if index2 is omitted,
grabs till end of string
|
toLowerCase()
|
String |
a new string with all lowercase letters
|
toUpperCase()
|
String |
a new string with all uppercase letters
|
[an error occurred while processing this directive]
[an error occurred while processing this directive]
Cumulative algorithms
A cumulative algorithm involves incrementally accumulating a value by repeatedly adding, multiplying, dividing, etc., while storing the result in a variable.
Key aspects of a cumulative algorithm: A loop, and a variable declared outside the loop whose value is modified inside the loop.
Example: Cumulative algorithm to sum the numbers 1-100:
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum = sum + i;
}
System.out.println(sum);
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
Fencepost Loops
A fencepost loop is a common algorithmic pattern where you want to perform N tasks with N-1 things between them.
It's like a fence with N posts with N-1 wires between the posts.
To achieve this, place one "post" outside your loop, then alternate between "wires" and "posts" inside the loop.
Example:
System.out.print(1);
for (int i = 2; i <= 5; i++) {
System.out.print(".." + i);
}
[an error occurred while processing this directive]
[an error occurred while processing this directive]
while
Loops
for
loops are fantastic for when we know how many times we want to repeat something. But sometimes we won't know how many times we'll want to repeat something in advance! while
loops repeat indefinitely while a given condition is true.
while (test) {
statement(s);
}
Example:
int num = 1;
while (num < 5) {
System.out.print(num + " ");
num++;
}
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
If you finish them all...
If you finish all the exercises, try out
our Practice-It
web tool. It lets you solve Java problems from our Building Java
Programs textbook.
You can view an exercise, type a solution, and submit it to see if you
have solved it correctly.
Choose some problems from the book and try to solve them!