I have written three java classes, and now need to sort the input array by the n
ID: 3896590 • 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, and I don't understand how implementing comparable in manager or worker helps me in the new class i am creating.
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
As you have asked just to help you using Comparable to sort workers by name, I am only explaining this. I have also created a class WorkerTester to show you how to sort workers by name.
Below is the code of Worker.java file, which I have changed
public class Worker implements Comparable<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 int compareTo(Worker o) {
return this.name.compareTo(o.name);
}
}
As you can see here, Worker class is implementing Comparable interface and a method called compareTo, which is doing nothing but just returning 0(if equal), negative(if less than) and positive(if greater than) on the basis of name of the worked.
Now below if the code of WorkerTester.java
public class WorkerTester {
public static void main(String[] args) {
Worker workers[] = new Worker[5];
Manager man = new Manager("Rajesh", 12, true);
Producer prod = new Producer("Adam", 111);
Manager man1 = new Manager("Vicky", 15, false);
Producer prod1 = new Producer("Buddy", 444);
Worker work = new Worker("Manjeet");
workers[0] = man;
workers[1] = prod;
workers[2] = man1;
workers[3] = prod1;
workers[4] = work;
System.out.println("Array Before sorting...");
for (int i = 0; i < workers.length; i++) {
System.out.println(workers[i]);
}
System.out.println();
sortByName(workers);
System.out.println("Array After sorting...");
for (int i = 0; i < workers.length; i++) {
System.out.println(workers[i]);
}
}
public static void sortByName(Worker[] arr) {
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j].compareTo(arr[min_idx]) < 0)
min_idx = j;
// Swap the found minimum element with the first
// element
Worker temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
}
Here I am using selection sort to sort by name . as you can see, I am using compareTo on objects directly.
I hope it is clear now.
Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments