Question

Java Programming

Answer 60a, 60b, 60c, 60d. Show code & output.

public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) {

60a. public class Demo { public static void main(String[] args) { ArrayList<Employee> al = new ArrayList<Employee>(); al.add(

0 0
Add a comment Improve this question Transcribed image text
Answer #1

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

NOTE: WE MUST USE COMPARETO METHOD IN THE EMPLOYEE CLASS INORDER TO SORT ID

WE CAN ALSO USE LAMBDA FUNCTION BUT IT WORKS ONLY IN JAVA 8 OR ABOVE, HENCE I USED COMPARABLE AND COMPARATOR SEE THE OUTPUT BELOW


public class Employee implements Comparable<Employee>{

   private int id;
   private String name;
   private int sal;

   public Employee(int id, String name, int sal) {
       super();
       this.id = id;
       this.name = name;
       this.sal = sal;
   }

   public int getId() {
       return id;
   }

   public void setId(int id) {
       this.id = id;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public int getSal() {
       return sal;
   }

   public void setSal(int sal) {
       this.sal = sal;
   }

   @Override
   public int compareTo(Employee o) {
       return this.getId()-o.getId();
   }

}

0 e Console X <terminated> Demo (2) [Java A > sam 101 1eeee ************** ram 102 2eee ********** 9 Le shyam 103 3000 ******

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

60 a.

USING COMPARABLE AND SORT

import java.util.*;

public class Demo{
  
    public static void main(String[] args) {
        ArrayList<Employee> al = new ArrayList<Employee>();
        al.add(new Employee(102, "ram", 2000));
        al.add(new Employee(103, "shyam", 3000));
        al.add(new Employee(101, "sam", 10000));
        al.add(new Employee(104, "sameer", 40000));

        Collections.sort(al);
      
        for (Employee employee : al) {
            System.out.println(employee.getName());
            System.out.println(employee.getId());
            System.out.println(employee.getSal());
            System.out.println("**************");
        }
    }

}

Console X <terminated> Demo (2) [Java Application > 2 4 56 6 sam 101 10000 88888 ******** 8 9 Demo.java X Employee.java 1 imp

USING COMPARATOR AND SORT

import java.util.*;

public class Demo {
  
   public static void main(String[] args) {
       ArrayList<Employee> al = new ArrayList<Employee>();
       al.add(new Employee(102, "ram", 2000));
       al.add(new Employee(103, "shyam", 3000));
       al.add(new Employee(101, "sam", 10000));
       al.add(new Employee(104, "sameer", 40000));
      
       al.sort(Comparator.comparing(Employee::getId));
       for (Employee employee : al) {
           System.out.println(employee.getName());
           System.out.println(employee.getId());
           System.out.println(employee.getSal());
           System.out.println("**************");
       }
   }
}

Employee.java *Demo.java X 10 import java.util.ArrayList; 2 import java.util.*; 3 4 public class Demo { public static void ma

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

60.b

WITH COMPARABLE AND SORT

import java.util.*;

public class Demo{
  
    public static void main(String[] args) {
        ArrayList<Employee> al = new ArrayList<Employee>();
        al.add(new Employee(102, "ram", 2000));
        al.add(new Employee(103, "shyam", 3000));
        al.add(new Employee(101, "sam", 10000));
        al.add(new Employee(104, "sameer", 40000));

        Comparator c = Collections.reverseOrder();
        Collections.sort(al, c);
      
        for (Employee employee : al) {
            System.out.println(employee.getName());
            System.out.println(employee.getId());
            System.out.println(employee.getSal());
            System.out.println("**************");
        }
    }

}

10 2 Console X <terminated> Demo (2) [Java sameer 104 48eee ************ 56 6 7 Demo.java X Employee.java 1 import java.util.

USING COMPARATOR AND SORT

import java.util.*;

public class Demo {
  
    public static void main(String[] args) {
        ArrayList<Employee> al = new ArrayList<Employee>();
        al.add(new Employee(102, "ram", 2000));
        al.add(new Employee(103, "shyam", 3000));
        al.add(new Employee(101, "sam", 10000));
        al.add(new Employee(104, "sameer", 40000));
      
        al.sort(Comparator.comparing(Employee::getId).reversed());
      
        for (Employee employee : al) {
            System.out.println(employee.getName());
            System.out.println(employee.getId());
            System.out.println(employee.getSal());
            System.out.println("**************");
        }
    }
}

2 4 Employee.java Demo.java X 1 import java.util.*; 3 public class Demo { 50 public static void main(String[] args) { 6 Array

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

60.c

import java.util.*;

public class Demo {
  
    public static void main(String[] args) {
        ArrayList<Employee> al = new ArrayList<Employee>();
        al.add(new Employee(102, "ram", 2000));
        al.add(new Employee(103, "shyam", 3000));
        al.add(new Employee(101, "sam", 10000));
        al.add(new Employee(104, "sameer", 40000));
      
        al.sort(Comparator.comparing(Employee::getSal));
      
        for (Employee employee : al) {
            System.out.println(employee.getName());
            System.out.println(employee.getId());
            System.out.println(employee.getSal());
            System.out.println("**************");
        }
    }
}

Employee.java Demo.java X 1 import java.util.*; 3 public class Demo { 2 4 50 6 7 public static void main(String[] args) { Arr

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

60d.

import java.util.*;

public class Demo {
  
    public static void main(String[] args) {
        ArrayList<Employee> al = new ArrayList<Employee>();
        al.add(new Employee(102, "ram", 2000));
        al.add(new Employee(103, "shyam", 3000));
        al.add(new Employee(101, "sam", 10000));
        al.add(new Employee(104, "sameer", 40000));
      
        al.sort(Comparator.comparing(Employee::getSal).reversed());
      
        for (Employee employee : al) {
            System.out.println(employee.getName());
            System.out.println(employee.getId());
            System.out.println(employee.getSal());
            System.out.println("**************");
        }
    }
}

2 4 50 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Employee.java Demo.java X 1 import java.util.*; 3 public class Demo { publ

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

IF YOU HAVE ANY PROBLEM REGARDING THE SOLUTION PLEASE COMMENT BELOW I WILL HELP YOU

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Add a comment
Know the answer?
Add Answer to:
Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int...

    Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int...

    Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • Java. What is the output of this code? Ace.java public class Ace { private double a;...

    Java. What is the output of this code? Ace.java public class Ace { private double a; public Ace ( double x) {       a = x; } public void multiply (double f) {       a *= x;         } public String toString () {       return String.format ("%.3f", x); AceDem.java public class AceDemo { public static void main(String args[]) {    Ace card = new Ace (2.133); card.multiply (.5); System.out.println (card); } }

  • What is the output of the code below? public class Hobbits { String name; public static...

    What is the output of the code below? public class Hobbits { String name; public static void main(String [] args) { Hobbits [] h = new Hobbits[3]; int z = -1; while (z < 2) { z = z +1; h[z] = new Hobbits(); h[z].name = "bilbo"; if (z == 1) { h[z].name = "frodo"; } if (z == 2) { h[z].name = "sam"; } system.out.println(h[z].name + " is a "); system.out.println("good Hobbit name"); } } } Output: a. bilbo...

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**...

    in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**      * @param args the command line arguments      */         public static void main(String[] args) {       Scanner input=new Scanner(System.in);          Scanner input2=new Scanner(System.in);                             UNOCard c=new UNOCard ();                UNOCard D=new UNOCard ();                 Queue Q=new Queue();                           listplayer ll=new listplayer();                           System.out.println("Enter Players Name :\n Click STOP To Start Game..");        String Name = input.nextLine();...

  • What is the output of the following code? Explain your answer. public class Test {public static...

    What is the output of the following code? Explain your answer. public class Test {public static void main (String [] args) {System.out. print((int) 5.6); System.out.println((int) 5.2);}}

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT