Due Date: Thursday of week 15

The following guidelines are expected for all homework submissions:

Problems for Assignment #4

Learning Outcomes: 1) implementing a sorting algorithm and testing it; 2) using a binary tree to implement an encoding scheme; 3) code a game in Java given a text description; 4) learning to use elements of the Java language to control the display of the terminal window; 5) using different data structures in concert to solve a problem

This problem set has one problem from chapter 3, one problem from chapter 8, and one that I've found from a completely different source. You will be working with sorting, binary trees, and representations of data for this assignment. Your task is to complete the following problems using the programs specified in each one. As always, when you are finished and ready to submit your work, do so in your repo. Here are the problems:

  1. Another kind of simple sort is the odd-even sort. The idea is to repeatedly make two passes through an array. On the first pass you look at all the pairs of items, a[j] and a[j+1], where j is odd (j = 1, 3, 5, …). If their key values are out of order, you swap them. On the second pass you do the same for all the even values (j = 2, 4, 6, …). You do these two passes repeatedly until the array is sorted.
    Replace the bubbleSort() method in bubbleSort.java [Listing 3.1] with an oddEvenSort() method. Make sure it works for varying amounts of data. You'll need to figure out how many times to do the two passes. The odd-even sort is actually useful in a multiprocessing environment, in which a separate processor can operate on each odd pair simultaneously and then on each even pair. Because the odd pairs are independent of each other, each pair can be checked – and swapped, if necessary – by a different processor. This makes for a very fast sort.
  2. Read the section on Huffman Coding in chapter 8 of the Lafore book [it begins on page 415]. Then write a program to implement Huffman coding and decoding. Use a binary tree to facilitate the decoding part. Your program should do the following:
    • Accept a text message, possibly of more than one line. Handle both upper and lower case, plus the space, newline, period and comma characters. That's up to 56 individual characters.
    • Count the number of occurrences of each each character and save the values to an array.
    • Put each of the characters and its count into a quasi-priority queue which has the lowest character count at the left end and the highest count at the right end.
    • Create a Huffman tree for this message using the process described on pp. 418 - 420. Following the diagrams can be a big help to understanding how this works.
    • Create the code table using the process described in the paragraph on page 421 under Creating the Huffman Code.
    • Encode your message into binary and display it on the screen.
    • Using your Huffman Tree, decode the message from binary back to text and display it.
    • If want to do so, the program could also display the Huffman tree after creating it. The ideas in Programming Projects 8.1, 8.2, and 8.3 might prove helpful. You can use String variables to store binary numbers as arrangements of the characters 1 and 0. Don't worry about doing actual bit manipulation unless you really want to.
  3. In the game Simon a four-colored disk can play a random sequence of tones and lights. Each color on the disk is actually a button that can be pressed, with a light behind it. As the game makes a tone, it also flashes the light with that tone. The game is for the user to press the buttons in the sequence that is played by the game's controller. The sequence starts with a single light/tone, then increases the number of lights/tones by adding one new trial to the sequence each time the player gets the entire order correct. The game is over when the user makes a mistake in entering the sequence.
    Using the letters R for Red, G for Green, Y for Yellow, and B for Blue, implement the game. When the player makes a mistake in the sequence, ask if they would like to play again. Implement the game as follows:
    Be sure that the sequence is random. Make your program display the string each time, letter by letter, with the letters separated by spaces. Try to make a short delay before each letter is displayed. Once all the letters in the sequence are displayed wait one second and then make your program output backspace characters to erase the displayed string. Then read the user's input and display each character as it is entered. When the user makes an error output a mistake message.