Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Data structure class Employee implements Comparable t private String nane; private int wage; private int rank; private char gender; //either f' or 'm public Employee
Data structure
class Employee implements Comparable t private String nane; private int wage; private int rank; private char gender; //either "f' or 'm public Employee boss;//here just try to demo public modifier /Make boss public noy negotively affect Object-Oriented encapsulation public Employee(String n, int , int r, Employee b, char g) this.nome- n; this.wage-w this.rank- ri this.boss b; this.gender g: eoverride //This nethod describes how we would like to compare two Employee objects. //Should we compare two such objects based on rank or woge or other criteria? //It is up to the progranner to decide how the comparison will be made usually on basis of the application needs. coareTo(Object other) nethod will compare this object with other object you passed in. //if this object is bigger thon the other, it returns a positive number. //if this object is snaller than the other, it returns a negative number. //if this object equals to the other, it returns zero. public int compareTo(Object other) t //here comparison is based on the field of rank. If this.rank is bigger than //other.rank, return a positive integer //If this.rank is less than other.rank, return a negative integer. //If this.rank equals to other.rank, you have to further compare their wage in order //to decide whether this object is bigger or the other is bigger or they ore equal. public String getNameo ( return this.name; public char getGenderO return this.gender eOverride public String toStringO ( String r r "Nane:"+ this.nane+Wage:"+ this.wage+,Rank:" +this.rank+ ,Gendar:"+this.gender + Boss:+ this.boss+ return r /end of class public class ComparableDemo ( public void initCobject arr3) // initialize each array element by instantializing Employee class arr[0]- new Employee("Abby", 3000, 1, nul1, f: arr[1] new Employee("John, 2000, 2, CEmployee)arr[0], 'm arr[2] new EmployeeC"Tim, 2008, 2, CEmployee)arr[0], 'm' ar[3neEployeeToy 100, 3, CEmployee arr), This method finds the largest object in the array arr. The largest object is determined on the basis of compareToO method in the Employee class. You have to call compareToO to determine which object is the largest public Object max(Object arra) t public static void main(String args) ComparableDemo demo new ComparableDemoO; // create one-dimensional array of 4 Java Object references. object arr new Object[4]; // populate the array with Employee objects demo.init(arr); //call maxO method to find the largest object in the array Object largest - demo.maxCarr) System.out.printlnC"The Largest object is:1n"+largest); //end of demo class Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started