Question: import java.io.*; import java.util.*; public class MainDriver { public static void main(String[] args) throws IOException { // TODO code application logic here Ladder wordLadder =

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(); } } 
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 } }
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 } }

package wordladder;

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();

}

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

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

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. Instructions Your program will accept starting and ending words from the input file called "input.txt". Then, you read the dictionary file (can be downloaded from Google Drive) and store it in a HashSet Finally, you build a word ladder between starting and ending words There are several ways to solve this problem. One simple method involves using stacks and queues. The algorithm (that you must implement) works as it follows 1) Get the starting word and search through the dictionary o Find all words that are one letter different. o Create stacks for each of these words, containing the starting word (pushed first) and the word that is one letter different. 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. Instructions Your program will accept starting and ending words from the input file called "input.txt". Then, you read the dictionary file (can be downloaded from Google Drive) and store it in a HashSet Finally, you build a word ladder between starting and ending words There are several ways to solve this problem. One simple method involves using stacks and queues. The algorithm (that you must implement) works as it follows 1) Get the starting word and search through the dictionary o Find all words that are one letter different. o Create stacks for each of these words, containing the starting word (pushed first) and the word that is one letter different

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!