Task - Classes and Objects with Arrays - Sorting an array of Employees #93
Replies: 8 comments
-
class Person {
String name;
Person() {
this.name = "";
}
Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Employee extends Person {
double salary;
int yOE;
String uAN;
Employee() {
super();
this.salary = 0;
this.yOE = 0;
this.uAN = " ";
}
Employee(String name, double salary, int yOE, String uAN) {
super(name);
this.salary = salary;
this.yOE = yOE;
this.uAN = uAN;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getyOE() {
return yOE;
}
public void setyOE(int yOE) {
this.yOE = yOE;
}
public String getuAN() {
return uAN;
}
public void setuAN(String uAN) {
this.uAN = uAN;
}
@Override
public String toString() {
return "Employee [salary=" + salary + ", uAN=" + uAN + ", yOE=" + yOE + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
return false;
if (uAN == null) {
if (other.uAN != null)
return false;
} else if (!uAN.equals(other.uAN))
return false;
if (yOE != other.yOE)
return false;
return true;
}
void printDetails() {
System.out.println("Employee Name:" + name);
System.out.println("Employee Salary: " + salary);
System.out.println("Year of Joining: " + yOE);
System.out.println("Employee UAN : " + uAN);
}
public static void sortBySal(Employee[] emps) {
int len = emps.length;
Employee swap;
for (int i = 0; i < len - 1; ++i) {
for (int j = 0; j < len - i - 1; ++j) {
if (emps[j + 1].salary < emps[j].salary) {
swap = emps[j];
emps[j] = emps[j + 1];
emps[j + 1] = swap;
}
}
}
}
}
public class discussion11 {
public static void main(String[] args) {
Employee[] emps = new Employee[3];
emps[0] = new Employee("Amit", 1000, 2010, "27158368867");
emps[1] = new Employee("Suresh", 2000, 2020, "27158368896");
emps[2] = new Employee("Mahesh", 800, 2021, "27158368899");
for (int i = 0; i < emps.length; i++) {
System.out.println(emps[i]);
}
Employee.sortBySal(emps);
for (int i = 0; i < emps.length; i++) {
System.out.println(emps[i]);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
import javax.naming.ldap.SortControl;
class Person {
String name;
}
class Employee extends Person {
double salary;
int year;
String UAN;
Employee(String name, double salary, int year, String UAN) {
this.name = name;
this.salary = salary;
this.year = year;
this.UAN = UAN;
}
String getName() {
return this.name;
}
double getSalary() {
return this.salary;
}
int getYear() {
return this.year;
}
String getUAN() {
return this.UAN;
}
@Override
public String toString() {
return "Employee [Name: " + this.name + " UAN=" + UAN + ", salary=" + salary + ", year=" + year + "]";
}
boolean compareObject(Employee o) {
if (this.name == o.getName() && this.salary == o.getSalary() && this.year == o.getYear()
&& this.UAN == o.getUAN()) {
return true;
}
return false;
}
}
public class EmployeeData {
public static void main(String[] args) {
Employee[] e = new Employee[3];
e[0] = new Employee("john", 10000, 2017, "111jjj");
e[1] = new Employee("jack", 15000, 2016, "333jjj");
e[2] = new Employee("jonas", 7000, 2016, "334jjj");
int len = e.length;
for (int i = 0; i < len - 1; ++i) {
for (int j = 0; j < len - i - 1; ++j) {
if (e[j + 1].getSalary() < e[j].getSalary()) {
Employee swap = e[j];
e[j] = e[j + 1];
e[j + 1] = swap;
}
}
}
for (int i = 0; i < 3; i++) {
System.out.println(e[i]);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
class Person {
private String name;
Person() {
this.name = name;
}
Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
class Employee extends Person {
private String uan;
private double salary;
private int dateOfJoining;
Employee() {
super();
this.salary = 0;
this.dateOfJoining = 0;
this.uan = " ";
}
Employee(String name, String uan, int salary, int dateOfJoining) {
super(name);
this.uan = uan;
this.salary = salary;
this.dateOfJoining = dateOfJoining;
}
public String getUan() {
return uan;
}
public void setUan(String uan) {
this.uan = uan;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getDateOfJoin() {
return dateOfJoining;
}
public void setDateOfJoin(int dateOfJoining) {
this.dateOfJoining = dateOfJoining;
}
@Override
public String toString() {
return "Employee[Person=" + super.getName() + ", salary=" + salary + ", dateOfJoining=" + dateOfJoining + "]";
}
public static void sort(Employee[] emps) {
int len = emps.length;
Employee swap;
for (int i = 0; i < len - 1; ++i) {
for (int j = 0; j < len - i - 1; ++j) {
if (emps[j + 1].salary < emps[j].salary) {
swap = emps[j];
emps[j] = emps[j + 1];
emps[j + 1] = swap;
}
}
}
}
}
public class d11 {
public static void main(String[] args) {
Employee[] emps = new Employee[3];
emps[0] = new Employee("ABC", "123", 10000, 20000);
emps[1] = new Employee("456", "STU", 20000, 10000);
emps[2] = new Employee("PQR", "899", 5000, 50000);
for (int i = 0; i < emps.length; i++) {
System.out.println(emps[i]);
}
Employee.sort(emps);
for (int i = 0; i < emps.length; i++) {
System.out.println(emps[i]);
}
// Person p = new Person("DEF");
// Employee e1 = new Employee("ABC", "XYZ10050", 50000, 2021);
// Employee e2 = new Employee("PQR", "AB057012", 80000, 2022);
// Employee e3 = new Employee("ABC", "XYZ10050", 50000, 2021);
// System.out.println(p);
// System.out.println(e1);
// System.out.println(e2);
// if (p.getName().equals(e1.getName()))
// System.out.println("Person1 works as employee1");
// else if (p.getName().equals(e2.getName()))
// System.out.println("Person1 works as employee2");
// else
// System.out.println("Person1 does not work as employee");
// if (e1.getName() == e2.getName() && e1.getSalary() == e2.getSalary() &&
// e1.getUan() == e2.getUan()) {
// System.out.println("Both Employees are same");
// } else {
// System.out.println("Both Employees are NOT same");
// }
// if (e1.getName() == e3.getName() && e1.getSalary() == e3.getSalary() &&
// e1.getUan() == e3.getUan()) {
// System.out.println("Both Employees are same");
// } else {
// System.out.println("Both Employees are NOT same");
// }
}
} |
Beta Was this translation helpful? Give feedback.
-
package Discussion89;
import java.util.Objects;
class Person {
private String name;
public Person(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
class Employee extends Person {
private double salary;
private int year;
private String uan;
public Employee(String name) {
super(name);
}
public Employee(String name, double salary, int year, String uan) {
super(name);
this.salary = salary;
this.year = year;
this.uan = uan;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getUan() {
return uan;
}
public void setUan(String uan) {
this.uan = uan;
}
@Override
public String toString() {
return "Employee [name=" + getName()+" salary=" + salary + ", year=" + year + ", uan=" + uan + "]";
}
@Override
public int hashCode() {
return Objects.hash(salary, uan, year);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
return Double.doubleToLongBits(salary) == Double.doubleToLongBits(other.salary)
&& Objects.equals(uan, other.uan) && year == other.year;
}
}
public class EmpArray {
public static void bubbleSort(Employee[] emp) {
int len = emp.length;
for (int i = 0; i < len - 1; ++i) {
for (int j = 0; j < len - i - 1; ++j) {
if (emp[j + 1].getSalary() < emp[j].getSalary()) {
Employee swap = emp[j];
emp[j] = emp[j + 1];
emp[j + 1] = swap;
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee[] emp = {new Employee("Rio",76983.23,2020,"ADG110KLS198"),new Employee("Kal",56983.23,2019,"11HGDSH877"),
new Employee("Jack",86983.23,2021,"98WEQU215321")};
bubbleSort(emp);
for(Employee em:emp) {
System.out.println(em);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
public class D93 {
public static void main(String[] args) {
Employee[] emp = new Employee[3];
emp[0] = new Employee("John", 30000, 2010, "ABCD12345");
emp[1] = new Employee("Martha", 20000, 2011, "EFGH0987");
emp[2] = new Employee("Jones", 10000, 2012, "IJKL5678");
System.out.println("Employee Details before sort :");
for (int i = 0; i < emp.length; i++) {
System.out.println(emp[i]);
}
int len = emp.length;
Employee swap;
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - i - 1; ++j) {
if (emp[j + 1].salary < emp[j].salary) {
swap = emp[j];
emp[j] = emp[j + 1];
emp[j + 1] = swap;
}
}
}
System.out.println("Employee details after sort :");
for (int i = 0; i < emp.length; i++) {
System.out.println(emp[i]);
}
}
}
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
class Employee extends Person {
double salary;
int yearOfJoining;
String uan;
public Employee(String name, double salary, int yearOfJoining, String uan) {
super(name);
this.salary = salary;
this.yearOfJoining = yearOfJoining;
this.uan = uan;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getYearOfJoining() {
return yearOfJoining;
}
public void setYearOfJoining(int yearOfJoining) {
this.yearOfJoining = yearOfJoining;
}
public String getUan() {
return uan;
}
public void setUan(String uan) {
this.uan = uan;
}
@Override
public String toString() {
return "Employee [Name=" + getName() + " , salary=" + salary + ", uan=" + uan + ", yearOfJoining="
+ yearOfJoining + "]";
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class task93 {
public static void main(String[] args) {
Employee[] e = new Employee[3];
Person person1 = new Person("John");
e[0] = new Employee("Bob", 75000, 2019, "ABC098");
e[1] = new Employee("John", 12500, 2020, "ADF789");
e[2] = new Employee("Bob", 15000, 2019, "JKY789");
int len = e.length;
for (int i = 0; i < len - 1; ++i) {
for (int j = 0; j < len - i - 1; ++j) {
if (e[j + 1].getSalary() < e[j].getSalary()) {
Employee swap = e[j];
e[j] = e[j + 1];
e[j + 1] = swap;
}
}
}
for (int i = 0; i < e.length; i++) {
System.out.println(e[i]);
}
}
}
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
class Employee extends Person {
private double salary;
private int yearOfJoining;
private String UAN;
public Employee(String name, double salary, int yearOfJoining, String UAN) {
super(name);
this.salary = salary;
this.yearOfJoining = yearOfJoining;
this.UAN = UAN;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getYearOfJoining() {
return yearOfJoining;
}
public void setYearOfJoining(int yearOfJoining) {
this.yearOfJoining = yearOfJoining;
}
public String getUAN() {
return UAN;
}
public void setUAN(String uAN) {
this.UAN = uAN;
}
public boolean compareEmployeetoPerson(Person person) {
return this.getName().equals(person.getName());
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (this.getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (getName() == null) {
if (other.getName() != null)
return false;
} else if (!getName().equals(other.getName()))
return false;
if (UAN == null) {
if (other.UAN != null)
return false;
} else if (!UAN.equals(other.UAN))
return false;
if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
return false;
if (yearOfJoining != other.yearOfJoining)
return false;
return true;
}
@Override
public String toString() {
return "Employee [name=" + super.getName() + ", UAN=" + UAN + ", salary=" + salary + ", yearOfJoining="
+ yearOfJoining + "]";
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
class Employee extends Person {
private double salary;
private int yearOfJoining;
private String UAN;
public Employee(String name, double salary, int yearOfJoining, String UAN) {
super(name);
this.salary = salary;
this.yearOfJoining = yearOfJoining;
this.UAN = UAN;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getYearOfJoining() {
return yearOfJoining;
}
public void setYearOfJoining(int yearOfJoining) {
this.yearOfJoining = yearOfJoining;
}
public String getUAN() {
return UAN;
}
public void setUAN(String uAN) {
this.UAN = uAN;
}
public boolean compareEmployeetoPerson(Person person) {
return this.getName().equals(person.getName());
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (this.getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (getName() == null) {
if (other.getName() != null)
return false;
} else if (!getName().equals(other.getName()))
return false;
if (UAN == null) {
if (other.UAN != null)
return false;
} else if (!UAN.equals(other.UAN))
return false;
if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
return false;
if (yearOfJoining != other.yearOfJoining)
return false;
return true;
}
public static void sort(Employee[] arr) {
int len = arr.length;
for (int i = 0; i < len - 1; ++i) {
for (int j = 0; j < len - i - 1; ++j) {
if (arr[j + 1].getSalary() < arr[j].getSalary()) {
Employee swap = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = swap;
}
}
}
}
@Override
public String toString() {
return "Employee [name=" + super.getName() + ", UAN=" + UAN + ", salary=" + salary + ", yearOfJoining="
+ yearOfJoining + "]";
}
}
class PersonEmployees {
public static void main(String[] args) {
Person person1 = new Person("John");
Employee employee1 = new Employee("Bob", 15000, 2019, "CNMD937319P");
Employee employee2 = new Employee("John", 12500, 2020, "ANMD933459Z");
Employee employee3 = new Employee("Lara", 18000, 2019, "CNMD937319P");
Employee[] arr = { employee1, employee2, employee3 };
Employee.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Refer to this Java file for the
Employeeclass code.Your objective is to create an array of
Employeeinstances of size 3.You can insert the three employee objects in the linked code example into the array.
After insertion, you have to sort the employees in the array according to their salary in ascending order.
You can refer to the below code snippet for implementing the sorting logic:
Modify the logic for comparing object data members instead of plain numbers.
Print the sorted array of employees.
Beta Was this translation helpful? Give feedback.
All reactions