The code from my class, I don't understand it totally. I read on internet that compareTo() method is used for comparing strings to strings and objects to strings,but in my code it is used to compare float to strings, I guess?! Can you explain how is used comparedTo() method in the code below and why do we use it in our case?
package ro.ase.acs.classes;
import ro.ase.acs.interfaces.Mobility;
//Comparable is a generic interface; click add unimplemented methods ; the method will appear at the end of the class
public final class Car extends Vehicle implements Mobility, Cloneable,Comparable<Car> {
public static final int maxNbOfKm = 1_000_000;//it is static, in order to acces it use .
private EngineType engineType;
private float speed;
public Car() {
super();
engineType = EngineType.gas;
speed = 0;
}
public Car(String _name, EngineType _engineType, float _speed) {
super(_name, true);
engineType = _engineType;
speed = _speed;
}
@Override
public void Start() {
System.out.println("The " + engineType.toString() +
" car has started!");
}
@Override
public void Stop() {
System.out.println("The " + engineType.toString() +
" car has stopped!");
}
@Override
public void DisplaySpeed() {
System.out.println(String.format("The %s car runs with %f km/h",
engineType.toString(), speed));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("The car named ");
sb.append(name);
sb.append(" has a(n) ");
sb.append(engineType.toString());
sb.append(" engine and is running with ");
sb.append(speed);
sb.append(" km/h");
return sb.toString();
}
@Override
public Object clone() throws CloneNotSupportedException {
Car c = (Car)super.clone();
c.engineType = engineType;
c.speed = speed;
return c;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Car)) {
return false;
}
else {
Car c = (Car)obj;
return name.equals(c.name);
}
}
@Override
public int hashCode()
{
return 31*name.hashCode()+13*(HasEngine()?1:0)+7*engineType.hashCode()+(int)speed*100;
}
public enum EngineType { gas, diesel, electric, hybrid }
@Override
public int compareTo(Car o) {
if(speed==o.speed)
{return 0;}
else if (speed>o.speed)
{
return 1;
}
else
{return -1;}
}
// public final int hashCode() {
// return engineType.hashCode();
// };
}
The contract of the Comparable interface, respectively the compareTo()
method is:
In other words: the method provides a mean to define a natural order of objects that can be ordered.
If ordering cars by their speed actually makes sense is a completely different story. I would argue that: no , that doesn't make sense. If at all, you could/should define external Comparator objects that define different ways of "ordering" cars, and then a SpeedComparator would obviously order by speed (but in an explicit way).
It doesn't matter what you compare, you can resolve which comes first and which second by what you define. In compareTo methods, -1 means that it comes first and the element you compare it to, comes after that. 1 means that it comes after the element and 0 means they are equal and doesn't get rearranged.
If speed is bigger then it comes later in your list, if it's smaller it comes after. Strings are compared by length for example, if you do "Chicken".compareTo("Cow") then chicken comes after.
compareTo()
method is used to sort object of a specific class. In your case it compares Car
objects by value of their speed
property.
So you can use it to sort list (or array) of Car
objects by values of their speed
property.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.