Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have written three java classes, and now need to sort the input array by the n

ID: 3894246 • Letter: I

Question

I have written three java classes, and now need to sort the input array by the name stored in each object. This is a case-sensitive lexicographic order sort. This is the assignment http://cs.sou.edu/~nordquip/cs257/labs/l6/doc/index.html and this is what I have written previously. My program runs fine, I just cannot figure out how to sort the array. Can you explain how to sort it lexicographically?

public class Manager extends Worker {

// do not produce anything

private int numberSubordinates;

private boolean wellLiked;

public Manager(String name, int numberSubordinates, boolean wellLiked) {

super(name);

this.numberSubordinates = numberSubordinates;

this.wellLiked = wellLiked;

}

public boolean liked(boolean liked) {

if (liked = true) {

wellLiked = true;

return wellLiked;

}

else {

wellLiked = false;

return wellLiked;

}

}

public long work(long units) {

if (units <= 0) {

getUnitsWorked();

return getUnitsWorked();

}

else {

setUnitsWorked(Math.round(units * numberSubordinates * .75) + getUnitsWorked());

return getUnitsWorked();

}

}

public String toString() {

return super.toString() + " , " + numberSubordinates + " ," + wellLiked;

}

public static void main(String[] args) {

Manager m1 = new Manager("BM", 2, false);

m1.work(3);

}

}

public class Worker {

private long unitsWorked;

private String name;

// store name and number of units earned

public Worker(String name) {

this.name = name;

}

public String getName() {

this.name = name;

return name;

}

public long work(long units) {

if (units <= 0) {

return unitsWorked;

}

else {

unitsWorked = units + unitsWorked;

return unitsWorked;

}

}

public String toString() {

return super.toString() + "," + unitsWorked + "," + name;

}

public double pay(double multiplier) {

multiplier = unitsWorked * multiplier;

unitsWorked = 0;

return multiplier;

}

public static void main(String[] args) {

Worker w = new Worker("Jm");

w.work(3);

}

public long getUnitsWorked() {

return unitsWorked;

}

public void setUnitsWorked(long unitsWorked) {

this.unitsWorked = unitsWorked;

}

}

public class Producer extends Worker {

private int productionTarget;

public Producer(String name, int productionTarget) {

super(name);

this.productionTarget = productionTarget;

}

// do not manage anything

public int getProductionTarget() {

this.productionTarget = productionTarget;;

return productionTarget;

}

public String toString() {

return super.toString() + " , " + productionTarget;

}

public long work(long units) {

if (units <= 0) {

getUnitsWorked();

return getUnitsWorked();

}

else {

setUnitsWorked(Math.round(productionTarget * .5 * units) + getUnitsWorked());

return getUnitsWorked();

}

}

}

Explanation / Answer

In the main method add this line, where objects can be an arraylist of objects of workers or managers.
Collections.sort(objects);


Add implements Comparable<Manager> and implements Comparable<Worker> at the class definition like

public class Manager extends Worker implements Comparable<Manager>{

public class Worker implements Comparable<Worker> {

Add the below method in each class
**For Manager class
public int compareTo(Manager other) {
return name.compareTo(other.name);
}

**For Worker class
public int compareTo(Worker other) {
return name.compareTo(other.name);
}

**Comment for any further queries.