This lab is intended to be relatively short and simple, and to exercise things that you have seen before. It is adapted from Savitch and Carrano, page 531.

ELIZA is a program that was created in the 1960s as an attempt to mimic a psychoanalyst. It would read a sentence from the user, look for some keywords in the sentence, and then make a response based on the keywords. For this assignment you will do a highly simplified version of ELIZA.

Write a program that reads one-line sentences from a user and responds to them. After each response it reads another sentence, responding to that, and so on until it reads a line that only contains the word "quit".

To formulate a response, first look at the sentence and find the longest word. Let's call that word X. Use the following rules.

  1. If X has length 4, then respond with "Tell me more about X."

  2. If X has length 5, then respond with "Why do you think X is important?"

  3. If X has length 6 or more, then respond with "Now we are getting somewhere. How does X affect you the most?"

  4. If the length of X is less than 4, then respond with "Maybe we should move on. Is there something else you would like to talk about?"

Nonfunctional requirements

Software designers use the term nonfunctional requirements to refer to requirements that are not directly related to what the software does, but rather refer to how the software is built. (Requirements that tell what the software does are called functional requirements.)

This program is partly to exercise in using arrays. You are required to put the words into an array as you read them in. Then, to find the longest word, search through the array. Although using an array is not really required just to handle the functional requirements of this assignment, it might well be needed for a more sophisticated version, so using an array makes the software easier to modify.

This program is also a further exercise in using methods. Some methods are listed below. You are required to write and use them. It is not acceptable to write the entire program as a single main method.

Getting words

Use the Java Scanner class to get lines and words. Get a line (using getLine). If the line is "quit", then end the program. Otherwise create a new scanner object that reads the line. If that scanner is called wordscan, then wordscan.next() skips over white space, then gets a string up to white space or the end of the line, and returns that string as its value. That is not quite a word because it might contain punctuation. Remove punctuation marks from its end. (Just do a loop and skip over all nonletters at the end. If there are any nonletters to remove, get a substring. Recall that, if s is a string then s.substring(i, j) is the substring of s from index i to index j-1.

Using methods

Break the program up into methods as outlined below. Provide a contract with each method. You are always free to add additional methods, but provide a contract for every method, whether you added it or not. Indent the contract the same amount as the method heading. Make sure that the contract describes what each argument is for. Refer an argument by its name, not using pronouns or phrases such as the string.

Include the following methods. Each is only briefly described according to what it does. Choose a reasonable name for each. Pass arguments that are needed.

  1. Return the result of removing punctuation from the end of a given word.
  2. Extract an array of words from a string. Return the array.
  3. Find and return the longest string in an array of strings.
  4. Return the response when the longest word has length 4. (This method needs to be told the longest string.)
  5. Return the response when the longest word has length 5. (This method needs to be told the longest string.)
  6. Return the response when the longest word has length 6 or more. (This method needs to be told the longest string.)
  7. Return the response when the longest word has length 3 or less. (This method needs to be told the longest string.)
  8. Return the response to a given sentence. (Notice that this method will use others above.)

Testing and submitting your program

Inexperienced programmers typically tackle a programming problem by trying to write the entire program before they start testing. Experienced programmers know that doing so is not a good idea, and that you will get the job done much sooner by using successive refinement. Set yourself a subgoal. Write the program to accomplish that subgoal. Test what you have, and do not move on until it works. Then set another subgoal, which is a little closer to the overall goal, and continue, until you finally have solved the entire problem. That tends to sound to time-consuming to inexperienced programmers, so they embark on an approach that, in reality, takes much longer. Do not be fooled. You will be surprised how much successive refinement speeds up the development process. Here are some suggested subgoals.

  1. Write the program so that it just reads each line and responds with the same line. At least this tests getting the lines and stopping at "quit". Test it.

  2. Write methods 2 and 3 above. Modify the program so that it responds to each line with the longest word on the line. Do not worry about removing punctuation yet. This tests that getting words and finding the longest word are working. Test it.

  3. Write methods 4 and 8 above. But make method 8 always use the response that is right when the longest word has length 4, even if the length is not 4. Modify the program so that it responds with "Tell me more about X." where X is the longest word. Test it.

  4. Write methods 5, 6 and 7 above. Modify method 8 so that it gives different responses, based on the length of the longest word. Test it.

  5. Write method 1 above, to remove punctuation, and make the program use it. (Where is the right place to use it? Think about that before you jump in and just put it anywhere.) Test the program. It should do the entire job now.

When the program works, paste it into the box below and push the submit button.