Question
Hi. I need some help here I created a LinkedStack class and a junit test case to test certain scenarios to determine if a word
Hi. I need some help here
I created a LinkedStack class and a junit test case to test certain scenarios to determine if a word or phrase was a palindrome.
My problem is all my test cases work with the exception of one: the multipleWords test case.
I believe I've narrowed my problem down to the fact that the the white space between the words are the cause of the error..
How do I get my isPalindrome method to ignore the white space or remove it?
My LinkedStack class:
import java.util.NoSuchElementException;
// Class to implement LinkedStack public class LinkedStack { private static class Node { //Variables /** Data reference. */ private char data; /** Node reference */ private Node next; // Constructors /** Create a new node with a null next field.*/ private Node(char dataItem) { data = dataItem; next = null; } // Create a new node that references another node. private Node(char dataItem, Node nodeRef) { data = dataItem; next = nodeRef; } } // Data Fields /** First stack node reference. */ private static Node topOfStackRef = null; /** Insert a new item on top of the stack. */ public static void push(char c) { topOfStackRef = new Node(c, topOfStackRef); } /** Remove item and return the new top item on the stack. */ public static char pop() { if (isEmpty()) { throw new NoSuchElementException(); } else { char result = topOfStackRef.data; topOfStackRef = topOfStackRef.next; return result; } } /** Return the top item on the stack.*/ public static char peek() { if (isEmpty()) { throw new NoSuchElementException (); } else { return topOfStackRef.data; } } /** See whether the stack is empty. */ public static boolean isEmpty() { return topOfStackRef == null; } /** Check if input is a Palindrome. */ public static boolean isPalindrome (String input) { String reverseInput = ""; for (int i = 0; i
JUnit test cases:
import static org.junit.Assert.assertTrue; import org.junit.jupiter.api.Test;
class LinkedStackTest {
public LinkedStackTest() { }
@Test public void singleCharacter() { assertTrue(LinkedStack.isPalindrome("c")); } @Test public void testmultipleCharacterWord() { assertTrue(LinkedStack.isPalindrome("radar")); } @Test public void multipleWords() { assertTrue(LinkedStack.isPalindrome("Was it a car or a cat I saw")); } @Test public void evenLengthString() { assertTrue(LinkedStack.isPalindrome("Racecar")); } @Test public void oddLengthString() { assertTrue(LinkedStack.isPalindrome("Madam")); } @Test public void emptyString() { assertTrue(LinkedStack.isPalindrome("")); } }
File Edit Source Refactor Navigate Search Project Run Window Help - H O IDN 3. DER EISHO 1 Debug Project Explorer # JUnit x = 0 D Stackint.java - * - - -2- D'LinkedStack.java -! D'LinkedStackTest.java X * = - 0 Q (x)= Variables Breakpoi... Go Expressio... x et 6+ * Name Value + Add new expression Finished after 0.434 seconds Runs: 6/6 Errors:0 20 import static org.junit. Assert.assertTrue; 3 import org. junit.jupiter.api. Test; Failures: 1 class LinkedStackTest { O OVOU public LinkedStackTest() { t: LinkedStackTest (Runner: JUnit 5] (0.144 s) singleCharacter (0.057 s) evenLengthString0 (0.004 s) multipleWords0 (0.032 s) oddLengthString0 (0.006 s) testmultipleCharacterWord0 (0.004 s) emptyString0 (0.038 s) @Test public void singleCharacter() { assert True (LinkedStack.isPalindrome ("c")); 16 170 @Test public void testmultipleCharacterWord() { assertTrue (LinkedStack.isPalindrome ("radar")); @Test public void multipleWords() { assert True (LinkedStack.isPalindrome ("Was it a car or a cat I saw")); @Test public void evenLengthString() { assert True (LinkedStack.isPalindrome ("Racecar")); @Test public void oddLengthString() { assert True (LinkedStack. ispalindrome ("Madam")); = Failure Trace java.lang.AssertionError Eat LinkedStackTest.multipleWords(LinkedStack Test.java:24) at java.util.ArrayList.forEach(ArrayList.java:1249) at java.util.ArrayList.forEach(ArrayList.java:1249) @Test public void emptyString() { assertTrue (LinkedStack.isPalindrome ("")); X * al 3 * - . - Console X A Problems Debug Shell CoverageStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started