Jave compareTo method

Topic by JollyMisanthrope

JollyMisanthrope

Home Forums Computers, Games and Technology Jave compareTo method

This topic contains 11 replies, has 5 voices, and was last updated by JollyMisanthrope  JollyMisanthrope 3 years, 12 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #186233
    +1
    JollyMisanthrope
    JollyMisanthrope
    Participant
    3356

    Alright, this project is almost finished but I need to use a compareTo method and haven’t been able to find any examples that are all that clear.

    
    public class Temp 
    {
    private double degrees;
    private char scale;
    private static int tempCount = 0;
    private int tempId = 0;
    
    public Temp()
    {
     this(0,'C');
    }
    
    public Temp(double degrees)
    {
     this(degrees, 'C');
    }
    
    public Temp(char scale)
    {
    this(0, scale);
    }
    
    public Temp(double degrees, char scale)
    {
    this.tempId = ++tempCount;
    this.degrees = degrees;
    this.scale = scale;
    }
    
    public double getC()
    {
    if (scale == 'F')
    
    return 5 * (degrees - 32)/9;
    
    else
    
    return degrees; 
    }
    
    public double getF()
    {
    if(scale == 'C')
    
    return (9*(degrees)/5)+32;
    
    else
    
    return degrees; 
    }
    
    public void setDegrees(double newDegrees)
    {
    degrees = newDegrees;
    }
    
    public void setScale(char newScale)
    {
    scale = newScale;
    }
    
    public void SetBoth(double newDegrees, char newScale)
    {
    degrees = newDegrees;
    scale = newScale;
    }
    
    public static int getTempCount()
    {
        return tempCount;
    }
    
    public int compareTo(Object obj)
    {
    {    
        Temp other = (Temp)obj;
    
        if (getC(this.temp) > getC(other.temp))
        {
            return 1;
        }
        if (getC(this.temp) < getC(other.temp))
        {
            return -1;
        }
        else
            return 0;
    }   }
    
    public boolean equals(Object obj)
    {
        if (!(obj instanceof Temp))
        {
            return false;
        } else 
            { 
                Temp other = (Temp)obj;
                return this.getC() == other.getC();
            }
    }
    
    public String toString() 
    {
      
         return "Temp Object #" + tempId +
                ("\nIn C: " + getC())+
                ("\nIn F: " + getF());
    
    }
    }
    
    
    
    //32 - 212  180 ticks
    
    //0-100  1/10
     
    public class TestTemp
    {
      public static void main(String [] args)
      {
         // only one constructor does any real work
         Temp temp1 = new Temp();         // 0  C
         Temp temp2 = new Temp(32);       // 32 C
         Temp temp3 = new Temp('F');      // 0  F
         Temp temp4 = new Temp(32, 'F');  // 32 F
         
         Temp temp5 = new Temp(); // 0 C
         temp5.setDegrees(10);
         temp5.setScale('F');     // 10 F
         
         System.out.println("C: " + temp1.getC() ); // C: 0.0
         System.out.println("F: " + temp1.getF() ); // F: 32.0
    
         System.out.println(temp1.equals(temp4)); // true
    
         System.out.println(temp1.equals(temp2)); // false
    
         System.out.println("You have " + Temp.getTempCount() ); // You have 5  
    
         
    
       /*  if( temp3.compareTo(temp5)< 0 ) //temp3 is lower than than temp5
         {
            System.out.println("temp3 is lower than than temp5");
         }
         else
         {
            System.out.println("temp3 is same or larger than temp5");
         }
    
    */     System.out.println(temp1); 
          
         /*
            TEMP OBJECT #1
            IN C:  0.0
            IN F: 32.0
    
         */
      }}
    
    

    Everything else is printing like it should be according to the TestTemp requirements.

    The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.
    #186245

    Anonymous
    0

    What exactly is the issue here?

    #186262
    +2
    Eek
    Eek
    Participant
    1162

    Edited: I think I was wrong initially, but be careful of int\double type assumptions made by the compiler as you are calling your main constructor. It may see 0 as an int and not see a matching constructor definition.

    Otherwise yeah what is the problem?

    #186270
    JollyMisanthrope
    JollyMisanthrope
    Participant
    3356

    What exactly is the issue here?

    public int compareTo(Object obj)
    {
    {    
        Temp other = (Temp)obj;
    
        if (getC(this.temp) > getC(other.temp))
        {
            return 1;
        }
        if (getC(this.temp) < getC(other.temp))
        {
            return -1;
        }
        else
            return 0;
    }}
    

    for each this.temp and other.temp I get an error that says “temp cannot be resolved or is not a field”.

    If I change .temp to .degrees I get an error message that says “the method getC() in the type Temp is not applicable for the arguments (double)

    The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.
    #186309
    +1

    Anonymous
    0

    What exactly is the issue here?

    public int compareTo(Object obj)
    {
    {    
        Temp other = (Temp)obj;
    
        if (getC(this.temp) > getC(other.temp))
        {
            return 1;
        }
        if (getC(this.temp) < getC(other.temp))
        {
            return -1;
        }
        else
            return 0;
    }}
    

    for each this.temp and other.temp I get an error that says “temp cannot be resolved or is not a field”.

    If I change .temp to .degrees I get an error message that says “the method getC() in the type Temp is not applicable for the arguments (double)

    Are these the only two classes? I will take the code, paste it into a test project in Eclipse IDE, and try to help you out with this.

    #186338
    JollyMisanthrope
    JollyMisanthrope
    Participant
    3356

    Well Temp is the class and degrees is a private double variable.

    The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.
    #186363
    +1
    Biggvs_Dickvs
    Biggvs_Dickvs
    Participant
    3725

    Quick question guys,

    I’m new to writing Java code, but highly interested. (I’m more on the systems/dba/security side)

    Anyway, can someone explain what it means that the same function(for lack of a better term) “Temp” appears to be declared three different times?:

    public Temp()
    {
    this(0,’C’);
    }

    public Temp(double degrees)
    {
    this(degrees, ‘C’);
    }

    public Temp(char scale)
    {
    this(0, scale);
    }

    public Temp(double degrees, char scale)
    {
    this.tempId = ++tempCount;
    this.degrees = degrees;
    this.scale = scale;
    }

    If I had to guess, it would be to define different behaviors based on how many and what type of arguments are passed to it, but that’s just a guess.

    If you could clarify I would very much appreciate it.

    Thanks,
    Rich

    "Data, I would be delighted to offer any advice I can on understanding women. When I have some, I'll let you know." --Captain Picard,

    #186404
    +1
    Eek
    Eek
    Participant
    1162

    Try this:

    public int (Object obj)
    {
    {    
        Temp other = (Temp)obj;
        // ** changed **    if (this.getC() > other.getC())
        {
            return 1;
        }
        // ** changed **
        if (this.getC() < other.getC())
        {
            return -1;
        }
        else
            return 0;
    }}

    You don’t have a function defined getC(double).

    #186718
    JollyMisanthrope
    JollyMisanthrope
    Participant
    3356

    Try this:

    public int (Object obj)
    {
    {    
        Temp other = (Temp)obj;
        // ** changed **    if (this.getC() > other.getC())
        {
            return 1;
        }
        // ** changed **
        if (this.getC() < other.getC())
        {
            return -1;
        }
        else
            return 0;
    }}

    You don’t have a function defined getC(double).

    Worked like a charm! Though it needed comparedTo after public int. Now I just need to step through the code and get a better idea of what is going on.

    The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.
Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.