J Alex
J Alex

Reputation: 25

I am having problems with compareTo

Well, I need to make a project where I have two interfaces and they are both used in two unrelated classes. I managed to get everything else to work out properly except for the compareTo method. The two classes I have made are Car and Horse. What I am trying to do is compare the milesGoal from Horse to the one in Car and return either a 1, 0, or -1.

However when I try doing this I get the error "double could not be dereferenced"

I have been stuck on this for a while trying to find different ways to approach this part of the code. I tried using compareTo in the tester class instead of making a method but I got the same error and I am required to make it into a method.

This is the Horse Class:

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;
    }
 }

The Car class is pretty much the same as the Horse one and I need to find a way to compare both of their milesGoal to see which one is greater. I tried multiple things but it doesn't seem to work

This is the interface I made:

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);
}

Upvotes: 0

Views: 98

Answers (2)

raptortech97
raptortech97

Reputation: 507

Firstly, (Horse)milesGoal.Other should be ((Horse) Other).milesGoal.

I would suggest overloading compareTo with one method for comparing to Horses and one method for comparing to Cars. Then your code looks like

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;
}

Upvotes: 0

SJuan76
SJuan76

Reputation: 24780

First, you are writting attribute.object. This is what fails. Other.milesGoal is a better option.

Another problema is with the casting. What you are doing is trying to cast milesGoal.other to Horse (you want to cast milesGoal)

You should use

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

Also, use proper capitalization (variables go in lowercase, clases/interfaces in uppercase) and setters and getters.

Additionally, you probably will want to cast to the interface so you can use the methods with other clases that implement it

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

Upvotes: 1

Related Questions