Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Activity: Use the Palindrome.java below to develop your class and method. The cl

ID: 3739158 • Letter: A

Question

Activity:

Use the Palindrome.java below to develop your class and method. The class name must be called Palindrome and the method to check for Palindrome is called checkPalindrome. Please develop the program and test the program running the main method. Run palindromeTest.java to verify.

The output should look something like this.

////////////////////////////////////palindrome.java///////////////////////////////////////////////////////////////////

// This class provides method to test if a string is a Palindrome using Stack/Queue ADTs.
// please provide more inputs here.
import java.util.Stack;
import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
class Palindrome {
// This is a main method to test checkPalindrome method
public static void main(String[] args) {
String inputString = new String("");
Scanner in = new Scanner(System.in);
do {
// please put your code to test checkPalindrome method here
} while ( inputString.equals("y") && inputString.length() == 1 );
System.out.print("Bye!");
}
// This is checkPalindrome method. It checks if an input string is Palindrome or not.
// It returns 0 if a string is a Palindrome. Otherwise, it returns a position of a character where it finds
// a different value.
// Pre-Condition: string must not be null.
// Post-Condition: Return 0 if input string is a Palindrome. Return a positive number indicate the location where
// a difference found.
public static int checkPalindrome(String strValue) {  
Stack stack = new Stack();
Queue queue = new LinkedList();
  
// check if string is null. If it is null, return a -1
  
// normalize the string values to lower case, remove spaces
strValue = strValue.toLowerCase().replaceAll("\W", "");

// store data on stack/queue adts first

// loop: comparing, retrieving text, terminate loop if stack is emptied or found a difference

return indexVal;
}
}

//////////////////////////////////////PalindromeTest.java//////////////////////////////////////////////

/** Unit test cases for checking the Palindrome method **/
/** Doan Nguyen: CSC 20 - Spring 2018 **/
import org.junit.Assert;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

public class PalindromeTest {

/** Fixture initialization (common initialization
* for all tests). **/
String testStr = new String("");
@Before public void setUp() {
testStr = "";

}

/** Step on no pets test. **/
@Test public void defaultTest() {
testStr = "Step on no pets";
int check = Palindrome.checkPalindrome(testStr);
assertEquals(0, check);
}

/** null test. **/
@Test public void nullTest() {
testStr = null;
int check = Palindrome.checkPalindrome(testStr);
assertEquals(-1, check);
}

/** BelphegorPrime number test. **/
@Test public void BelphegorPrimeTest() {
testStr = "1000000000000066600000000000001";
int check = Palindrome.checkPalindrome(testStr);
assertEquals(0, check);
}

/** fail lonely tylenol test. **/
@Test public void failTest() {
testStr = "Lonely Tvlenol";
int check = Palindrome.checkPalindrome(testStr);
assertEquals(6, check);
}
}

Important membermethods are as follows. Stack Queue Member function s is a stack object.) Member function (q is a queue object.) Meaning Meaning s.size(); Returns the number q.size); of elements in this stack Pushes an item onto q.add(item); the top of this stack. Returns the number of elements in this queue Inserts the specified element into this queue Retrieves removes the head of this queue. s.push(item); Object obj s.pop); Removes the object at Object obj and the top of this stackq.remove) and returns that object as the value of this function Object obj- s.peek) Looks at the objectat Object obj-q.peek);Retrieves, but does the top of this stack without removing it from the stack not remove, the head of this queue, o returns null if queue is empty this s.empty(); Tests if this stack is Use q.peek()to empty determine if queue is emptied. Check for null as returned value. Or use q.size()

Explanation / Answer

import java.util.Stack;
import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
public class Palindrome {
   // This is a main method to test checkPalindrome method
   public static void main(String[] args) {
       String inputString = new String("");
       Scanner in = new Scanner(System.in);
       do {
           // please put your code to test checkPalindrome method here
           System.out.print("Please enter a string of characters: ");
           inputString = in.nextLine();
          
           int index = checkPalindrome(inputString);
          
           if(index == -1) {
               System.out.println("Input string is null");
           }else if(index > 0) {
               System.out.println("The given string is not a palindrome, since the symbol at position "+index
                       + "from the left is different from the symbol at position "+index+" from the right.");
           }else{
               System.out.println("The given string is a palindrome.");
           }
          
           System.out.print("Want to examine another string? (y/n): ");
           inputString = in.nextLine();
       } while ( inputString.equals("y") && inputString.length() == 1 );
       System.out.print("Bye!");
   }
   // This is checkPalindrome method. It checks if an input string is Palindrome or not.
   // It returns 0 if a string is a Palindrome. Otherwise, it returns a position of a character where it finds
   // a different value.
   // Pre-Condition: string must not be null.
   // Post-Condition: Return 0 if input string is a Palindrome. Return a positive number indicate the location where
   // a difference found.
   public static int checkPalindrome(String strValue) {
       Stack<Character> stack = new Stack<Character>();
       Queue<Character> queue = new LinkedList<Character>();

       // check if string is null. If it is null, return a -1
       if(strValue == null)
           return -1;

       // normalize the string values to lower case, remove spaces
       strValue = strValue.toLowerCase().replaceAll("\W", "");

       // store data on stack/queue adts first
       for(int i=0; i<strValue.length(); i++) {
           char c = strValue.charAt(i);
          
           stack.push(c);
           queue.add(c);
       }

       // loop: comparing, retrieving text, terminate loop if stack is emptied or found a difference
      
       int indexVal = 0;
      
       while(!stack.isEmpty()) {
          
           if(stack.peek() != queue.peek()) {
               return (indexVal+1);
           }
          
           indexVal++;
           stack.pop();
           queue.poll();
       }

       return 0;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote