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

import java.util.*; import java.security.*; import javax.crypto.*; import java.nio.file.*; public class CryptoApp {    public static void...
import java.util.*; import java.security.*; import javax.crypto.*; import java.nio.file.*; public class CryptoApp {    public static void main(String[] args) throws Exception { Crypto crypto = new BasicCrypto();        String welcome = "Hello 2043-er's! Let's try this again :-)"; System.out.println(welcome); // First, where are we? //Let's print out our current working directory        Path cwd = FileSystems.getDefault().getPath("").toAbsolutePath(); System.out.println("Current Working Directory: " + cwd); // Read in our file to encrypt    byte[] originalData = Files.readAllBytes(Paths.get(System.getProperty("user.home"), "C-2044-Sample/Crypto/src/encrypt.txt")); // Encrypt it and...
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]+" ");   ...
Explain the java class below, how it make: import java.util.*; import java.util.concurrent.TimeUnit; public class RacingGame {...
Explain the java class below, how it make: import java.util.*; import java.util.concurrent.TimeUnit; public class RacingGame {       ArrayList<Driver> player = new ArrayList<Driver>();    CarType car = new CarType("Ferrari","2018",280,90,15);    Formula formula = new Formula(5);// number of laps    long startTime = System.currentTimeMillis();    long currentTime = System.currentTimeMillis();    int totalDis = 0;       public static void main(String[] args)    {        RacingGame formulaRace = new RacingGame();        formulaRace.player.add(new Driver("Saleh",10,3));        formulaRace.formula.setDistance(20);//lap distance        formulaRace.totalDis...
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 comments to the following code: PeopleQueue.java import java.util.*; public class PeopleQueue {     public static...
Add comments to the following code: PeopleQueue.java import java.util.*; public class PeopleQueue {     public static void main(String[] args) {         PriorityQueue<Person> peopleQueue = new PriorityQueue<>();         Scanner s = new Scanner(System.in);         String firstNameIn;         String lastNameIn;         int ageIn = 0;         int count = 1;         boolean done = false;         System.out.println("Enter the first name, last name and age of 5 people.");         while(peopleQueue.size() < 5) {             System.out.println("Enter a person");             System.out.print("First Name: ");             firstNameIn...
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:...
*// 1- Add JavaDoc to This classes 2- Mak a UML */ import java.util.*; public class...
*// 1- Add JavaDoc to This classes 2- Mak a UML */ import java.util.*; public class Display { public static void main(String[] args) { altEnerCar car1 = new altEnerCar(20000, 2001, 20000); altEnerCar car2 = new HydrogenCar(0, 2012, 50000, 100, false); altEnerCar car3 = new ElectricCar(0, 2014, 30000, 10, 50); altEnerCar car4 = new NaturalGasCar(0, 2000, 60000, 5, 20); altEnerCar car5 = new PropaneCar(0, 2011, 45000, 10, true); ArrayList<altEnerCar> cars = new ArrayList<altEnerCar>(); cars.add(car1); cars.add(car2); cars.add(car3); cars.add(car4); cars.add(car5); Collections.sort(cars); System.out.println(cars); }...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT