我遇到了compareTo的问题

时间:2013-03-24 19:03:23

标签: java methods compareto

好吧,我需要创建一个项目,我有两个接口,它们都用在两个不相关的类中。除了compareTo方法之外,我设法让其他一切正常工作。我制作的两个班级是Car and Horse。我想要做的是将来自Horse的milesGoal与Car中的milesGoal进行比较并返回1,0或-1。

然而,当我尝试这样做时,我得到错误“double无法解除引用”

我一直试图找到不同的方法来处理这部分代码。我尝试在测试器类中使用compareTo而不是制作方法,但我得到了相同的错误,我需要将它变成一个方法。

这是马类:

public class Horse implements milesInterface , kilometersInterface{
private double currentMile;
private double currentKilo;
private double milesGoal;
private double kilosGoal;
private String horseBreed;

// CONSTRUCTORS
public Horse(){
    currentMile = 0;
    currentKilo = 0;
    milesGoal = 0;
    kilosGoal = 0;
    horseBreed = "Unspecified";
}
public Horse(double cm, double ck, double mg, double kg, String hb){
    currentMile = cm;
    currentKilo = ck;
    milesGoal = mg;
    kilosGoal = kg;
    horseBreed = hb;
}

// MILE METHODS
public double remainingMiles(){ // Finds the remaining miles
    return milesGoal-currentMile;
}
public void halfMile(){ // Divides the desired goal halfway (Miles)
    milesGoal = milesGoal/2;
} 
public void setMileGoal(double newMile){ // Allows you to set a new goal
    milesGoal = newMile;
}
public double getMileGoal(){
    return milesGoal;
}

// KILOMETER METHODS
public double remainingKilos(){ // Finds the remaining Kilos
return kilosGoal-currentKilo;
}
public void halfKilo(){ // Divides the desire goal halfway (Kilos)
    kilosGoal = kilosGoal/2;
}
public void setKiloGoal(){ // Allows you to set a new goal
    kilosGoal = milesGoal*1.6;
}
public void setCurrentKilo(){ // Allows you to set the current Kilo
    currentKilo = currentMile * 1.6;
}

// UNIQUE METHODS
public double getMilesStatus(){
    return currentMile;
}
public double getKilosStatus(){
    return currentKilo;
}
public String getHorseBreed(){
    return horseBreed;
}
public void convertToKilos(){ // Converts current miles to kilometers
    double kilos = currentMile * 1.6;
    System.out.println("The current miles to kilometers is: " + kilos + "km.");
}
public void convertToMiles(){ // Converts current kilometers to miles
    double miles = currentKilo * .6;
    System.out.println("The current kilometers to miles is: " + miles + "m.");
}
public void milesPerHour(double hours){ // Calculates the mph to the goal by a time
double answer = milesGoal / hours;
System.out.println("The mph needed to reach the desination in " + hours + " hours: " + answer);
}
 public void kilosPerHour(double hours){ // Calculates the kmph to the goal by a time
double answer = kilosGoal / hours;
System.out.println("The kilometers needed to reach the desination in " + hours + " hours: " + answer);
}
public int compareTo(Object Other){

    if(milesGoal > (Horse)milesGoal.Other)
        return 1;
    if(milesGoal < (Horse)milesGoal.Other)
        return 0;
        return -1;
    }
 }

Car类几乎与Horse类相同,我需要找到一种方法来比较它们的两个里程目标,看看哪一个更大。我尝试了多种方法,但似乎无法正常工作

这是我制作的界面:

abstract interface milesInterface{
public double remainingMiles();
public void halfMile();
public void setMileGoal(double newMile);
public int compareTo(Object Other);
}
abstract interface kilometersInterface{
public double remainingKilos();
public void halfKilo();
public void setCurrentKilo();
public int compareTo(Object Other);
}

2 个答案:

答案 0 :(得分:1)

首先,您正在撰写attribute.object。这是失败的。 Other.milesGoal是更好的选择。

另一个问题是铸造。您正在做的是尝试将milesGoal.other投射到Horse(您想要投射milesGoal

你应该使用

       if (milesGoal > ((Horse) other).milesGoal)

此外,使用适当的大写(变量以小写形式,clases / interfaces以大写形式)和setter和getter。

此外,您可能希望转换为界面,以便您可以将这些方法与其他实现它的条款一起使用

       if (milesGoal > ((MilesInterface) other).milesGoal)

答案 1 :(得分:0)

首先,(Horse)milesGoal.Other应为((Horse) Other).milesGoal

我建议使用一种方法重叠compareTo,以便与Horse进行比较,并使用一种方法与Car进行比较。然后你的代码看起来像

public int compareTo(Horse horse){
    if(milesGoal > horse.milesGoal)
        return 1;
    if(milesGoal < horse.milesGoal)
        return -1;
    return 0;
}

public int compareTo(Car car){
    if(milesGoal > car.milesGoal)
        return 1;
    if(milesGoal < car.milesGoal)
        return -1;
    return 0;
}