Question: Word Ladder Java Programming Assignment... Hi, I have a Java project that I am having trouble with. I provided all the neccesary files below. Most
Word Ladder Java Programming Assignment...
Hi, I have a Java project that I am having trouble with. I provided all the neccesary files below. Most of the project is already completed, I just need help implementing a few methods. Any help on this would be greatly appreciated!
The Project:





![{ public static void main(String[] args) throws IOException { // TODO code](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f4f2e663d70_99766f4f2e5efe6d.jpg)

The Starter Code:
MainDriver.java
import java.io.*;
import java.util.*;
public class MainDriver {
public static void main(String[] args) throws IOException {
// TODO code application logic here
Ladder wordLadder = null; new Ladder();
Scanner in=null;
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();
wordLadder = new Ladder();
Stack ladder = wordLadder.buildLadder(start, end);
if (ladder.isEmpty())
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);
}
in.close();
}
}
Ladder.java
import java.util.*; import java.io.*;
public class Ladder { private Set dictionary; private Set used; private Queue> wordLadder; /** * Constructor. * Initialize private data fields */ public Ladder()throws IOException { dictionary = new HashSet(); wordLadder = new Queue(); used = new HashSet(); readDictionary("dictionary.txt"); }
/** * Reads the dictionary and store it in the hash table */ public void readDictionary(String urlString) throws IOException { // "You need to implement this method" } public Stack buildLadder(String start, String end){ Stack ladder = new Stack(); for(String s : dictionary){ Stack stack = new Stack(); if (isAnEdge(start,s)) { stack.push(start); stack.push(s); wordLadder.enqueue(stack); } } used.add(start); // You need to implement rest of the method return ladder; } private boolean isAnEdge(String w1, String w2) { // Implementation left as exercise }
}
Queue.java
import java.util.*; public class Queue implements QueueInterface{ private Stack inbox; private Stack outbox; public Queue(){ inbox = new Stack(); outbox = new Stack(); }
public boolean enqueue(E e){ try{ inbox.push(e); } catch (ClassCastException ex) { return false; } return true; } public E dequeue(){ try{ if(outbox.isEmpty()){ while(!inbox.isEmpty()){ outbox.push(in.pop()); } } }catch(NoSuchElementException ex){} return outbox.pop(); } public E peek(){ // Implementation left as exercise } public boolean isEmpty(){ // Implementation left as exercise } public void clear(){ // Implementation left as exercise } }
QueueInterface.java
public interface QueueInterface { /** * Tests if the queue is logically empty */ public boolean isEmpty();
/** * Puts a value into the back of the queue. * * * @param value the item to insert. * @return true if successful * @throws ClassCastException */ public boolean enqueue (E value);
/** * Returns the first element in the queue. * * @return element at front of the queue * @throws java.util.NoSuchElementException if the queue is empty. */ public E peek() throws java.util.NoSuchElementException;
/** * Returns and removes the front element of the queue. It works with wraparound. * * @return element at front of the queue * @throws java.util.NoSuchElementException if the queue is empty. */ public E dequeue() throws java.util.NoSuchElementException;
/** * Makes the queue physically empty. */ public void clear(); }
Dictionary.txt
lice deck cord bent band cast bike cash card boat cold coat dear core dash cost dame fish dorm dine deer dime deme dive dish dinn door dome fake face find fast fire fine finn help held hash fore folk fold hard hear here host hold hire lase land knot lake kunn kuns last mind main line lime like lost live linn love lunn mike maze mash make mice meta mien milk vice silk neck mink mine must most more nash sick nice rain pour pine nick pain nine nuns pond pony poor sake rich rick rash rime rust sane sand sine sure sony tiny warm vide ward worm
Input.txt
card fine
help rich
dime rich
loss help
java fish
Sample Run
[card, cord, core, fore, fire, fine]
[help, held, hold, cold, cord, core, fore, fire, fine, nine, nice, nick, rick, rich]
[dime, dine, nine, nice, nick, rick, rich]
[loss, lost, last, lase, lake, like, line, fine, fire, fore, core, cord, cold, hold, held, help]
There is no word ladder between java and fish!
Overview: stone Atone aLone Clone clonS cOons CONns conEs coneY Money Word ladders were invented by Lewis Carroll in 1878, the author of Alice in Wonderland. A ladder is a sequence of words that starts at the starting word, ends at the ending word, In a word ladder puzzle you have to change one word into another by altering a single letter at each step. Each word in the ladder must be a valid English word, and must have the same length. For example, to turn stone into money, one possible ladder is given on the left. Many ladder puzzles have more than one possible solutions. Another path from stone to money is: stone store shore chore choke choky cooky cooey coney money Objectives Practice implementing and using a Queue data structure. Practice using Stack data structure Practice using Set, HashSet data structure Gain an understanding of algorithms used for efficient implementation
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
