Activity 2
- Keywords Added:
- “dog” or “cat” → “Tell me more about your pets.”
- “Mr. Finkelstein” → “He sounds like a good teacher.” (Teacher name example)
- Check for empty input → “Say something, please.”
- Three student-chosen keywords (example below):
Keyword | Response |
---|---|
robot | Robots are cool! |
school | How is school? |
friend | Friends are important. |
-
Random Responses Added: Two additional noncommittal responses (e.g., “That’s quite something.”, “How intriguing!“)
were added to thegetRandomResponse
method’s pool, increasing variety. -
Prioritization (Multiple Keywords): The response triggered depends on the order of
if-else if
statements in
getResponse
. The first condition that matches the input determines the response. To change priority, reorder the
code blocks. For “My mother has a dog but no cat,” the response depends on which keyword check (no
,mother
,dog
,
cat
) comes first. -
Keyword Within Word: Using
indexOf
is problematic because it matches substrings.- “I know…” matches “no”.
- “smothered” matches “mother”.
- This leads to incorrect responses because
indexOf
doesn’t respect word boundaries.
Activity 3 Questions & Exercises Summary
findKeyword
Tracing:-
The
findKeyword
method correctly identifies keywords only when they appear as distinct words, not as substrings
within other words. It checks characters before and after the potential match. -
Trace Table:
-
Method Call | Iteration | psn | before | after | Returns | Notes |
---|---|---|---|---|---|---|
findKeyword("She's my sister", "sister", 0) | 1 | 9 | " " | "." | 9 | Found “sister”, surrounded by space & punctuation |
findKeyword("Brother Tom is helpful", "brother", 0) | 1 | 0 | " " | " " | 0 | Found “brother”, at start & space after |
findKeyword("I can't catch wild cats.", "cat", 0) | 1 | 17 | " " | "s" | -1 | Found “cat”, but letter “s” follows (not standalone) |
findKeyword("I know nothing about snow plows.", "no", 0) | 1 | 2 | "k" | "w" | Found “no” in “know”, letters around | |
(Same as above) | 2 | 7 | " " | "t" | -1 | Found “no” in “nothing”, letter follows |
findKeyword("Hello world", "hello", 0) | 1 | 0 | " " | " " | 0 | Found “hello”, at start & space after (case ignored) |
findKeyword("No, I mean no.", "no", 0) | 1 | 0 | " " | "," | 0 | Found first “no”, surrounded by start & punctuation |
findKeyword("That is the plan", "an", 0) | 1 | 11 | "l" | " " | -1 | Found “an” in “plan”, letter precedes |
-
Re-implement Activity 2: The keyword detection logic from Activity 2 was redone using
findKeyword
to avoid the
substring matching problem. -
Prepare for Next Activity: Simple keyword detection is limited. Better chatbots recognize patterns (like “I like
something”) and transform the input, which is the focus of the next activity.
Activity 4 Questions & Exercises Summary
-
“I want something” Transformation: This was implemented. The code now transforms “I want fried chicken” into “
Would you really be happy if you had fried chicken?“. Place this check before the “I want to” check ingetResponse
if you want it to handle cases like “I want a…” distinctly. Correction: The guide implies adding it, but the
providedMagpie4.java
already hadtransformIWantToStatement
. The exercise asks to addtransformIWantStatement
.
The logic should be placed carefully, usually checking for the more specific “I want to” before the general “I
want”. -
“I something you” Transformation (Example Problem): The simple transformation (“Why do you something me?”) can
be grammatically incorrect.- Statement: “I admire you.” → Response: “Why do you admire me?” (Works okay)
- Statement: “I gave you the book.” → Response: “Why do you gave me the book?” (Incorrect tense)
- Improvement: Fixing this requires more complex grammar analysis (parsing, conjugation) to adjust the verb
tense, which is beyond the scope of this basic lab. A simple fix isn’t readily available without significantly
more code.