Given the below Tesla class, update the class to inherit from the Comparable interface and also write a compareTo method that orders two Tesla objects according to 1. model, 2. range, and 3. color: public class Tesla { private String model; private int range; private String color; public Tesla(String model, int range, String color) { this.model = model; this.range = range; this.color = color; } @Override public boolean equals(Object o) { if (this == o) return true; } else if (!(o instanceof Tesla)) { return false; } else { Tesla t = (Tesla) o; return model.equals(t.model) && range == t.range && color.equals(t.color); } } }
(Java)
Question 6
Explain the answer step-by-step and include verbal explanation. Thank you!
Given the below Tesla class, update the class to inherit from the Comparable interface and also write a compareTo method that orders two Tesla objects according to 1. model, 2. range, and 3. color:
public class Tesla {
private String model;
private int range;
private String color;
public Tesla(String model, int range, String color) {
this.model = model;
this.range = range;
this.color = color;
}
@Override public boolean equals(Object o) {
if (this == o)
return true;
} else if (!(o instanceof Tesla)) {
return false;
} else {
Tesla t = (Tesla) o;
return model.equals(t.model) &&
range == t.range &&
color.equals(t.color);
}
}
}
Step by step
Solved in 2 steps