In: Computer Science
Consider the following program that creates an ArrayList of objects of a type A, and sorst them. Supply the missing code. Sample output when you run the program is shown below.
import java.util.*;
class A
{ int i, j, k;
public A(int i, int j, int k){
this.i=i;
this.j=j;
this.k=k;
}
public String toString(){
return "A("+i+","+j+","+k+")";
}
}
class SortA {
public static void main(String[] args){
ArrayList aL=new ArrayList();
Random rand= new Random(1000); //1000 is a seed value
for (int p=0; p<10; p++){
int i = rand.nextInt(100);
int j = rand.nextInt(200);
int k = rand.nextInt(300);
aL.add(new A(i, j, k));
}
System.out.println("----- Original arraylist------");
for (A a: aL){
System.out.println(a);
}
System.out.println("----- Sorting by first integer-------");
/*YOUR CODE - Use anonymous interface types to sort by first integer Field in A, and then print the resulting ArrayList */
System.out.println("----- Sorting by second integer-------");
/*YOUR CODE - Use anonymous interface types to sort by the second integer Field in A, and then print the resulting ArrayList */
System.out.println("----- Sorting by third integer-------"); /*YOUR CODE - Use anonymous interface types to sort by the third integer Field in A, and then print the resulting ArrayList */
} }
Output
----- Original list -------
A(87,135,276)
A(24,192,149)
A(41,45,164)
A(50,179,259)
A(72,183,36)
A(75,46,202)
A(23,41,222)
A(71,189,202)
A(93,142,49)
A(42,35,176)
----- Sorting by first integer-------
A(23,41,222)
A(24,192,149)
A(41,45,164)
A(42,35,176)
A(50,179,259)
A(71,189,202)
A(72,183,36)
A(75,46,202)
A(87,135,276)
A(93,142,49)
----- Sorting by second integer-------
A(42,35,176)
A(23,41,222)
A(41,45,164)
A(75,46,202)
A(87,135,276)
A(93,142,49)
A(50,179,259)
A(72,183,36)
A(71,189,202)
A(24,192,149)
----- Sorting by third integer-------
A(72,183,36)
A(93,142,49)
A(24,192,149)
A(41,45,164)
A(42,35,176)
A(75,46,202)
A(71,189,202)
A(23,41,222)
A(50,179,259)
A(87,135,276)
The code is
import java.util.*;
class A
{
int i, j, k;
public A(int i, int j, int k) {
this.i = i;
this.j = j;
this.k = k;
}
public String toString() {
return "A(" + i + "," + j + "," + k + ")";
}
}
public class SortA {
public static void main(String[] args) {
ArrayList<A> aL = new ArrayList<A>();
Random rand = new Random(1000); //1000 is a seed value
for (int p = 0; p < 10; p++) {
int i = rand.nextInt(100);
int j = rand.nextInt(200);
int k = rand.nextInt(300);
aL.add(new A(i, j, k));
}
System.out.println("----- Original arraylist------");
for (A a: aL) {
System.out.println(a);
}
System.out.println("----- Sorting by first
integer-------");
Collections.sort(aL,new Comparator()
{
public int compare(Object o1, Object o2)
{
A sa = (A)o1;
A sb = (A)o2;
return sa.i-sb.i;
// it can also return 0, and 1
}
});
for (A a: aL) {
System.out.println(a);
}
/*YOUR CODE - Use anonymous interface types to sort by first integer Field in A, and then print the resulting ArrayList */
System.out.println("----- Sorting by second integer-------");
/*YOUR CODE - Use anonymous interface types to sort by
the second integer Field in A, and then print the resulting
ArrayList */
Collections.sort(aL,new Comparator()
{
public int compare(Object o1, Object o2)
{
A sa = (A)o1;
A sb = (A)o2;
return sa.j-sb.j;
// it can also return 0, and 1
}
});
for (A a: aL) {
System.out.println(a);
}
System.out.println("----- Sorting by third integer-------"); /*YOUR
CODE - Use anonymous interface types to sort by the third integer
Field in A, and then print the resulting ArrayList */
Collections.sort(aL,new Comparator()
{
public int compare(Object o1, Object o2)
{
A sa = (A)o1;
A sb = (A)o2;
return sa.k-sb.k;
// it can also return 0, and 1
}
});
for (A a: aL) {
System.out.println(a);
}
}
}
The output is