Comp 170 Week 7

Exam 1

Chapter 5: read
Theme: bigness requires libraries, encapsulation, separation

== versus .equals()

    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


statements

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.



Revisiting "return" loops

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;
}


Documentation for Random

        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!

  1. bring up doco page
  2. examine format
  3. make some tweaks, rebuild docos
/**
 *   special javadoc comments
 */

@version
@author
@param
@return


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

  1. Can flip this with frobPreserveOrder
  2. Note use of while(true) and break, instead of while ( ! finished)
  3. Why can't we call frobPreserveOrder during program execution?
String library:


Wednesday:


how frobPreserveOrder() works, and where.

break is a bit like return, except it only terminates the current loop, not the current method.
Note use of while(true) and break, instead of while ( ! finished). Some people (purists?) object to the while(true) on the grounds that it's misleading: the loop is clearly not intended to be infinite! Others argue that, hey, at least while(true) warns you that something fishy is going to happen (like a break or return) in the loop body.



demoWordSet is there to explore with the Inspector


Lab 5: three passes, or levels:

  1. look for keyword match
  2. look for FIRST questionword match in input sentence
  3. random match

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.


not done:

phonebook-hm

HashMap v StringMap: in the phonebook-hm example (hm for HashMap), what gets complicated?



TechSupport: String.split() and what is really going on

Splitdemo, part of pldtech
use of inspector


Searching

Searching an array A for number x, using return:
Must be part of a larger method.

	
i = 0;
while (i < A.length) {
if (A[i]==x) return i;
i++;
}
Searching an ArrayList<String> A for string s, return version:
	i = 0;
while (i < A.size()) {
if (A.get(i).equals(s)) return i;
i++;
}
break version of array search, printing the value of i:
	i = 0;
while (i < A.length) {
if (A[i]==x) break;
i++;
}
if (i < A.length) {
System.out.println("value found at i= " + i);
}
Array version without return or break inside the loop: "big-condition" example
What does it return if the value is not found?
	
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++;
    }

Note that the condition gets to be most of the loop!

Summary:
    break/return loops: these violate our usual expectations for while loops
    big-condition loops: somewhat ungainly
    flag-variable loops: trickier than they look, and they look plenty tricky!

This is why, when given a choice, I often end up choosing a break/return style.


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