Wed Jan 14-15
Due: Before Wed Jan 21
The purpose of this lab is to implement utility classes, which includes implementing static fields and methods, adding documentation in the form of Javadoc comments and generating APIs using the Javadoc tool, and using JUnit to test your code. This lab will be graded for style and correctness.
There are two programming problems to this lab.
The goal of this problem is to create a small utility class that can convert temperatures between degrees Celcius and degrees Fahrenheit.
The API for the required utility class is shown here.
Your class needs to provide and API identical to the one located at the above link; i.e., it must provide:
TemperatureUtilDEG_F_PER_DEG_C and
DEG_C_PER_DEG_F having the correct values fahrenheitToCelcius and
celciusToFahrenheit that perform the computation
described in their contracts
The formula to convert from degrees Fahrenheit to degrees Celcius is:
degc = (degf - 32) * DEG_C_PER_DEG_F
The formula to convert from degrees Celcius to degrees is Fahrenheit:
degf = degc * DEG_F_PER_DEG_C + 32
To get started, you should do the following in eclipse:
lab2)TemperatureUtil
TemperatureUtil.
Recall that the API for a class typically shows all of the public
features of a class. You should see that TemperatureUtil has
two public attributes and two public methods.
Make sure that you can find the values for the fields and that
you understand the API for each method.
TemperatureUtilTemperatureUtil, add the two
public attributes.
public static double celciusToFahrenheit(double degc) {
return 0;
}
public static double fahrenheitToCelcius(double degf) {
return 0;
}
Notice that method implementations are clearly incorrect (because
they both return zero for any temperature); however, they do
compile. Such temporary method implementations are called
method stubs. Stubs are useful in test-driven software
development because you can write unit tests for the methods that
will compile and run; obviously, the unit tests will fail, but you
can now use the unit tests to help guide the development of the
methods.
Unit testing is the act of testing the smallest functional units of a program. For most Java programs, the smallest functional units are the methods of the classes. JUnit is a unit testing framework that helps programmers unit test their programs. In this lab, you are provided with a simple unit tester:
TemperatureUtilTest
in the default package.
TemperatureUtilTest
with the contents of this file
TemperatureUtilTemperatureUtil by implementing each
method so that it meets the requirements of its API. Every time you
complete a method, save your work and re-run the tester to check if your method passes
its unit tests. If your implementation fails a unit test, carefully
examine the results of the test case to help you debug your implementation.
A possible use for a utility class is to group together a dictionary and methods that operate on strings for the purpose of creating or solving simple word games.
The Hamming distance is defined for two strings of equal length as being the number of indices where the two strings have different characters. The Hamming distances for several pairs of strings are shown in the table below.
| String 1 | String 2 | Hamming distance |
|---|---|---|
| frog | frog | 0 |
| matter | master | 1 |
| barter | career | 2 |
| cramping | tripping | 3 |
| impostor | imparted | 4 |
| match | alarm | 5 |
| if | soup | undefined, strings have different lengths |
The Hamming distance is useful for creating many types of word games (such
as word ladders described in the next section) but was actually invented
for detecting and correcting errors
in digital communications. It appears in many fields of study that make
much of today's digital communication technologies possible.
A word ladder is a sequence of words beginning from a start word
and finishing at an end word. The player transforms the start word
into the end word by modifying a single letter of the start word so that
the result is also a word. The process repeats with the new word until the
end word is reached. For example, the following is a word ladder
from cat to dog:
cat → cot → cog → dog
We will say that a word is adjacent to another word if the two words
differ by exactly a single letter; that is, two words are
adjacent if their Hamming distance is exactly one.
For example, in the ladder above,
cot is adjacent to both cat and cog.
cot is also adjacent to dot, cut,
and coy (as well as many others).
Word ladders were described by Lewis Carroll (author of Alice's Adventures in Wonderland) in the 19th century. The renown computer scientist and mathematician Donald Knuth has studied word ladders of five-letter words, and found that most five letter words could be joined by a word ladder. He also found that there were some words that were adjacent to no other words. Visualizations of word adjacency can be found at Jon McLoone's blog. Computational techniques used to study word ladders rely heavily on a branch of mathematics and computer science called graph theory.
To get started, you should do the following in eclipse:
WordGamesUtilWordGamesUtil uses the Dictionary class
which is provided to you in this JAR file.
Download the JAR file and add it to your project:
Project menuPropertiesJava Build PathLibraries tabAdd External JARs... button
WordGamesUtil.
Recall that the API for a class typically shows all of the public
features of a class. You should see that WordGamesUtil has
one public attribute and four public methods.
WordGamesUtilWordGamesUtil, add the field named
DICTIONARY to the body of the class:
/** * A dictionary of lower case English words. The dictionary cannot be * modified. */ public static final Dictionary DICTIONARY = Dictionary.INSTANCE;
public methods. Make
sure that your methods have the correct modifiers,
return the correct types, have names that
are spelled exactly the same as in the API, and
have the correct parameter lists. Also, make sure that your
temporary implementations return appropriate values.
WordGamesUtilTest
in the default package.
WordGamesUtilTest
with the contents of this file
WordGamesUtildistance(String, String)areAdjacent(String, String)allAdjacent(String)allAdjacent(String, int)areAdjacent
can make use of distance, and both allAdjacent
methods can make use of areAdjacent
WordGamesUtil by implementing each
method so that it meets the requirements of its API. Every time you
complete a method, save your work and re-run the tester to check if your method passes
its unit tests. If your implementation fails a unit test, carefully
examine the results of the test case to help you debug your implementation.
Log into a computer of the Prism lab (you can do this remotely).
Transfer your files to your EECS account (if they are not
already there). Create a file named group.txt in
the directory that contains your files. Each line of
group.txt contains the EECS login name of a
member of the group. Open a terminal and go to the directory
that contains your files. In that directory, run the following
command:
submit 1030 lab2 group.txt TemperatureUtil.java WordGamesUtil.java