CSE1030 Lab 01
Utility classes

Week of Jan 13, 2014
Due: Mon Jan 20 before 11:59PM


Recall that a utility class is a class containing only public static constant attributes (fields) and methods. The purpose of a utility class is to group together related constants and methods so that a client can access these features from a single class.

In this lab, you will create a utility class for simple word games. The class has a public final static reference to an English language dictionary of words, and several public static methods that operate on strings.

The goals of this lab include:

Style Rules

The style rules are not overly restrictive in CSE1030.

1. Your programs should use the normal Java conventions (class names begin with an uppercase letter, variable names begin with a lowercase letter, etc.).

2. In general, use descriptive variable names. There are exceptions to this rule; for example, traditional loop variables are often called i, j, k, etc.

3. Use a consistent indentation size. Beware of the TAB vs SPACE problem: Tabs have no fixed size; one editor might interpret a tab to be 4 spaces and another might use 8 spaces. If you mix tabs and spaces, you will have indenting errors when your code is viewed in different editors.

4. Use a consistent brace style:

// left aligned braces

class X
  public void someMethod()
    // ...
  public void anotherMethod()
    for (int i = 0; i < 1; i++)
      // ...


// ragged braces

class X {
  public void someMethod() {
    // ...
  public void anotherMethod() {
    for (int i = 0; i < 1; i++) {
      // ...

5. This one always causes problems for students: Insert a space around operators (except the period ".").

The following is

    // some code somewhere
    boolean isBetween = (x > MIN_VALUE) && (x > MAX_VALUE);
    int someValue = x + y * z;

much easier to read than this

    // some code somewhere
    boolean isBetween=(x>MIN_VALUE)&&(x>MAX_VALUE);
    int someValue=x+y*z;

A utility class for word games

Utility classes are used to group a set of constants and methods that are used in related tasks. For example, java.lang.Math groups together mathematical constants and methods, java.util.Arrays groups together methods that operate on arrays, and java.util.Collections groups together methods that operate on collections.

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.


An isogram is a word where each letter in the word occurs only once. The following words are all example of isograms:


The word java is not an isogram because it contains two 'a's.

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


An anagram is a word whose letters can be rearranged to form another word. The following words are all examples of anagrams:

  elvis ↔ lives
  listen ↔ silent
  married ↔ admired
  dictionary ↔ indicatory

More elaborate anagrams can be formed if we ignore capitalization, spaces, and punctuation, and allow proper names, but we will not consider such problems in this lab. Some fun ones include:

  debit card ↔ bad credit
  New York Times ↔ monkeys write
  eleven plus two ↔ twelve plus one
  Tom Marvolo Riddle ↔ I am Lord Voldemort

Many more examples can be found at the Internet Anagram Server.

Anagrams that do not form words are related to simple forms of cryptography based on shifting positions of letters in a regular fashion.

Question: Implement the utility class WordGamesUtil

In CSE1030, you are typically required to implement an API that has been designed by someone else. Typically, to implement an API you must complete the following steps:


Getting started

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

  1. Create a new Java Project (perhaps called lab1)
  2. In your project, create a new package named cse1030.games.wordgames
  3. In the package cse1030.games.wordgames create a new Java class named WordGamesUtil
  4. 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 field and eight 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:
      public static final Dictionary DICTIONARY = Dictionary.INSTANCE;
  3. There are eight methods in WordGamesUtil. When you add a public method, it is important that the method you add is identical to that found in the API. For example, the method named isIsogram appears in the API like so:
      public static boolean isIsogram(String s)
    Add the method to the body of the class beneath the constant DICTIONARY like so:
      public static boolean isIsogram(String s) {
        return true;
    You might wonder why the method always returns true when most words are not isograms. The reason why we do this now is so that the implementation will compile (even though it is logically incorrect). If you omit the return statement, then the compiler will generate a compilation error (stating that the method must return a boolean value). Later, when you complete the implementation of the method, you will change the return value so that the correct value is returned.
  5. Add the method named allIsograms to the body of the class beneath the previous method like so:
      public static SortedSet<String> allIsograms() {
        return null;
    The method allIsograms is supposed to return a SortedSet of all isograms found in DICTIONARY. A SortedSet is a reference type so a legal return value is null. Again, we always return some legal value so that the source code file will compile.
  6. Add the remaining six methods to your class, always making sure that you return an appropriate legal value.
  7. Save your file.


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 WordGamesUtilTest to the package cse1030.games.wordgames
  2. Replace the automatically created contents of WordGamesUtilTest with the contents of this file
  3. Follow the instructions here to run the unit tester


Complete the implementation of WordGamesUtil

  1. Complete the class WordGamesUtil by implementing each method so that it meets the requirements of its API. Every time you complete a method, re-run the tester to check if your method passes its unit tests. You will need the API for the Dictionary class which can be found here. Ignore the methods named valueOf and values; these methods are present because Dictionary is something called an enumeration (which we do not normally discuss in CSE1030).


Here are some hints:


Warning: The unit tests provided to you are very simplistic in that they only use a single test case for each test. It is possible that you produce an implmentation that passes all of the tests but is totally incorrect. A better tester would include more test cases chosen to provide coverage of cases that are likely to cause problems; however, creating such a tester is beyond the scope of this first lab.


Submit your solution using the submit command. Remember that you first need to find your workspace directory, then you need to find your project directory. In your project directory, your files will be located in the directory src/cse1030/games/wordgames

submit 1030 L1 WordGamesUtil.java

It is possible to submit online using web-based submit https://webapp.eecs.yorku.ca/submit/

Some things to think about