-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortableArray.java
More file actions
131 lines (97 loc) · 3.63 KB
/
SortableArray.java
File metadata and controls
131 lines (97 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.Comparator;
public class SortableArray<T extends Comparable<T>> {
private T[] array;
public SortableArray(T[] array) {
this.array = array;
}
public void sort() {
Arrays.sort(array);
}
public void sortStrings() {
Arrays.sort(array, (a, b) -> ((String) a).compareTo((String) b));
}
@Test
public void testSortStrings() {
String[] stringArray = {"banana", "apple", "cherry", "date"};
SortableArray<String> sortableStringArray = new SortableArray<>(stringArray);
sortableStringArray.sortStrings();
String[] expectedArray = {"apple", "banana", "cherry", "date"};
Assert.assertArrayEquals(expectedArray, stringArray);
}
public void sortFloats() {
Arrays.sort(array, new Comparator<T>() {
@Override
public int compare(T float1, T float2) {
return float1.compareTo(float2);
}
});
}
@Test
public void testSortFloats() {
Float[] floatArray = {3.14f, 1.618f, 2.718f, 0.577f};
SortableArray<Float> sortableFloatArray = new SortableArray<>(floatArray);
sortableFloatArray.sortFloats();
Float[] expectedArray = {0.577f, 1.618f, 2.718f, 3.14f};
Assert.assertArrayEquals(expectedArray, floatArray);
}
private void sortIntegers() {
Integer[] intArray = (Integer[]) array;
Arrays.sort(intArray);
array = (T[]) intArray;
}
@Test
public void testSortIntegers() {
Integer[] intArray = {5, 2, 8, 1, 9};
SortableArray<Integer> sortableIntArray = new SortableArray<>(intArray);
sortableIntArray.sort();
Integer[] expectedArray = {1, 2, 5, 8, 9};
Assert.assertArrayEquals(expectedArray, sortableIntArray.getArray());
}
public T[] getArray() {
return array;
}
public void setArray(T[] array) {
this.array = array;
}
public void print() {
System.out.println(Arrays.toString(array));
}
public static void main(String[] args) {
// Test sorting integers
Integer[] intArray = {5, 2, 8, 1, 9};
SortableArray<Integer> sortableIntArray = new SortableArray<>(intArray);
System.out.println("Original array:");
sortableIntArray.print();
sortableIntArray.sortIntegers();
System.out.println("Sorted array:");
sortableIntArray.print();
// Run the unit tests
sortableIntArray.testSortIntegers();
// Test sorting floats
Float[] floatArray = {3.14f, 1.618f, 2.718f, 0.577f};
SortableArray<Float> sortableFloatArray = new SortableArray<>(floatArray);
System.out.println("Original float array:");
sortableFloatArray.print();
sortableFloatArray.sortFloats();
System.out.println("Sorted float array:");
sortableFloatArray.print();
// Run the unit test for sortFloats
sortableFloatArray.testSortFloats();
// Test sorting strings
String[] stringArray = {"banana", "apple", "cherry", "date"};
SortableArray<String> sortableStringArray = new SortableArray<>(stringArray);
System.out.println("Original string array:");
sortableStringArray.print();
sortableStringArray.sortStrings();
System.out.println("Sorted string array:");
sortableStringArray.print();
// Run the unit tests
sortableStringArray.testSortStrings();
//test new change
// test new change 1
// test new change 2
}
}