Reputation: 121
I have to make a prog using Java generics, and implement Comparable. The code should basically compare 3 peoples age and tell you true or false.
We must include the "int age" variable in our class.
This is what I have done:
@SuppressWarnings("rawtypes")
public class Person implements Comparable<Person>{
int age;
//He said you should have ("int age"), but I dont know how to do this without using generics?
public Person(int age)
{
this.age = age;
}
public int compareTo(Person o) {
return compareTo(o);
}
}
And my Comparing class:
public class OrderedTrio<T> {
@SuppressWarnings("unused")
public static void main(String[] args)
{
//Create Person object
Person personA = new Person(10);
Person personB = new Person(20);
Person personC = new Person(30);
System.out.println(allEqual(personA, personB, personC));
//Create Employee object
}
//All Equal Method: Returns true if all 3 items are equal according to their equals method
public static boolean allEqual(Person personA, Person personB, Person personC)
{
if(personA.compareTo(personB) ==0 && personB.compareTo(personC)==0) //If A=B, B=C then A=C
return true;
else
return false;
}
//Sort Method: Orders items
//ToString Method: Output format: Item1, 2, 3
}
When I run these I get this error: Exception in thread "main" java.lang.StackOverflowError
I think the error is in return compareTo(o) , but I dont understand how to compare my CURRENT object, with the one being passed in.
I also don't know what to use the "int age" variable for, it wont let me compare an int with the Person Object.
Upvotes: 1
Views: 1674
Reputation: 47994
Well this is the cause of your stack overflow error:
public int compareTo(Person o) {
return compareTo(o);
}
compareTo, calls compareTo, calls compareTo, calls.... you get the idea, it never finishes and returns.
A simple comparison of the integer values would be something like
public int compareTo(Person o) {
return age - o.age; //or to be java-y :
//Integer.valueOf(age).compareTo(Integer.valueOf(o.age));
//or Integer.signum(age - o.age);
//I personally don't trust people to use compareTo properly and not look for 1 or -1 :)
}
Edit: also, suppressing rawtype warning is pretty naughty. Raw Type is the exact opposite of generic. If you're getting raw type warnings, you're writing explicitly non-generic code!
Upvotes: 1
Reputation: 21778
public int compareTo(Person o) {
return age-o.age; // youngest first
// return o.age-age; // oldest first
}
Also, read the documentation about compareTo. You appear not doing.
Upvotes: 1
Reputation: 4102
Yes this will result in stack overflow. You have infinite recursion in your compareTo function, calling itself over and over.
The compareTo function is supposed to return an int that represents your "value" you want to give to this object in comparison to the passed in object. So you say you want to compare a person based on age, compareTo should simply return an int based on the age variable, something like the following
compareTo(Person o){
//i'm younger
if (this.age < o.age)
return -1;
//i'm older
if (this.age > o.age)
return 1;
//same age
return 0;
}
Now, when you compare two persons via PersonA.compareTo(PersonB) it should return a value indicated the PersonA is less than (-1) same as (0) or greater than (1) PersonB
this solution is meant to show the general concept of what your trying to do, a more efficient solution would be to just subtract the values from eachother and return that, something like
return age - o.age;
Upvotes: 3
Reputation: 3863
You made your method compareTo
recursive but without any possibility to stop this recursion. Inside your compareTo
method you have to add some condition which will return int
, for example:
public int compareTo(Person o) {
return (age - o.age);
}
This way you will compare two Person
instances based on their age
field. You can't compare to instances of a class like whole instances - it's not the role of compareTo
. You have to make some assumptions on what condition you will compare those two instances and here, as you have only one field age
, good assumption will be to compare to Person
based on their age.
Upvotes: 1