Palindrome checker is a better basic example of test-driven development

Image for post
Image for post
Photo by Filip Mroz on Unsplash
package basicexercises;public class PalindromeChecker {

public static boolean isPalindromic(String s) {
return false;
}


public static void main(String[] args) {
// TODO: Write arg : args loop
}

}
    @Test
public void testIsPalindromic() {
System.out.println("isPalindromic");
String word = "kayak";
String msg = "\"" + word +
"\" should be considered palindromic";
assertTrue(msg, PalindromeChecker.isPalindromic(word));
}
Testcase: testIsPalindromic(basicexercises.PalindromeCheckerTest): FAILED
"kayak" should be considered palindromic
junit.framework.AssertionFailedError: "kayak" should be considered palindromic
at basicexercises.PalindromeCheckerTest.testIsPalindromic(PalindromeCheckerTest.java:48)
    @Test
public void testIsNotPalindromic() {
String word = "canoe";
String msg = "\"" + word +
"\" should NOT be considered palindromic";
assertFalse(msg, PalindromeChecker.isPalindromic(word));
}
    public static boolean isPalindromic(String s) {
StringBuilder intermediate = new StringBuilder(s);
return s.equals(intermediate.reverse().toString());
}
    @Test
public void testIsPalindromicNotCaseSensitive() {
String word = "Kayak";
String msg = "\"" + word +
"\" should be considered palindromic";
assertTrue(msg, PalindromeChecker.isPalindromic(word));
}
    public static boolean isPalindromic(String s) {
String lowerCased = s.toLowerCase();
StringBuilder intermediate = new StringBuilder(lowerCased);
return lowerCased.equals(intermediate.reverse().toString());
}
    @Test
public void testIsPalindromicEmoji() {
String emojiSeq = "😁😁😁";
String msg = "\"" + emojiSeq +
"\" should be considered palindromic";
assertTrue(msg, PalindromeChecker.isPalindromic(emojiSeq));
}
    public static void main(String[] args) {
for (String arg : args) {
System.out.print("\"" + arg + "\" is");
if (!isPalindromic(arg)) {
System.out.print(" NOT");
}
System.out.println(" palindromic");
}
}
Image for post
Image for post
Photo by Marat Gilyadzinov on Unsplash

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store