Write a method indexOf that accepts a String and a char as parameters. Without using the builtin String.indexOf method, it returns the index at which the given character resides in the String. If no such character can be found, return -1.
Then, let’s seem some more example of loops:
Write lastIndexOf.
Write a routine that supports fill-down behavior as seen in a spreadsheet.
Write a text-based spinner/beach-ball/progress wheel.
packagelecture1028;
publicclassStringing {
publicstaticvoid main(String[] args) {
String s = "Abraham";
System.out.println(indexOf(s, 'm'));
System.out.println(indexOf(s, 'a'));
System.out.println(lastIndexOf(s, 'a'));
}
publicstaticint indexOf(String s, char c) {
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == c) {
return i;
}
}
return -1;
}
publicstaticint lastIndexOf(String s, char c) {
for (int i = s.length() - 1; i >= 0; --i) {
if (s.charAt(i) == c) {
return i;
}
}
return -1;
}
}
Chess.java
packagelecture1028;
publicclassChess {
publicstaticvoid main(String[] args) {
// For each rowfor (int r = 1; r <= 8; r++) {
// For each columnfor (int c = 1; c <= 8; c++) {
System.out.printf("%3d", r * c);
}
System.out.println();
}
}
}