CSC 212: Programming with Data Structures

Homework 1 Details

Credit for this assignment: Nick Howe


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.

Here is what your program should do: [Hints on creating your Java implementation are shown in red.]

  • Set up variable called cannedArray to hold a sequence of at least 8 strings that you make up. [You can make this a local variable inside main(), or better yet a class 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 cannedArray and place that value in another variable (or public static final field) -- call it numCanned or something like that. Do not assign the variable the number directly, such as 8. Compute the number from cannedArray itself. 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. [To compute the number of elements, use the .length property of arrays.]

  • Now ask the user how many times they want to interact and place the user's answer in a variable (what type of variable?) [Refer back to Lab 1.]

  • Create an array of strings called conversation to hold the interactions. [You will need to think about the total number of lines to be stored for the conversation, and supply this when you create the array.]

  • Print one 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. This will be 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 mirrorWordsFound, 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 mirrorWordsFound 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. [Consider using the class Random or the class Math.]

    • 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 to the conversation.

  • Finally, print a few blank lines, then print the entire conversation, using the array stored in conversation. Make sure there is one string printed per line, and no blank lines in between. [The code to do this should be defined as a separate method (with a for loop) 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 far 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? Is there a more creative or more human way to respond to the user? If you wish, you may submit a solution to the base assignment and a second program with more ambitious goals.