Help me please. create test case using a already given code. My test implement i
ID: 3555610 • Letter: H
Question
Help me please. create test case using a already given code.
My test implement is keep getting error
package select;
public class selectionSort2 {
public static void main(String[] args)
{
int n = 20;
int[] numArray = unsortedArray(n); // re-initialize
printArray(numArray,n);
selectSort2(numArray, n);
printArray(numArray, n);
}
//Generate data for the selection sort array
public static int[] unsortedArray(int n) {
int[] a = new int[n];
for (int index = 0; index < n; index++) {
a[index] = (n - index);
}
return a;
}
//print the array
public static void printArray(int[] data, int n)
{
for (int index = 0; index < n; index++) {
System.out.print(data[index] + " ");
}
System.out.println();
} public static void selectSort2(int[] data, int n)
{
for (int numUnsorted = n; numUnsorted > 0; numUnsorted--) {
int maxIndex = 0;
for (int index = 1; index < numUnsorted; index++) {
if (data[maxIndex] < data[index])
maxIndex = index;
//swap the element
int temp = data[numUnsorted-1];
data[numUnsorted-1] = data[maxIndex];
data[maxIndex] = temp;
}
}
}
}
Test
public class parameterTest {
private int test;
public parameterTest(int testParameter) {
this.test = testParameter;
}
// creates the test data
@Parameters
public static List<Object> data() {
Object[] data = new Object[] { 20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
return Arrays.asList(data);
}
@Test
public void testException() {
selectionSort2 tests = new selectionSort2();
int []data= {20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7,
6, 5, 4, 3, 2, 1};
int[] expected = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20 };
int n=20;
assertEquals("Result", data,n);
tests.selectSort2(expected, n);
}
}
Explanation / Answer
package abc;
import java.awt.*;
import java.util.Arrays;
import java.util.List;
public class selectionSort2 {
public static void main(String[] args)
{
int n = 20;
int[] numArray = unsortedArray(n); // re-initialize
printArray(numArray,n);
selectSort2(numArray, n);
printArray(numArray, n);
}
//Generate data for the selection sort array
public static int[] unsortedArray(int n) {
int[] a = new int[n];
for (int index = 0; index < n; index++) {
a[index] = (n - index);
}
return a;
}
//print the array
public static void printArray(int[] data, int n)
{
for (int index = 0; index < n; index++) {
System.out.print(data[index] + " ");
}
System.out.println();
} public static void selectSort2(int[] data, int n)
{
for (int numUnsorted = n; numUnsorted > 0; numUnsorted--) {
int maxIndex = 0;
for (int index = 1; index < numUnsorted; index++) {
if (data[maxIndex] < data[index])
maxIndex = index;
//swap the element
int temp = data[numUnsorted-1];
data[numUnsorted-1] = data[maxIndex];
data[maxIndex] = temp;
}
}
}
}
class parameterTest {
private int test;
public parameterTest(int testParameter) {
this.test = testParameter;
}
// creates the test data
public static List<Object> Listdata() {
Object[] data = new Object[] { 20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
return Arrays.asList(data);
}
public void testException() {
selectionSort2 tests = new selectionSort2();
int []data= {20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7,
6, 5, 4, 3, 2, 1};
int[] expected = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20 };
int n=20;
tests.selectSort2(expected, n);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.