Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The word ladder game was invented by Lewis Carroll in 1877. The idea is to begin

ID: 3694921 • Letter: T

Question

The word ladder game was invented by Lewis Carroll in 1877. The idea is
to begin with a start word and change one letter at a time until arriving at
an end word. Each word along the way must be an English word.
For example, starting from FISH you can make a word ladder to MAST
through the following ladder:
FISH, WISH, WASH, MASH, MAST
Write a program that uses recursion to find the word ladder given a start
word and an end word, or determines if no word ladder exists. Use the
file words.txt that is available online with the source code for the book
as your dictionary of valid words. This file contains 87314 words. Your
program does not need to find the shortest word ladder between words,
any word ladder will do if one exists.

This is from Walter Savitch, Java: An introduction to problem solving and programming. It's from Chapter 11, number 8. Please solve this recursively using relatively simple methods. This is a very simple program for an intro to Java course. It does not need any fancy techniques like nodes and regex and all that, it just needs to be done recursively. Chegg does not let me upload the words.txt file, but it can be found here: http://s000.tinyupload.com/index.php?file_id=16996529956442064272. Thank you.

Explanation / Answer

WordLadder.java

import java.io.*;
import java.util.*;

public class MainDriver
{
   public static void main(String[] args)
   {
       Scanner in = null;
       WordLadder wordLadderCreator = new WordLadder();

       // Read the input file and build ladders between given words
       try
       {
           in = new Scanner(new File("input.txt"));

           while (in.hasNext())
           {
           // Build word ladders for the given input and print them
               String start = in.next();
               String end = in.next();
               Stack<String> ladder = wordLadderCreator.buildLadder(start, end);

               if (ladder == null)
                   System.out.println("There is no word ladder between " + start + " and " + end + "!");
               else
                   System.out.println(ladder);
           }
       }
       catch (FileNotFoundException e)
       {
           System.out.println("Wrong file name!");
           System.exit(0);
       }
       finally
       {
           if(in != null) in.close();
       }
   }
}


WordLadder.java
import java.io.*;
import java.util.*;
import java.net.*;

@SuppressWarnings("unchecked")
public class WordLadder
{
    private HashSet<String>       dictionary;    // dictionary
    private HashSet<String>       used;           // table of used words
    private Queue<Stack<String>> wordLadders;

    /**
     * Constructor.
     * Initializes private variables
     */
    public WordLadder()
    {
        readDictionary("dictionary.txt");
    }

    /**
     * Reads the dictionary and store it in the hash table
     */
   public void readDictionary(String urlString)
   {
       throw new UnsupportedOperationException("You need to implement this method");
    }

/**
   *   Finds the shortest word-ladder that connects start and end words
   *
   *   Returns null if such a ladder does not exist.
   */
   public Stack<String> buildLadder(String start, String end)
   {
       throw new UnsupportedOperationException("You need to implement this method");
   }


    /**
     * Extra Credit Method.
     * You are not required to implement it.
     */
   public ArrayList<Stack<String>> buildLadder(String start, String end, int ladderLength)
   {
       return null;
   }

}


QueueInterface.java

import java.util.*;

public interface QueueInterface<AnyType>
{
   /**
   * Tests if the queue is logically empty
   */
   public boolean isEmpty();

   /**
   * Puts a value into the back of the queue. It works with wraparound.
   * If the queue is full, tt doubles its size.
   *
   * @param value the item to insert.
   */
   public void enqueue (AnyType value);

   /**
   * Returns the first element in the queue.
   *
   * @return element at front of the queue
   * @throws NoSuchElementException if the queue is empty.
   */
   public AnyType getFront() throws java.util.NoSuchElementException;

   /**
   * Returns and removes the front element of the queuee. It works with wraparound.
   *
   * @return element at front of the queue
   * @throws NoSuchElementException if the queue is empty.
   */
   public AnyType dequeue() throws java.util.NoSuchElementException;

   /**
   * Makes the queue physically empty.
   */
   public void clear();
}
Queue.java

import java.util.*;

public class Queue<AnyType> implements QueueInterface<AnyType>
{
   private Stack<AnyType> A;
   private Stack<AnyType> B;
}

input.txt
dears fears
heart heart
monk perl
slow fast
blue pink
bluw pink
stone money
money smart
devil angel
atlas zebra
babes child
mumbo ghost
train bikes
babies sleepy
brewing whiskey


output.txt
[dears, fears]
[heart]
There is no word ladder between mikhail and jeff!
[monk, conk, cork, pork, perk, perl]
[slow, slot, slit, flit, fait, fast]
[blue, blae, blad, bead, beak, peak, penk, pink]
There is no word ladder between bluw and pink!
[stone, atone, alone, clone, clons, coons, conns, cones, coney, money]
[money, boney, bones, bonks, boaks, boars, soars, scars, scart, smart]
[devil, devel, level, lever, leger, luger, auger, anger, angel]
[atlas, aulas, auras, aures, lures, lares, laris, labis, labia, labra, zabra, zebra]
[babes, bales, balls, calls, cells, ceils, ceili, chili, child]
[mumbo, bumbo, bombo, bombs, boobs, blobs, globs, gloss, glost, ghost]
[train, brain, brawn, brown, brows, brogs, biogs, bings, bines, bikes]
[babies, gabies, gables, gabled, sabled, sailed, stiled, stiles, steles, stells, steels, steeps, sleeps, sleepy]
[brewing, crewing, chewing, chowing, choking, cooking, booking, boobing, bobbing, bobbins, bobbies, bobbles, babbles, babbled, wabbled, warbled, warsled, warsted, waisted, whisted, whisked, whiskey]