Question: Data Structure class Enployee implenents Comparable f private String none private int wage: private int rank private char gender: //either f or n public Eelee
class Enployee implenents Comparable f private String none private int wage: private int rank private char gender: //either f or n public Eelee boss;//here just try to deno public nodifier boss;//here just try to deno public modi //Make boss public nay negatively o affect object-oriented encapsulation. public Enployee(String n, int w, int r, Employee b, char g) t this.nane- n: this.wage - this.rank this.boss b this.gender- g: 0Override //This method describes how we would 1ike to compare to Employee objects //Should we compare two such objects based on rank or wage or other criterio? t is up to the progromner to decide how the comparison will be made other) me //p are Tecojec s biogen thod till conpore this eeu pssed in. 7 this object is bigger than the other, it returns a positive number. this object is smaller than the other, it returns a negative number. //if this object equals to the other, it returns zero. public int compareToCObject other) f //here comparison is based on Employee's wage Employee another-CEmployee) other; return this.wage another.wage; //this can be replaced with a if-else statement. public String getName f return this.name; public char get enderO f return this.gender; eoverride public String toStringO String r r + "Name:"+this.name +"Wage:" + this.wage +,Rank:"+ this.rank + ",Gendar:"+ this.gender+ ",Boss:+ this.boss return r //end of Employee class public class ComparableDemo f public void init(Object arrO) initialize each array element by arr[0] new Employee( Abby", 3000, 1, null, f); arr[1] new Employee"John", 2000, 2, (Employee)arr[0], '): arrE2] new Employee("Tim", 2000, 2, CEmployee arr[0], 'm) arrt3 new EmployeeC"Tony", 1000, 3, (Employee)arr [0], ' Employee class Page 1- //This ne theh ng finds ond returns the firss tmptoyee objsct in the arroy nhose none euals to lic Object nane string indString nane, object arr) s method finds how many male Employees are stored in the array arr public int numhalte public static void main(String argsC]) f ComparableDemo demo new Comparabl eDemo); // create one-dimensional array of 4 Java Object references. object arr new Object[4] // populate the array with Employee objects demo.init(arr); //search the name "John object target - demo, find( John", arr); System.out.println Employee with name John is found:n"+ target); //find out how many imale employees in the array int n - demo.numMal e(arr); System.out.println( There aren male employees in the array."); // end mainO /end of demo class Page -2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
