import java.util.Arrays;
class Student implements
Comparable<Student>
{
private String name;
private float height, weight, score;
public Student(String name, float height, float weight, float score) {
this.name = name;
this.height = height;
this.weight = weight;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
public void speak() {
System.out.println("I am " + name + ",my height " + height + ",my weight " + weight + ",my score " + score);
}
public int compareTo(Object o) { //这一行不改变
int m = (int) (this.getWeight() * 0.3 + this.getHeight() * 0.3 + this.getScore()*0.4)
- (int) (o.getWeight() * 0.3 + o.getHeight() * 0.3 + o.getScore());
return m;
在上一行不改变的情况下,请问这里怎么修改才可以正确编译,谢谢
}
public String toString() {
return "Person [name=" + name + ",height=" + height + ",weight=" + weight + ",score=" + score + "]";
}
}
public class TestCompare {
public static void main(String[] args) {
int i;
Student ps[] = new Student[6];
ps[0] = new Student("zhangsan", 170, 110, 95);
ps[1] = new Student("lisi", 168, 120, 75);
ps[2] = new Student("wangwu", 165, 115, 88);
ps[3] = new Student("zhaoliu", 172, 121, 90);
ps[4] = new Student("zhouqi", 160, 100, 85);
ps[5] = new Student("zhengba", 166, 119, 70);
System.out.println("array sort before:");
for (i = 0; i < ps.length; i++) {
ps[i].speak();
}
Arrays.sort(ps);
// call sort method
System.out.println("\narray sort after:");
for (i = 0; i < ps.length; i++) {
System.out.println(ps[i]);
}
}
}
这道题只有一个错误,就是object,在不改变object的情况下,请问怎么改