Using Junit method creatd test function. not work.. helpme please package select
ID: 3555606 • Letter: U
Question
Using Junit method creatd test function. not work.. helpme please
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;
}
}
}
}
J-unit case
package select;
import static org.junit.Assert.*;
import static org.junit.Assert.assertArrayEquals;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @author secc
*
*/
public class exceptionTest {
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
/**
* @throws java.lang.Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
/**
* {@
*/
@Test
public void testExceptionIsThrown()
{
selectionSort2 test = 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 n=20;
test.selectSort2(data, n);
}
@Test
public void testSelectSort2() {
selectionSort2 test = 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;
selectionSort2.selectSort2(data,n);
assertArrayEquals(expected, n);
}
}
Explanation / Answer
hey,
you can use this
package select;
public class selectionSort2 {
int n = 20;
int[] numArray = unsortedArray(n); // re-initialize
//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 int [] 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;
}
}
return data;
}
}
package select;
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Test;
/**
* @author secc
*
*/
public class testSelectSort2{
public static void main(String args[]) {
int[] num = { 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7,6, 5, 4, 3, 2, 1 };
int n=num.length;
int[] expected = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,17, 18, 19, 20 };
num=selectionSort2.selectSort2(num, n);
selectionSort2.printArray(num, n);
assertArrayEquals(expected, num);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.