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.
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.
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...
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) {...
Write a for-each loop that prints all of the Student objects in an ArrayList<student> object called...
Write a for-each loop that prints all of the Student objects in an ArrayList<student> object called roster. Write this piece in Java. Write the correct code to execute.
Using an ArrayList, create a program which does the following: If the user enters quit, the...
Using an ArrayList, create a program which does the following: If the user enters quit, the program ends. If the user enters add followed by a string, program adds the string only to your ArrayList. If the user enters print, the program prints out the ArrayList contents. If the user enters size, the program prints out size of ArrayList, If the user enters remove followed by string, it removes all strings. interface example: **** Welcome to my List Program ****...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a Singapore Airlines Loyalty program. You are asked to write an inheritance hierarchy discount system that benefits KrisFlyer members program to calculate their profit. A brief about KrisFlyer is that it is useful for those who fly on Singapore Airlines (its partners like Virgin Australia and Air New Zealand) frequently. KrisFlyer miles can be earned through credit cards, flying and bonus miles promotions. The miles...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT