String s = "hello";
String t = s;
String u = "hello";
String v = "hel" + "lo"; String w = "Hello";
s==t true (why?)
s==u false
s==v false
s.equals(t) true
s.equals(u) true
s.equals(v) true
s.equals(w) false
basic statements
compound statements: {}
while, if statement bodies:
The next statement following the closing paren around the condition.
Normally it will be a compound statement, containing as many statements inside as you want.
If the paren is followed by a basic statement, that's it!
If the paren is followed by a ";", then the body is empty.
Lab 4 can be submitted this week, too.
This is a loop where we return out of the middle of it. This terminates both the loop and the method.
This can be tricky to follow! However, it is common (and useful) in practice.
Note that the return -1 isn't connected to the if statement through an else: the if is inside the loop, and the return is outside.
private int findIndex(String s) {
int i = 0;
while (i<keys.size()) {
if (keys.get(i).equals(s) ) {
return i;
}
i++;
}
return -1;
}
Here's the same loop without using return:
private int findIndex(String s) {
int i = 0;
boolean found = false;
while (i<keys.size() && ! found) {
if (keys.get(i).equals(s) ) {
found = true;
}
i++;
}
if (found) return i;
else return -1;
}
do we ever need more than one Random object??
What if we wanted responses to cycle, rather than to be chosen randomly?
We would have to add a field, perhaps named responsePosition, and change pickDefaultResponse() to be:
String response = defaultResponses.get(responsePosition);
responsePosition = (responsePosition + 1) % defaultResponses.size();
return response;
Documentation for ArrayList, HashSet
TechSupport "final" book version: answers depending on input
HashMap, HashSet
A problem: we go through the words of the sentence in "random" order!
How it is fixed in my version (the lab version)
javadoc. Awesome!
pld version of TechSupport (not the lab version): http://cs.luc.edu/pld/courses/170/spr09/zips/pldtech.zip
can use ArrayList OR HashSet
Note differences in how words are handled!
pld version: now has support for swapping strategy based on a parameter, preserveOrder
Lab 5: three passes, or levels:
The need to match the FIRST questionword makes the set implementation impossible.
Logic of going through every word in the sentence twice
public versus private
What is what? Methods are public only if they need to be called "outside" the class.
Why not make everything public? Because it's too confusing: public methods define the interface of a class, and the smaller the interface the easier to understand.
TechSupport: String.split() and what is really going on
Splitdemo, part of pldtech
use of inspector
Searching an array A for number x, using return:
Must be part of a larger method.
Searching an ArrayList<String> A for string s, return version:
i = 0;
while (i < A.length) {
if (A[i]==x) return i;
i++;
}
i = 0;break version of array search, printing the value of i:
while (i < A.size()) {
if (A.get(i).equals(s)) return i;
i++;
}
i = 0;Array version without return or break inside the loop: "big-condition" example
while (i < A.length) {
if (A[i]==x) break;
i++;
}
if (i < A.length) {
System.out.println("value found at i= " + i);
}
i=0;
while (i < A.length && A[i]!= x) {
i++;
}
return i;
But note that we've used short-circuiting feature of the && operator.
Compare this last one with the flag-variable version in B&K on page 101:
int i = 0;
boolean found = false;
while (i < notes.size() && ! found) {
String note = notes.get(i);
if (note.contains(searchString)) {
found = true; // and do NOT increment i !!
} else {
i++;
}
}
Why is it so important that we don't increment i above, when we find the entry we're looking for?
You can make this a "big-condition" loop like the previous example:
int i = 0;
while (i < notes.size() && ! notes.get(i).contains(searchString) ) {
i++;
}
What is going on in chapter 5?
Partially talked about:
What would it take to have a set of responses for each keyword,
and to pick different ones at random?
What would it take to match two-word pairs, in order?
Using docs to find the String methods we need.
Details of HashMap class, and its methods put(k,v) and get(k)
Logic of trying group1 words before group2 (question) words. More elaborate logic for multiple tests