Instructions
Practice Problems
Problem 1:   Secret Code
Problem 2 Insertion Sort
Problem 3 Eliza
Random Numbers - A Brief Note
5.3.5

Assignment 9

Goals: Practice designing loops, using and mutating the ArrayList

Instructions

The names of the classes must be exactly the same as specified. The same is the case for the names and types of the fields within the class, as well as the order in which they are defined and listed as the constructor arguments. This allows us to design our own Examples class that tests your program.

Make sure you follow the style guidelines that WebCAT enforces. For now the most important ones are: using spaces instead of tabs, indenting by 4 characters, following the naming conventions (data type names start with a capital letter, names of fields and methods start with a lower case letter), and having spaces before curly braces.

You will submit this assignment by the deadline using the Web-CAT submission system. You may submit as many times as you wish. Be aware of the fact that close to the deadline the WebCAT system may slow down to handle many submissions - so try to finish early.

With each homework you will also submit your log file named pair-user1-user2.txt where you replace user1 and user2 with the usernames of the two partners.

On top of both files you will have five lines of comments as follows:

// assignment 9

// partner1-last-name partner1-first-name

// partner1-username

// partner2-last-name partner2-first-name

// partner2-username

(In the text file you do not need the two slashes)

There will be a separate submission for each problem - it makes it easier to grade each problem, and to provide you with the feedback for each problem wou work on.

The two submissions will be organized as follows:

Due Date: Wednesday, March 19th, 11:59 pm.

Practice Problems

Work out these problems on your own. Save them in an electronic portfolio, so you can show them to your instructor, review them before the exam, use them as a reference when working on the homework assignments.

Problem 1: Secret Code

Create a project for your Problem 1.

You goal is to write a program that will encode and decode secret messages using a simple mapping of letters to a permutation of all letters. So if our alphabet had only five letters (a, b, c, d, and e) we could choose to encode them as (b, e, a, c, and d). Then the received message abe edc would be decoded as cab bed and the message bad ace would be sent encoded as ebc bad.

Download the file PermutationCode.java. It is a skeleton for your program. Your job is to design the three methods for which the purpose statements and the headers are already provided. Of course, you may need additional helper methods, and, of course, you will still follow the design recipe.

The class PermutationCode contains the key for the encoding and decoding of the messages, as well as the methods that perform these tasks. There are two constructors. One allows you to specify explicitly what will be your encoding permutation. This allows you to test your methods that encode and decode the messages. The second constructor generates a new encoding permutation that may be given to the parties that wish to communicate in secret.

Black Box Tests

The WebCAT may invoke these three methods in the PermutationCode class to test your work. It will make its own examples of data that will be used in the tests.

Problem 2 Insertion Sort

Create a project for your Problem 2.

We have seen the recursively defined insertion sort algorithm both in the first semester and also recently, using the recursively defined lists in Java. The main idea behind the insertion sort was that each new item has been inserted into the already sorted list.

We can modify this as follows:

Test your code on ArrayLists with elements of the type String (sorted lexicographically) and with elements of the type Integer sorted by their magnitude. You already have the necessary variants of Comparators from the binary search problem on the previous assignment.

Black Box Tests

The WebCAT will test your methods sortedInsert and insertSort defined in your ExamplesInsertionSort class on lists of Integerss and Strings.

Additional help: The Lab 9: Fall 2013 has a detailed explanation of the insertion sort with illustrations.

Problem 3 Eliza

Create a project with for your Problem 3.

Our goal is to train our computer to be a mock psychiatrist, carrying on a conversation with a patient. The patient (the user) asks a series of questions. The computer-psychiatrist replies to each question as follows. If the question starts with one of the following (key)words: Why, Who, How, Where, When, and What, the computer selects one of the three (or more) possible answers appropriate for that question.

If the first word is none of these words the computer replies I do not know or Why do you want to know? – a generic answer that does not depend on what was the question.

The file Interactions.java contains the code that will run your game, once you design it.

Black Box Tests

There will be no black box tests for this problem.

Instead, include in your submission the output of running the program with at least ten questions asked by the user. Save it as Transcript.txt.

Random Numbers - A Brief Note

For two of the problems you will need to generate random integers. You may have done so in your game earlier in the semester.

The code:

/** A random number generator */

Random rand = new Random();

produces an instance of a new Random a random number generator defined in Java libaries. The class defines a method rand.nextInt(int n) that returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), provided that n >= 0.

Use this method to produce any desired random values.

Use the checkOneOf and checkRange methods to test your outcomes.