I was given this class and told to fix errors and Finnish the class.
For this assignment, you will need to develop a test driver and a comparable class. Luckily, Prof. Fuller has such class already written (well most of it), you just need to add some code and debug some errors. This is what I have as well as what was given.
public class Jedi implements Comparable {
private String name;
private double midi;
public Jedi(String name, double midi) {
this.name = name;
this.midi = midi;
}
public Jedi(String name) {
this.name = name;
this.midi = -1;
}
public String toString() {
return "Jedi " + name + "\nMidi-chlorians count : " + midi + "\n";
}
public double getMidi() {
return midi;
}
public String getName() {
return name;
}
// returns true if the object’s midi value are equal and false otherwise – CODE INCOMPLETE
public boolean equals(Jedi other) {
return this.getMidi() == other.getMidi();
}
// returns -1 if less, 1 if larger, or 0 if it is an equal midi count – CODE INCOMPLETE
public int compareTo(Object other) {
if (getMidi() < other.getMidi()) {
return -1;
} else if (getMidi > other.getMidi()) {
return 1;
} else if (this.equals(other)) {
return 0;
}
}
}
I keep getting a cannot find symbol - method getMidi()
What is wrong with this because I can not figure it out?
This is the problem:
public int compareTo(Object other)
You've said you can compare this object to any other object. You can't call other.getMidi()
, because getMidi()
isn't a method declared on Object
.
I would suggest you change your class and method declarations to use the fact that Comparable<T>
is generic:
public class Jedi implements Comparable<Jedi> {
...
public int compareTo(Jedi other) {
...
}
}
See more on this question at Stackoverflow