-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaller3.java
More file actions
57 lines (46 loc) · 2.07 KB
/
caller3.java
File metadata and controls
57 lines (46 loc) · 2.07 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
//The main class which creates objects and calls sort method for Question3
public class caller3 {
public static void main(String[] args) throws Exception {
String[] array1= {"bcd","b","abc","aalm","sai","krishna","ireland","america","europe","france",
"india","canada","australia","boy","girl","phone"};
Double[] array2= {9.999,8.68,2.35,6.58,0.199,1.29,2.344,100.0,0.001,123.1,1000.1,45.45,454.9,1.1,1.12,2.11};
//Making sure that the lengths of both the arrays needs to be a power of 2
if(!isPowerOfTwo(array1.length) || !isPowerOfTwo(array2.length))
{
throw new Exception();
}
System.out.println("String Array Before Sorting");
for(int i=0; i < array1.length; i++){
System.out.print(array1[i] + " ,");
}
System.out.println();
System.out.println("Double Array Before Sorting");
for(int i=0; i < array2.length; i++){
System.out.print(array2[i] + " ,");
}
System.out.println();
question4 ob1 = new question4();
question4 ob2 = new question4();
//the comparator for strings and Double numbers are provided as Lambda expressions
ob1.sort(array1, (Object a1, Object a2)->{if(a1==null || a2==null){return 0;} return ((String) a1).compareTo((String)a2);});
ob2.sort(array2, (Object a1, Object a2)->{if(a1==null || a2==null){return 0;} return ((Double) a1).compareTo((Double)a2);});
System.out.println("String Array after sorting");
for(int i=0; i < array1.length; i++){
System.out.print(array1[i] + " ,");
}
System.out.println();
System.out.println("Double Array after sorting");
for(int i=0; i < array2.length; i++){
System.out.print(array2[i] + " ,");
}
System.out.println();
}
//method to check if the length of array is power of 2
static boolean isPowerOfTwo(int n)
{
if(n==0)
return false;
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) ==
(int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
}