Base Directions for Provided Code: Write a version of insertion sort that is cap
ID: 3815164 • Letter: B
Question
Base Directions for Provided Code:
Write a version of insertion sort that is capable of sorting an array of objects of any class that implements the Comparable interface. Write Junit tests to thoroughly test your method.
----------------------------------------------------------
Provide JUnit Testing For the Following Code:
import java.util.Arrays;
public class InsertionSortGenerics {
public static <T extends Comparable<T>> void insertionSort(T[] list) {
for (int i=0; i < list.length; i++) {
/* Insert a[i] into the sorted sublist */
T v = list[i];
int j;
for (j = i - 1; j >= 0; j--) {
if (list[j].compareTo(v) <= 0)
break;
list[j + 1] = list[j];
}
list[j+1] = v;
}
}
public static void main(String[] args){
Integer[] test = {1, 0, -4, -6, 2, 1};
insertionSort(test);
System.out.println(Arrays.toString(test));
// test is now {-6, -4, 0, 1, 2, 10}
Student bob = new Student("Bob", 4.0);
Student alice = new Student("Alice", 3.5);
Student carol = new Student("Carol", 3.5);
Student dave = new Student("Dave", 3.7);
Student[] test1 = {bob, alice, dave, carol};
// assume Students are compared first on GPA and then on name
// test is now {bob, dave, alice, carol}
insertionSort(test1);
System.out.println(Arrays.toString(test1));
}
}
class Student implements Comparable<Student>{
private String name;
private double gpa;
public Student(String name, double gpa) {
this.name = name;
this.gpa = gpa;
}
@Override
public int compareTo(Student o) {
if(gpa < o.gpa)
return -1;
else if(gpa > o.gpa)
return 1;
else{
return name.compareTo(o.name);
}
}
@Override
public String toString() {
return name;
}
}
Explanation / Answer
// Junit test case, you need to install jnuit.jar in eclipse to run the junit tc
// Right click the project and Run as Junit to run your jnuit code.
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
public class JnuitSort
{
private static String test_expected1 = "[-6, -4, 0, 1, 1, 2]";
private static String test_expected2 = "[Alice, Carol, Dave, Bob]";
private static String test_expected3 = "[Alice, Carol, Dave, Bob]";
static Integer[] test1 = {1, 0, -4, -6, 2, 1};
static Student bob = new Student("Bob", 4.0);
static Student alice = new Student("Alice", 3.5);
static Student carol = new Student("Carol", 3.5);
static Student dave = new Student("Dave", 3.7);
static Student[] test2 = {bob, alice, dave, carol};
static Double[] test3 = {1.2, 2.9, 3.2, 3.5, 2.1, 1.2};
public static void func()
{
// Test Case 1
InsertionSortGenerics.<Integer>insertionSort(test1);
System.out.println(Arrays.toString(test1));
// Test Case 2
InsertionSortGenerics.<Student>insertionSort(test2);
System.out.println(Arrays.toString(test2));
// Test Case 3
InsertionSortGenerics.<Double>insertionSort(test3);
System.out.println(Arrays.toString(test3));
}
@Test
public void testIntegerSort(){
assertEquals(test_expected1,Arrays.toString(test1));
}
@Test
public void testStringSort() {
assertEquals(test_expected2,Arrays.toString(test2));
}
@Test
public void testDoubleSort() {
assertEquals(test_expected3,Arrays.toString(test3));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.