Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Word Game Java Word Ladder Game Java Word-Ladder Game Doublets Can anyone help me write the methods for this assignment per instructions given? import

Java Word Game Java Word Ladder Game Java Word-Ladder Game Doublets

Can anyone help me write the methods for this assignment per instructions given?

image text in transcribedimage text in transcribed

import java.util.List; /** * WordLadderGame.java Defines an interface for games that construct word * ladders. See https://en.wikipedia.org/wiki/Word_ladder for a definition and * history. * * Word ladders are constructed in the context of some predefined list of valid * words. We will refer to this word list as the lexicon. An implementing class * of this interface must provide a way to explicitly set the lexicon. This will * typically be done in the constructor. * * For the purposes of this interface and all implementing classes, a string is * a word if and only if it appears in the current lexicon. In the documentation * of each interface method, the use of 'string' means that the referenced * string does not have to be a word, while the use of 'word' implies that the * referenced string must be a word. * */ public interface WordLadderGame { /** * Returns the Hamming distance between two strings, str1 and str2. The * Hamming distance between two strings of equal length is defined as the * number of positions at which the corresponding symbols are different. The * Hamming distance is undefined if the strings have different length, and * this method returns -1 in that case. See the following link for * reference: https://en.wikipedia.org/wiki/Hamming_distance * * @param str1 the first string * @param str2 the second string * @return the Hamming distance between str1 and str2 if they are the * same length, -1 otherwise */ int getHammingDistance(String str1, String str2); /** * Returns a word ladder from start to end. If multiple word ladders exist, * no guarantee is made regarding which one is returned. If no word ladder exists, * this method returns an empty list. * * Depth-first search with backtracking must be used in all implementing classes. * * @param start the starting word * @param end the ending word * @return a word ladder from start to end */ List getLadder(String start, String end); /** * Returns a minimum-length word ladder from start to end. If multiple * minimum-length word ladders exist, no guarantee is made regarding which * one is returned. If no word ladder exists, this method returns an empty * list. * * Breadth-first search must be used in all implementing classes. * * @param start the starting word * @param end the ending word * @return a minimum length word ladder from start to end */ List getMinLadder(String start, String end); /** * Returns all the words that have a Hamming distance of one relative to the * given word. * * @param word the given word * @return the neighbors of the given word */ List getNeighbors(String word); /** * Returns the total number of words in the current lexicon. * * @return number of words in the lexicon */ int getWordCount(); /** * Checks to see if the given string is a word. * * @param str the string to check * @return true if str is a word, false otherwise */ boolean isWord(String str); /** * Checks to see if the given sequence of strings is a valid word ladder. * * @param sequence the given sequence of strings * @return true if the given sequence is a valid word ladder, * false otherwise */ boolean isWordLadder(List sequence); }

---------------------------------------------------------------------------------------------

import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; import java.util.LinkedList; import java.util.List; import java.util.Scanner; import java.util.TreeSet; /** * Doublets.java * Provides an implementation of the WordLadderGame interface. The lexicon * is stored as a TreeSet of Strings. * */ public class Doublets implements WordLadderGame { //////////////////////////////////////////// // DON'T CHANGE THE FOLLOWING TWO FIELDS. // //////////////////////////////////////////// // A word ladder with no words. Used as the return value for the ladder methods // below when no ladder exists. List EMPTY_LADDER = new ArrayList(); // The word list used to validate words. // Must be instantiated and populated in the constructor. TreeSet lexicon; /** * Instantiates a new instance of Doublets with the lexicon populated with * the strings in the provided InputStream. The InputStream can be formatted * in different ways as long as the first string on each line is a word to be * stored in the lexicon. */ public Doublets(InputStream in) { try { lexicon = new TreeSet(); Scanner s = new Scanner(new BufferedReader(new InputStreamReader(in))); while (s.hasNext()) { String str = s.next(); //////////////////////////////////////////////// // Add code here to store str in the lexicon. // //////////////////////////////////////////////// s.nextLine(); } in.close(); } catch (java.io.IOException e) { System.err.println("Error reading from InputStream."); System.exit(1); } } /////////////////////////////////////////////////////////////////////////////// // Fill in implementations of all the WordLadderGame interface methods here. // /////////////////////////////////////////////////////////////////////////////// /** * Returns the Hamming distance between two strings, str1 and str2. The * Hamming distance between two strings of equal length is defined as the * number of positions at which the corresponding symbols are different. The * Hamming distance is undefined if the strings have different length, and * this method returns -1 in that case. See the following link for * reference: https://en.wikipedia.org/wiki/Hamming_distance * * @param str1 the first string * @param str2 the second string * @return the Hamming distance between str1 and str2 if they are the * same length, -1 otherwise */ public int getHammingDistance(String str1, String str2) { return 0; } /** * Returns a word ladder from start to end. If multiple word ladders exist, * no guarantee is made regarding which one is returned. If no word ladder exists, * this method returns an empty list. * * Depth-first search with backtracking must be used in all implementing classes. * * @param start the starting word * @param end the ending word * @return a word ladder from start to end */ public List getLadder(String start, String end) { return null; } /** * Returns a minimum-length word ladder from start to end. If multiple * minimum-length word ladders exist, no guarantee is made regarding which * one is returned. If no word ladder exists, this method returns an empty * list. * * Breadth-first search must be used in all implementing classes. * * @param start the starting word * @param end the ending word * @return a minimum length word ladder from start to end */ public List getMinLadder(String start, String end) { return null; } /** * Returns all the words that have a Hamming distance of one relative to the * given word. * * @param word the given word * @return the neighbors of the given word */ public List getNeighbors(String word) { return null; } /** * Returns the total number of words in the current lexicon. * * @return number of words in the lexicon */ public int getWordCount() { return 0; } /** * Checks to see if the given string is a word. * * @param str the string to check * @return true if str is a word, false otherwise */ public boolean isWord(String str) { return false; } /** * Checks to see if the given sequence of strings is a valid word ladder. * * @param sequence the given sequence of strings * @return true if the given sequence is a valid word ladder, * false otherwise */ public boolean isWordLadder(List sequence) { return false; } }

----------------------------------------------------------------------------------------

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.List; /** * ExampleClient.java * Provides example calls to WordLadderGame methods in an instance of * the Doublets class. * * The word list files must be extracted into the current directory * before running this class. * * jar xf WordLists.jar * */ public class ExampleClient { /** Drives execution. */ public static void main(String[] args) throws FileNotFoundException { WordLadderGame doublets = new Doublets(new FileInputStream(new File("sowpods.txt"))); System.out.println(doublets.getHammingDistance("tiger", "tiger")); System.out.println(doublets.getHammingDistance("tiger", "eagle")); System.out.println(doublets.getHammingDistance("war", "eagle")); System.out.println(doublets.getHammingDistance("barner", "bammer")); System.out.println(doublets.isWord("tiger")); System.out.println(doublets.isWord("eagle")); System.out.println(doublets.isWord("aubie")); System.out.println(doublets.getWordCount()); System.out.println(doublets.isWordLadder(Arrays.asList("cat", "cot", "zot", "dot"))); System.out.println(doublets.isWordLadder(Arrays.asList("cat", "cot", "pot", "dot"))); System.out.println(doublets.getNeighbors("tiger")); System.out.println(doublets.getLadder("cat", "hat")); System.out.println(doublets.getMinLadder("cat", "hat")); } } /* RUNTIME OUTPUT 0 4 -1 2 true true false 267751 true [liger, niger, tiler, timer, titer, tiges] [cat, bat, eat, fat, gat, hat] [cat, hat] */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago