0% found this document useful (0 votes)
9 views

Java

report on project

Uploaded by

mathewstroke
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Java

report on project

Uploaded by

mathewstroke
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

interface JumbleSolver {
List<String> solveJumble(String jumble);
}

class SimpleJumbleSolver implements JumbleSolver {


private Set<String> dictionary;

public SimpleJumbleSolver(Set<String> dictionary) {


this.dictionary = dictionary;
}

@Override
public List<String> solveJumble(String jumble) {
Set<String> results = new HashSet<>();
String sortedJumble = sortString(jumble);

for (String word : dictionary) {


if (sortString(word).equals(sortedJumble)) {
results.add(word);
}
}

return new ArrayList<>(results);


}

private String sortString(String input) {


char[] charArray = input.toCharArray();
java.util.Arrays.sort(charArray);
return new String(charArray);
}
}

class DictionaryManager {
private Set<String> words;

public DictionaryManager() {
words = new HashSet<>();
}

public void addWord(String word) {


words.add(word.toLowerCase());
}

public Set<String> getDictionary() {


return words;
}
}

public class JumbleWordSolverApp {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
DictionaryManager dictionaryManager = new DictionaryManager();
System.out.println("Enter words for the dictionary (type 'done' to
finish):");
while (true) {
String word = scanner.nextLine();
if (word.equalsIgnoreCase("done")) {
break;
}
dictionaryManager.addWord(word);
}

JumbleSolver solver = new


SimpleJumbleSolver(dictionaryManager.getDictionary());

System.out.print("Enter a jumbled word: ");


String jumble = scanner.nextLine();

List<String> solutions = solver.solveJumble(jumble);

System.out.println("Jumble: " + jumble);


System.out.println("Possible words: " + solutions);

scanner.close();
}
}

You might also like