EECS1030 Lab 02

Wed Jan 14-15
Due: Before Wed Jan 21

Introduction

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.

Problem 1: Create a Simple Utility Class from Scratch

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:

  1. a utility class named TemperatureUtil
  2. fields named DEG_F_PER_DEG_C and DEG_C_PER_DEG_F having the correct values
  3. methods named 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


Getting started

To get started, you should do the following in eclipse:

  1. Create a new Java Project (perhaps called lab2)
  2. In the default package create a new Java class named TemperatureUtil

 

Inspect the API

  1. Study the API for 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.

 

Add the fields and methods to TemperatureUtil

  1. To the class TemperatureUtil, add the two public attributes.
  2. Add the following temporary implementations of the two methods:
    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.

 

Add a unit tester

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:

  1. Create a new class for the unit tester named TemperatureUtilTest in the default package.
  2. Replace the automatically created contents of TemperatureUtilTest with the contents of this file
  3. Follow the instructions here to run the unit tester

 

Complete the implementation of TemperatureUtil

  1. Complete the class TemperatureUtil 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.

 

Problem 2: A utility class for word games

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.

Hamming distance

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.

Word ladders

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.

Getting started

To get started, you should do the following in eclipse:

  1. In your project, create a new Java class named WordGamesUtil
  2. WordGamesUtil uses the Dictionary class which is provided to you in this JAR file. Download the JAR file and add it to your project:
    1. select the Project menu
    2. select Properties
    3. on the left side of the dialog that appears, select Java Build Path
    4. on the right, select the Libraries tab
    5. click the Add External JARs... button
    6. add the jar file you downloaded

 

Inspect the API

  1. Study the API for 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.

 

Add the fields and methods to WordGamesUtil

  1. To the class WordGamesUtil, add the field named DICTIONARY to the body of the class:
  2.   /**
       * A dictionary of lower case English words. The dictionary cannot be
       * modified. 
       */
      public static final Dictionary DICTIONARY = Dictionary.INSTANCE;
      
    
  3. Add method stubs for the four 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.
  4. Save your file.

Add a unit tester

  1. Create a new class for the unit tester named WordGamesUtilTest in the default package.
  2. Replace the automatically created contents of WordGamesUtilTest with the contents of this file
  3. Run the unit tester

 

Complete the implementation of WordGamesUtil

  1. It is recommended that you complete the implementation of the methods in the following order:
    1. distance(String, String)
    2. areAdjacent(String, String)
    3. allAdjacent(String)
    4. allAdjacent(String, int)
    The reason for following this order is that areAdjacent can make use of distance, and both allAdjacent methods can make use of areAdjacent
  2. Complete the class 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.

 

Submit

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