In class, we briefly discussed chatbot programs. One well-­‐known chatbot is ELIZA, which was written by computer scientist Joseph Weizenbaum in the 1960s to parody a conversation with a psychotherapist. ELIZA used scripted replies to reply to simple sentences typed in by the user. For this project, you will implement a simple chatbot that mostly simulates ELIZA (note that, due to this version's extreme simplicity, many of its responses will be grammatically incorrect and may appear to be nonsensical; this is all right for our purposes).

Start by defining a new Java class named ChatBot. This class contains the following methods:

A public method named getResponse(). This method takes a String as its input, and returns a String value, according to the rules that follow (use the longestWord() method from step 1(b) to find the longest word X in the input String):

  • If the input contains the word “you”, the method should return “I’m not important. Let’s talk about you instead.” (Use indexOf() to test for the presence of “you”)
  • If X has 3 or fewer characters, return “Maybe we should move on. Is there anything else you would like to talk about?”
  • If X has exactly 4 characters, return “Tell me more about X”.
  • If X has exactly 5 characters, return “Why do you think X is important?”
  • Otherwise, return “Now we are getting somewhere. How does X affect you the most?”

A private method named longestWord(). This method takes a String (representing a sentence) as its input, and returns a String that matches the longest single word in the input. You may assume that the input does not contain any punctuation, and that the words are all separated by spaces.

We will use a Scanner to process the input String one word at a time. So far, we have used Scanner to read from the keyboard (System.in), but we can also tell a Scanner to use a String value as its input source.

In particular, we will use two new (to us) Scanner methods:

hasNext() — this method returns true if the Scanner’s input source has at least one more word left in it to be read; otherwise, the method returns false.

next() — this method returns the next “word” in the Scanner’s input source. A word is defined as a sequence of characters separated from what precedes or follows it by spaces. For example, the String “abc 123 def 456” has four words in it.

Your longestWord() method will follow the procedure below:

  • Create a new Scanner to read from the String parameter
  • Set the String variable longest to “” (the empty String)
  • While your Scanner has a next word in it:
    • Set temp to the value of the next word from the Scanner
    • If temp’s length is greater than longest’s length: Set longest to temp

When the loop ends, return longest.

Create a second class to test your ChatBot class. Your program should create a new ChatBot object and then ask "What would you like to talk about?" Until the user types "Goodbye", your program should enter a loop where it passes the user’s input to getResponse(), prints the String returned from that method, and then reads in a new sentence from the user.

A pseudocode algorithm for main() would look something like the following:

Create a new Scanner to read from the keyboard
Create a new ChatBot named e (for example)

prompt <- "What would you like to talk about?"
Print prompt input <- user input
while input does not equal "Goodbye":
print e.getResponse(input)
Read in user input

Sample Program Execution (program output is in boldface, user input is in italics)

What would you like to talk about?
how are you?
I'm not important. Let's talk about you instead.
I like Java programming Now we are getting somewhere. How does programming affect you the most?
bugs make me sad
Tell me more about bugs.
they mean the code is not right
Why do you think right is important?
CSE 110 is fun Maybe we should move on. Is there something else you would like to talk about?
Goodbye

Extra Credit Options

  • Modify your ChatBot class so that it keeps track of the number of times it has responded. After 10 responses, your ChatBot should always return the reply “Sorry, but our time is up. I can’t talk with you any longer.”
  • Modify your ChatBot class so that it occasionally (say, 30% of the time) returns a randomly-­‐generated standard reply to user input (one of at least five possible replies, like “LOL”, “OMG”, “You don’t say”, “Really?”, or “I see”).
  • To do this, add a new private method named getRandomResponse() to your ChatBot class. This method does not take any arguments, but it returns a String. Inside getResponse(), generate a random number between 1 and 10 each time the method is called. If the random number is 3 or less, getResponse() should call getRandomResponse() and return whatever value it generates. (+10 points)
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.