Java Programming
Answer 60a, 60b, 60c, 60d. Show code & output.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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("**************");
}
}
}
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("**************");
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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("**************");
}
}
}
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("**************");
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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("**************");
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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("**************");
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
IF YOU HAVE ANY PROBLEM REGARDING THE SOLUTION PLEASE COMMENT BELOW I WILL HELP YOU
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private...
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 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 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 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; 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 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 = 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 { 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 { /** * @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 void main (String [] args) {System.out. print((int) 5.6); System.out.println((int) 5.2);}}