This assignment will give you practice with trees. You are to write a class MorseCoder that allows you to encode and decode Morse code.
The Morse code (see Table 6.10) is a common code that is used to encode messages consisting of letters and digits. Each letter consists of a series of dots and dashes; for example, the code for the letter a is •– and the code for the letter b is –•••. Store each letter of the alphabet in a node of a binary tree of level 5. The root node is at level 1 and stores no letter. The left node at level 2 stores the letter e (code is •), and the right node stores the letter t (code is –). The 4 nodes at level 3 store the letters with codes (••, •–, –•, ––). To build the tree (see Figure below), read a file in which each line consists of a letter followed by its code. The letters should be ordered by tree level. To find the position for a letter in the tree, scan the code and branch left for a dot and branch right for a dash. Encode a message by replacing each letter by its code symbol. Then decode the message using the Morse code tree.
Your class must implement the MorseCodeInterface provided and must contain the methods specified. You may need to provide a constructor to build the tree from the input file, encodings.txt. You may create private methods to implement the recursion for the different methods.
You are allowed to declare whatever data fields you want for your class, but you should try to keep these to a minimum. You can get by with just one data field (possibly root) and you shouldn’t need much more. If you have unnecessary data fields (data fields that could be turned into local variables), you will lose style points.
In terms of correctness, your class must provide all of the functionality described above. In terms of style, we will be grading on your use of comments, good variable names, consistent indentation and good coding style to implement these operations.
You should name your file MorseCoder.java and you should turn it in electronically from the “assignments” link on the class web page. You will need to have MorseCodeInterface.java, MorseNode.java, MorseMain.java and encodings.txt (In the same level as src) all in the same directory as your MorseCoder.java in order to run MorseMain. A sample output file, output.txt file is also attached.
You may work on this assignment in pairs.
You are going to implement a class that computes all the primes up to some integer n. The technique you are to use was developed by a Greek named Eratosthenes who lived in the third century BC. The technique is known as the Sieve of Eratosthenes. The algorithm is described by the following pseudocode:
create a queue and fill it with the consecutive integers 2 through n inclusive.
create an empty queue to store primes.
obtain the next prime p by removing the first value in the queue of numbers.
put p into the queue of primes.
go through the queue of numbers, eliminating numbers divisible by p.
} while (p < sqrt(n))
all remaining values in numbers queue are prime, so transfer them to primes queue
You are to use the Queue interface provided. When you want to construct a Queue object, you should make it of type LinkedQueue. These classes are included.
You should define a class called Sieve with the following public methods:
Sieve() - Constructs a sieve object.
void computeTo(int n) - This is the method that should implement the sieve algorithm. All prime computations must be implemented using this algorithm. The method should compute all primes up to and including n. It should throw an IllegalArgumentException if n is less than 2.
void reportResults() - This method should report the primes to System.out. It should throw an IllegalStateException if no legal call has been made yet on the computeTo method. It is okay for it to have an extra space at the end of each line.
int getMax() - This is a convenience method that will let the client find out the value of n that was used the last time computeTo was called. It should throw an IllegalStateException if no legal call has been made yet on the computeTo method.
int getCount() - This method should return the number of primes that were found on the last call on computeTo. It should throw an IllegalStateException if no legal call has been made yet on the computeTo method.
Your reportResults method should print the maximum n used and should then show a list of the primes, 12 per line with a space after each prime. Notice that there is no guarantee that the number of primes will be a multiple of 12. The calls on reportResults must exactly reproduce the format of the sample log. The final line of output that appears in the log reporting the percentage of primes is generated by the main program, not by the call on reportResults.