Question

In: Computer Science

Consider the following program that creates an ArrayList of objects of a type A, and sorst...

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)

Solutions

Expert Solution

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


Related Solutions

java please Write a program that creates an ArrayList and adds 5 circle objects to the...
java please Write a program that creates an ArrayList and adds 5 circle objects to the list , and display all elements in the list by invoking the object’s toString() method.
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date object, a ClockWithAudio object, a BMI object, a Day object, and a FigurePane object. Then display all elements in the list. Assume that all classes (i.e. Date, Account, etc.) have their own no-argument constructor.
Write a program that creates three vector objects IN C++. Fill the first two objects with...
Write a program that creates three vector objects IN C++. Fill the first two objects with 25 floating-point numbers using a for loop as follows: 1. fill the first vector object with the loop counter value; 2. fill the second vector object with the loop counter value squared; 3. finally, write a for loop that adds the corresponding elements in the first two vectors, and puts the result in the corresponding element of the third vector. Display all three vectors...
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
Java OVERVIEW This program primarily focuses on the implementation of a ArrayList type interface and the...
Java OVERVIEW This program primarily focuses on the implementation of a ArrayList type interface and the necessary methods to implement the ArrayList. It also includes polymorphism and class comparison. INSTRUCTIONS Your deliverable will be to turn in three files. The files will be named Card.java, PremiumCard.java and the last file will be called CardArrayList.java. For this assignment, any use of a data control structure other than a simple Arrary or String will result in no credit. I am aware that...
Write a java program that creates a hashtable with 10 objects and 5 data members using...
Write a java program that creates a hashtable with 10 objects and 5 data members using mutator and accessor methods for each data member.
Using Java, design a program that creates an array of Card objects, shuffles them, then recursively...
Using Java, design a program that creates an array of Card objects, shuffles them, then recursively sorts them by value and then recursively sorts them by value and suit You will need to create a Card class with the following two fields: • value (a String) • suit (a String) You may include any other fields, constructors, or methods as needed. Your single array will need to contain 52 card objects, one for each value of each suit. The values...
You will generate a People class of object and load an ArrayList with person objects, then...
You will generate a People class of object and load an ArrayList with person objects, then report the contents of that ArrayList. To do so, you must perform the following: (30 pts.) A) (15/30 pts. (line break, 11 pt) ) - Create a class file “People.java” (which will generate People.class upon compile). People.java will have eight(8) methods to 1) read a .txt file ‘people.txt’ 2) generate: ▪ List of all students AND teachers ▪ List of all students OR teachers...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
3.1 Write code that creates an ArrayList object named list and fills list with these numbers...
3.1 Write code that creates an ArrayList object named list and fills list with these numbers (using one or a pair of for or while loops): 0 1 2 3 4 0 1 2 3 4 3.2 Consider the ArrayList object named list containing these Integers: list = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 } What are the contents of list after this loop completes? for (int i = 1; i < 10; ++i) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT