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):
KeywordResponse
robotRobots are cool!
schoolHow is school?
friendFriends are important.
  • Random Responses Added: Two additional noncommittal responses (e.g., “That’s quite something.”, “How intriguing!“)
    were added to the getRandomResponse 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 CallIterationpsnbeforeafterReturnsNotes
findKeyword("She's my sister", "sister", 0)19" ""."9Found “sister”, surrounded by space & punctuation
findKeyword("Brother Tom is helpful", "brother", 0)10" "" "0Found “brother”, at start & space after
findKeyword("I can't catch wild cats.", "cat", 0)117" ""s"-1Found “cat”, but letter “s” follows (not standalone)
findKeyword("I know nothing about snow plows.", "no", 0)12"k""w"Found “no” in “know”, letters around
(Same as above)27" ""t"-1Found “no” in “nothing”, letter follows
findKeyword("Hello world", "hello", 0)10" "" "0Found “hello”, at start & space after (case ignored)
findKeyword("No, I mean no.", "no", 0)10" "","0Found first “no”, surrounded by start & punctuation
findKeyword("That is the plan", "an", 0)111"l"" "-1Found “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 in getResponse
    if you want it to handle cases like “I want a…” distinctly. Correction: The guide implies adding it, but the
    provided Magpie4.java already had transformIWantToStatement. The exercise asks to add transformIWantStatement.
    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.