Home › Forums › Computers, Games and Technology › Jave compareTo method
This topic contains 11 replies, has 5 voices, and was last updated by JollyMisanthrope 3 years, 12 months ago.
- AuthorPosts
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.
Anonymous0What exactly is the issue here?
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?
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.
Anonymous0What 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.
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.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,
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).
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.- AuthorPosts
You must be logged in to reply to this topic.

921526
921524
919244
916783
915526
915524
915354
915129
914037
909862
908811
908810
908500
908465
908464
908300
907963
907895
907477
902002
901301
901106
901105
901104
901024
901017
900393
900392
900391
900390
899038
898980
896844
896798
896797
895983
895850
895848
893740
893036
891671
891670
891336
891017
890865
889894
889741
889058
888157
887960
887768
886321
886306
885519
884948
883951
881340
881339
880491
878671
878351
877678