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

Assignment 9

Goals: Practice designing loops, using and mutating the ArrayList


The names of the projects and some of the project files must be exactly the same as specified in the assignment. Failure to do so makes it impossible for the graders to run your submission and results in immediate loss of at least 50% of the homework credit.

Make sure you follow the style guidelines for code indentation.

You will submit this assignment by the deadline using the Web-CAT submission system.

With each homework you will also submit your log file named pairxxx.txt where you replace xxx with your pair number.

On top of every file you submit you will have the names of both partners, and the pair number.

The .txt file will be the log of your work on this assignment. Each log entry will have data and time, who was present (one or both of the partners) and a short comment decribing what you were working on.

Submission Details:

Due Date: Friday, March 22th, 6:00 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 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 ExamplesIsertSort class on lists of Integerss and Strings.

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 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.