Create a test plan for a method that tests whether an array of characters is a p
ID: 3681382 • Letter: C
Question
Create a test plan for a method that tests whether an array of characters is a palindrome. A palindrome is a sequence of characters that can be read the same forwards as well as backwards. For example, the arrays {‘r’,’a’,’d’,’a’,’r’} and {‘$’,’1’,’*’,’1’,’$’} are both palindromes while the arrays {‘h’,’e’,’l’,’l’,’o’} and {‘!’,’n’,’n’,’$’} are not. The method will use the following method signature: public static boolean isPalindrome(char [] arr) In your test plan: Consider arrays up to size 5 Make sure you cover all of the possible cases for input Provide at least one test for an array of size 6 Your submission should be a table showing your chosen test cases (input and expected output pairs). Using Eclipse, define a class named PalindromeDetector and define a single (empty) method in it using the above signature. Create a second class as a JUnit test class named PalindromeDetectorTest with a method that includes unit tests for each test case in your above test plan. Using the TDD approach discussed in class and your test plan, fill in the code for the isPalindrome method incrementally, using the tests you outlined above to guide you. When you are done with your code, are you convinced that you wrote enough test cases to ensure that it is a working PalindromeDetector method? If not, add more tests to your class and modify your answers to question 1 to include your new tests. Indicate in your answer to question 1 which tests you added after performing the JUnit tests. Submit a hard copy of your code for the JUnit test class and a hard copy of your code for the PalindromeDetector class. Create a test plan for a method that tests whether an array of characters is in sorted order from smallest to largest or not. Define a class named SortDetector and define a single method with the following method signature: public static boolean isSorted(int [] arr) In your Test Plan: Consider arrays up to size 5 Make sure you cover all of the possible cases for input Test your method on at least one array with a size greater than 5
Explanation / Answer
//PalindromeDetector.java
public class PalindromeDetector {
//The method isPalindrome that takes a character
//array and returns true if character array is palindrom
//otherwise returns false
public static boolean isPalindrome(char [] arr){
char[] c = arr;
int len = c.length - 1;
int i = 0;
while (i < len)
{
if (c[i] != c[len])
{
return false;
}
i++;
len--;
}
return true;
}
}
------------------------------------------------------------------------------------------
//PalindromeDetectorTest.java
/**The class PalindromeDetectorTest that tests the method
* isPalindrome using assertTrue method*/
import static org.junit.Assert.*;
import org.junit.Test;
public class PalindromeDetectorTest {
@Test
public void test() {
//Create an characater array
char str1[]={'r','a','d','a','r'};
//call asssertTrue for str1
assertTrue(PalindromeDetector.isPalindrome(str1));
char str2[]={'$','1','*','1','$'};
//call asssertTrue for str2
assertTrue(PalindromeDetector.isPalindrome(str2));
char str3[]={'h','e','l','l','o'};
//call asssertTrue for str3
//Fail case
assertTrue(PalindromeDetector.isPalindrome(str3));
}
}
---------------------------------------------------------------------------------------------------
//SortDetector.java
public class SortDetector {
/**The method isSorted that takes arr as integer
* array and returns true if the integer array
* is true otherwise returns false*/
public static boolean isSorted(int [] arr){
boolean sorted=true;
for (int i = 0; i < arr.length-1 &&sorted; i++) {
if(arr[i]>arr[i+1])
sorted=false;
}
return sorted;
}
}//end of the class SortDetector
----------------------------------------------------------------------------------------------------------------
//SortDetectorTest.java
//Junit test for class SortDetector
//isSorted method
import static org.junit.Assert.*;
import org.junit.Test;
public class SortDetectorTest {
@Test
public void test() {
//Create an integer array
int arr1[]={1,2,3,4,5};
//call asssertTrue for arr1
assertTrue(SortDetector.isSorted(arr1));
//Create an integer array
int arr2[]={2,3,4,5,6};
//call asssertTrue for arr2
assertTrue(SortDetector.isSorted(arr2));
//Create an integer array
int arr3[]={3,4,5,6,1};
//call asssertTrue for str3
//Fail case
assertTrue(SortDetector.isSorted(arr3));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.