Question: JAVA Tester assignment Objective: Program Testing: Creating Test Plans You have been given a class called IntSorter that contains some simple searching and sorting methods:

JAVA Tester assignment

Objective: Program Testing: Creating Test Plans

You have been given a class called IntSorter that contains some simple searching and sorting methods: linearSearch, slectionSort and binarySearch. Your assignment is to prepare a test plan for each of the methods and write an IntSorterTester class that implements this test plan.

Do these first:

  • Create a package called: TestingAndComplexity using Eclipse
  • Create a class called IntSortSearch. Copy and paste the code below.

public class IntSortSearch

{

private static int numComps = 0;

//--------------------------------------------------

public static int getNumComps()

{

return numComps;

}

//--------------------------------------------------

public static int linearSearch(int[] a, int x)

{

numComps = 0;

for (int k = 0; k < a.length; k++)//------Line 1

{

numComps++;

if (a[k] == x)//-----------------------Line 2

{

System.out.println("Num Comps = "+numComps);

return k;//-------------------------Line 3

}

}

System.out.println("Num Comps = "+numComps);

return -1;//------------------------------Line 4

}

//--------------------------------------------------

private static int select(int[] a, int k)

{

//find the location of the smallest element in the array

//between position k and the end of the array

int smallestPos = k; //-------------------Line 5

for (int j = k; j < a.length; j++)

{

numComps++;

if (a[j] < a[smallestPos])//----------Line 6

smallestPos = j;//-----------------Line 7

}

return smallestPos;//--------------------Line 8

}

//--------------------------------------------------

public static void selectionSort(int[] a){

int pos; //position of the smallest elelemt

int temp; //temp variable for swap

numComps = 0;

for (int k = 0; k < a.length-1; k++)//---Line 9

{

//find the smallest element

pos = select(a, k);//-----------------Line 10

//swap it with k-th element

temp = a[k];

a[k] = a[pos];

a[pos] = temp;

}

System.out.println("Num Comps = "+numComps);//Line 11

}

//--------------------------------------------------

public static int binarySearch(int[] a, int x)

{

int lowerLimit = 0;

int upperLimit = a.length-1;

int middle = 0;

numComps = 0;

while (lowerLimit <= upperLimit)//-------------Line 12

{

numComps++;

middle = (lowerLimit+upperLimit) / 2;

if (a[middle] == x){//---------------------Line 13

System.out.println("Num Comps = "+numComps);

return middle;//------------------------Line 14

}

if (a[middle] < x){//----------------------Line 15

lowerLimit = middle+1;//----------------Line 16

} else {

upperLimit = middle-1;//----------------Line 17

}

}

System.out.println("Num Comps = "+numComps);

return -1;//-----------------------------------Line 18

}

//--------------------------------------------------

}

Now write the IntSortSearchTester class. It will contain the following methods:

  • public static void linearSearchTester() This hard-codes the test data for linearSearch method
  • public static void binarySearchTester() This hard-codes the test data for binarySearch method
  • public static void sortTester() This hard-codes the test data for selectionSort method
  • public static void main(String[] args) method which just calls all the tester methods, using a statement like: IntSortSearch.linearSearchTester();

How to create the test data for hardcoding into the tester methods:

  • In each of the two search tester methods, you should have test cases (written in the style of the example given in class) that simulate the following situations:
    • Array containing one element only, and the value being searched for is the element in the array
    • Array containing one element only, and the value being searched for is NOT the element in the array
    • Array containing two unequal elements, and the value being searched for is one of the elements of the array
    • Array containing two unequal elements, and the value being searched for is NOT ANY of the elements of the array
    • Array containing 3 elements, and the value being searched for is the very first element of the array
    • Array containing 3 elements, and the value being searched for is the very last element of the array
    • Array containing 3 elements, and the value being searched for is the middle element
    • Array containing 3 elements, and the value being searched for is NOT ANY of the elements in the array
  • For the one sort tester method, you should have test cases (again, written in the style of the example given in class) that simulate the following situations:
    • Array containing one element only
    • Array containing two unequal elements
    • Array containing 3 elements, already sorted
    • Array containing 3 elements, already sorted in the reverse order
    • Array containing 3 elements, all of them negative
    • Array containing 3 elements, one negative, one zero, one positive
    • Array containing 3 elements, all of them the same value (like: 5,5,5)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!