Below is a more detailed description of what your program should do. This is more or less a pseudocode for the assignment, and is given this week only to get you started. You may wish to compare it to a Python program which follows the same pseudocode. For this particular assignment, the Python code and the Java code are (almost) line-by-line translations of each other. This may not be exactly the case for more complex programs, since one language may provide a feature that is not implemented in exactly the same way in the other.
Here is what your program should do: [Hints on creating your Java implementation are shown in red. See the Python-to-Java handout for more details and hints.]
- Set up variable called cannedlist to hold a sequence of at least 8 strings that you make up. [You can make this a local variable inside main(), or a constant field declared public static final outside main as we did with TAXRATE. To fill in the individual string values, you can use a number of individual assignment statements as we did in lab 1. Alternately, there is a syntax for declaring an array and its elements at the same time: String[] numbers = {"one","two","three","etc."}; ]
- Compute the number of strings in cannedlist and place that value in another variable (or public static final field) -- call it num_canned or something. Do not assign the variable the number directly, such as 8. Compute the number from cannedlist itelf. That way if you add a new canned response, you don't have to change the number farther down in your code. This is one aspect of modular programming. [See the Python-to-Java handout for how to compute how many elements are in an array.]
- Now ask the user how many times they want to interact and place the user's answer in a variable.[The user's response will come in as a String and you will need to convert it to an int.]
- Create an array of strings called conversation to hold the interactions. [You will need to compute the total number of lines to be stored for the conversation, and supply this when you create the array.]
- Print some message to start the conversation. This can be a random pick from the canned sentences, or something special to start. Add it to the beginning of the conversation array.
- Now use a loop to provide the number of interactions requested.
This is the outer loop. The body of the outer loop will take
a user response and then generate the computer's reply based upon
it.
- Get a response from the user. You can use the nextLine() function of class Scanner, which returns a string. Put the response in a variable called user, and add it to the conversation.
- Use the String.split() function to separate the words in the user string. Place the resulting sequence of words in the variable userwords.
- You will construct the computer's response out of the user's words, replacing certain words to make a question. Create a string to hold the constructed response, called computer. Initialize it to be empty.
- Define a variable mirror_words_found, and set it to false initially.
- Now loop through each word in userwords constructing a
the response string with mirror words swapped out. Mirror words
are words like "I", "You", "are", and "am", which can be swapped
for "You", "I", "am", and "are", respectively. This is
the inner loop. [You can use a
for-each loop structure (Murach pp.328-329)]
- Set the response word equal to the user's word initially.
- Test the user's word to see if it matches any of the mirror words. [Use the String.equals() method in an if/else construct (Murach ch. 4, p. 128). Do not use == since this tests object equality, not string equality.]
- If you match on a mirror word, replace the response word with the mirror's opposite and set mirror_words_found to true
- Append the response word (mirrored or not) to the full response string.
- Swap the punctuation: replace periods and exclamation marks with question marks. [Use String.replace().]
- If no mirror words were found, pick a canned response at random instead of the response just constructed.
- Add your response to the conversation record, and display it on the screen.
- After the body of the outer loop, print a goodbye message to the screen and also add the same message out to the conversation.
- Finally, print a few blank lines, then print the entire conversation, using the list stored in conversation. Make sure there is one string printed per line, and no blank lines in between. [This code to do this should be defined as a separate method that is called at this point in main().]
Feel free to depart somewhat from the above, so long as your program accomplishes the same goals. For instance, if a different set of variables seems more useful to you, you may design your program accordingly. Or you could add new methods that perform small tasks that occur more than once, like adding a line to the conversation record. Part of your grade will be based upon the elegance and efficiency of your solution. Extra credit may also be granted in cases where students extend the assignment beyond what is required, although this is at sole discretion of the professor. For example, there are types of input where the mirror word process described above will not work properly. Can you fix the program so it works on these? If you wish, you may submit a solution to the base assignment and a second program with more ambitious goals.
Does the Java syntax still seem awkward? It may, but the extra precision becomes useful in larger programs. In addition to the syntax changes, the program description has changed in a few other minor ways as well. For example, Java does not offer a String.count() method, so the detection of mirror words had to be changed slightly. Also, your program will have to precompute the total number of lines in the conversation in advance rather than appending lines one at a time.
If that last restriction seems awkward, there is another refinement you can attempt. An arbitrary-length conversation is difficult to do with arrays, which require their size to be allocated in advance. The Vector class works somewhat like an array but can be extended in size as needed. The program could be rewritten to use a Vector<String> to store the conversation. This gets into areas of Java we haven't covered yet, which is why it is not part of the main assignment. If you attempt it, please turn in the basic version as well.