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 need a java flowchart diagram for the following code: import java.util.*; public class Main {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");   ...
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   ...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public final class Account implements Comparable {     private String firstName;     private String lastName;     private int accountNumber;     private double balance;     private boolean isNewAccount;     public Account(             String firstName,             String lastName,             int accountNumber,             double balance,             boolean isNewAccount     ) {         this.firstName = firstName;         this.lastName = lastName;         this.accountNumber = accountNumber;         this.balance = balance;         this.isNewAccount = isNewAccount;     }     /**      * TO DO: override equals      */     @Override     public boolean equals(Object other) {...
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...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test the smallest method System.out.print("smallest(1, 0, 2) -> "); System.out.println( smallest(1, 0, 2) ); // test the average method System.out.print("average(95, 85, 90) -> "); System.out.println( average(95, 84, 90) ); } // end main /* * smallest(double, double, double) -> double * * method is given 3 numbers, produces the smallest of the three * * examples: * smallest(1, 0, 2) -> 0.0 */ public static...
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...
Stack2540Array   import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY...
Stack2540Array   import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int top ; String [] stack ; public Stack2540Array () { stack = new String [ CAPACITY ]; top = -1; } 1 public int size () { return top + 1; } public boolean isEmpty () { return (top == -1); } public String top () { if ( top == -1) return null ; return stack [ top ]; }...
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 " +...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result;...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result; String msg; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); if(guess == result) msg = "\nRight!"; else if(guess < result) msg = "\nYour guess was too low"; else msg = "\nYour guess was too high"; JOptionPane.showMessageDialog(null,"The number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT