CS 145 Lecture 20 – For Loops
Agenda
- what ?s
- alphabet
- fillDown
- indexOf
- program this
TODO
- Start homework 4, which is due before October 31. This is a full homework. You will want to use the full two weeks.
- Read sections 5.1, 2.3-2.4, and 4.2. Turn in next lecture a 1/4 sheet with questions, observations, and examples of algorithmic repetition you see in life.
Program This 1
- Write a method
fillDown
that prints a series as shown in the spreadsheet. What do you need for parameters? - Write a method
indexOf
that accepts aString
parameter and a character to locate. Return the index of the character. What would you do differently to report the index of the last location? What if you wanted to find the first character after a particular starting point?
Program This 2
Code
Alphabet.java
package lecture1017;
public class Alphabet {
public static void main(String[] args) {
for (char c = 'a'; c <= 'z'; ++c) {
System.out.print(c);
}
}
}
Spreadsheet.java
package lecture1017;
public class Spreadsheet {
public static void main(String[] args) {
fillDown(1, 3, 10);
}
public static void fillDown(int first,
int second,
int times) {
int difference = second - first;
System.out.println(first);
// System.out.println(second + 0 * difference);
// System.out.println(second + 1 * difference);
// System.out.println(second + 2 * difference);
// System.out.println(second + 3 * difference);
// System.out.println(second + 4 * difference);
for (int i = 0; i <= times - 2; ++i) {
System.out.println(second + difference * i);
}
}
}
StringUtilities.java
package lecture1017;
public class StringUtilities {
public static void main(String[] args) {
System.out.println(indexOf("Lincoln, Abraham", '!'));
}
public static int indexOf(String s, char c) {
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == c) {
return i;
}
}
return -1;
}
}
Grid.java
package lecture1017;
public class Grid {
public static void main(String[] args) {
for (int r = 1; r <= 8; ++r) {
for (int c = 1; c <= 8; ++c) {
System.out.printf("%3d", c * r);
}
System.out.println();
}
// for (int c = 1; c <= 8; ++c) {
// System.out.printf("%3d", c * 2);
// }
// System.out.println();
//
// for (int c = 1; c <= 8; ++c) {
// System.out.printf("%3d", c * 3);
// }
// System.out.println();
}
}
Haiku
from a geek on the road to his love:
It’s a whirlwind tour
But
He wants to see
It’s a whirlwind tour
But
i
can’t wait to get homeHe wants to see
u