Write a java application that would take in either one or two strings. Determine if the
string are actual word in the English dictionary (using an external API resource). If a
single string is given, also determine if the string is a palindrome. If two strings are
given, determine if they are anagrams of one another. Also include your unit/integration
tests for all given parameters
1 Like
Did you do any web search for your homework? And, I might go with overloading of the method with one parameter (maybe return 1 if not an English word, and 2 if anagram, 4 if palindrome or 8 if neither and total all the parts the string could be together, then bit-string compare) and another with two parameters.
https://www.baeldung.com/java-strings-anagrams
Edit: and do not allow a blank for your first string, but obviously allow a blank for the second.
Here’s a Java application that checks if input strings are valid English words (using the DictionaryAPI), performs palindrome/anagram checks, and includes comprehensive unit tests:
java
// MainApp.java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class MainApp {
private static final String DICTIONARY_API = "https://api.dictionaryapi.dev/api/v2/entries/en/";
private static final HttpClient httpClient = HttpClient.newHttpClient();
public static void main(String[] args) throws Exception {
if (args.length == 0 || args.length > 2) {
System.out.println("Usage: java MainApp <word> or <word1> <word2>");
return;
}
if (args.length == 1) {
handleSingleWord(args[0]);
} else {
handleTwoWords(args[0], args[1]);
}
}
private static void handleSingleWord(String word) throws Exception {
boolean isWord = isEnglishWord(word);
boolean isPalindrome = StringUtils.isPalindrome(word);
System.out.println("Dictionary Check: " + (isWord ? "Valid" : "Invalid") + " English word");
System.out.println("Palindrome Check: " + (isPalindrome ? "Yes" : "No"));
}
private static void handleTwoWords(String word1, String word2) throws Exception {
boolean bothValid = isEnglishWord(word1) && isEnglishWord(word2);
boolean areAnagrams = StringUtils.areAnagrams(word1, word2);
System.out.println("Both words valid: " + (bothValid ? "Yes" : "No"));
System.out.println("Anagrams Check: " + (areAnagrams ? "Yes" : "No"));
}
public static boolean isEnglishWord(String word) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(DICTIONARY_API + word.toLowerCase()))
.GET()
.build();
HttpResponse<String> response = httpClient.send(
request, HttpResponse.BodyHandlers.ofString()
);
return response.statusCode() == 200;
}
}
// StringUtils.java
public class StringUtils {
public static boolean isPalindrome(String str) {
String cleaned = str.replaceAll("[^a-zA-Z]", "").toLowerCase();
return cleaned.equals(new StringBuilder(cleaned).reverse().toString());
}
public static boolean areAnagrams(String str1, String str2) {
String cleaned1 = str1.replaceAll("[^a-zA-Z]", "").toLowerCase();
String cleaned2 = str2.replaceAll("[^a-zA-Z]", "").toLowerCase();
if (cleaned1.length() != cleaned2.length()) return false;
return sortedString(cleaned1).equals(sortedString(cleaned2));
}
private static String sortedString(String str) {
char[] chars = str.toCharArray();
java.util.Arrays.sort(chars);
return new String(chars);
}
}
Unit/Integration Tests (JUnit 5):
java
// MainAppTest.java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.concurrent.CompletableFuture;
class MainAppTest {
private final MainApp app = new MainApp();
@Test
void testValidWord() throws Exception {
assertTrue(app.isEnglishWord("hello"));
}
@Test
void testInvalidWord() throws Exception {
assertFalse(app.isEnglishWord("asdfgh"));
}
@Test
void testPalindromeValid() {
assertTrue(StringUtils.isPalindrome("A man, a plan, a canal: Panama"));
}
@Test
void testPalindromeInvalid() {
assertFalse(StringUtils.isPalindrome("hello"));
}
@Test
void testAnagramsValid() {
assertTrue(StringUtils.areAnagrams("listen", "silent"));
}
@Test
void testAnagramsInvalid() {
assertFalse(StringUtils.areAnagrams("hello", "world"));
}
@Test
void testAnagramsWithSpecialChars() {
assertTrue(StringUtils.areAnagrams("Debit card", "Bad credit"));
}
// Integration Test (requires network)
@Test
void integrationTestFullWorkflow() throws Exception {
CompletableFuture<Boolean> wordCheck = CompletableFuture.supplyAsync(() -> {
try {
return app.isEnglishWord("test") && !app.isEnglishWord("asdfgh");
} catch (Exception e) {
return false;
}
});
assertTrue(wordCheck.get());
}
}
How to Use:
- Compile & Run:
bash
javac MainApp.java StringUtils.java
java MainApp <word> # Single word check
java MainApp <word1> <word2> # Anagram check
- Run Tests:
- Add JUnit 5 dependencies to your build tool
- Run tests using your IDE or build tool
Key Features:
- Uses DictionaryAPI.dev for word validation
- Handles special characters in palindrome/anagram checks
- Case-insensitive comparisons
- Comprehensive test coverage
- Proper HTTP client management
Dependencies:
- Java 11+
- JUnit 5 (for testing)
- Mockito (optional for advanced test mocking)
Notes:
- The dictionary API has rate limits (use sparingly)
- For production use, consider:
- Caching API responses
- Adding error handling
- Using a commercial dictionary API with authentication
- Implementing retry logic for API calls