Question

In: Computer Science

import java.util.*; class A { int i, j, k; public A(int i, int j, int k)...

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 Main

{

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-------");

/*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 */

}

}

Ideal 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

Solutions

Expert Solution

Thanks for the question, Here is the complete code in Java. Have used anonymous interface Comparator to sort the array list

=======================================================

import java.util.*;
import java.util.ArrayList;

public 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 Main {
    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-------");
/*YOUR CODE - Use anonymous interface types to sort by first integer Field in A, and then
print the resulting ArrayList */
        Collections.sort(aL, new Comparator<A>() {
            @Override
            public int compare(A objA, A objB) {
                return objA.i - objB.i;
            }
        });
        for (A a : aL) {
            System.out.println(a);
        }
        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<A>() {
            @Override
            public int compare(A objA, A objB) {
                return objA.j - objB.j;
            }
        });
        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<A>() {
            @Override
            public int compare(A objA, A objB) {
                return objA.k - objB.k;
            }
        });
        for (A a : aL) {
            System.out.println(a);
        }
    }
}

==================================================================


Related Solutions

I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class Qup3 implements xxxxxlist {// implements interface    // xxxxxlnk class variables    // head is a pointer to beginning of rlinked list    private node head;    // no. of elements in the list    // private int count; // xxxxxlnk class constructor    Qup3() {        head = null;        count = 0;    } // end Dersop3 class constructor   ...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); HashMap labs = new HashMap(); while (true) { System.out.println("Choose operation : "); System.out.println("1. Create a Lab"); System.out.println("2. Modify a Lab"); System.out.println("3. Delete a Lab"); System.out.println("4. Assign a pc to a Lab"); System.out.println("5. Remove a pc from a Lab"); System.out.println("6. Quit"); int choice = sc.nextInt(); String name=sc.nextLine(); switch (choice) { case 1:...
I need to implement incrementalInserstionSort im stuck on that part import java.util.*; /** * This class...
I need to implement incrementalInserstionSort im stuck on that part import java.util.*; /** * This class represents chains of linked nodes that * can be sorted by a Shell sort. * * @author Charles Hoot * @author Frank M. Carrano * Modified by atb * @author YOUR NAME * @version 9/29/2020 */ public class ChainSort> { private Node firstNode; // reference to first node public ChainSort() { this.firstNode = null; } public void display() { Node currentNode = this.firstNode; while...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num1; double num2; System.out.print("Enter two integers: "); num1 = console.nextInt(); num2 = console.nextInt(); System.out.println(); if (num1 != 0 && num2 != 0) System.out.printf("%.2f\n", Math.sqrt(Math.abs(num1 + num2 + 0.0))); else if (num1 != 0) System.out.printf("%.2f\n", Math.floor(num1 + 0.0)); else if (num2 != 0) System.out.printf("%.2f\n",Math.ceil(num2 + 0.0)); else System.out.println(0); }} a. What is the...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put code here return myint; } public int getInt() { int f = 0; // put code here return f; } } public class Assignment06 { public static void main(String args[]){ System.out.println("Assignmemnt 06 Written by Matt Weisfeld"); int myInt = 0; // create a Factorial object Factorial factorial = new Factorial(); // get a number from the console myInt = factorial.getInt(); System.out.println("Factorial of " +...
INSERT STRING INTO SEPARATE CHAIN HASHTABLE & ITERATE THROUGH HASHTABLE: JAVA _________________________________________________________________________________________________________________ import java.util.*; public class...
INSERT STRING INTO SEPARATE CHAIN HASHTABLE & ITERATE THROUGH HASHTABLE: JAVA _________________________________________________________________________________________________________________ import java.util.*; public class HashTable implements IHash { // Method of hashing private HashFunction hasher; // Hash table : an ArrayList of "buckets", which store the actual strings private ArrayList<List<String>> hashTable; /** * Number of Elements */ private int numberOfElements; private int numberOfBuckets; /** * Initializes a new instance of HashTable. * <p> * Initialize the instance variables. <br> * Note: when initializing the hashTable, make sure to...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT