0

Hi i'm having trouble implementing the compareTo method. I've looked for answers but nothing has been any help. I'm trying to fill a TreeSet with various sizes of circles. I need compareTo in my circle class to be able to store them this way.

import java.util.*;
import java.lang.*;

abstract class Shape
{ 
private String name; //e.g."circlel", "rectangle3" 

Shape(String name0) 
{
    name = name0;
} 

abstract double area (); // area of shape 

abstract double perim(); // length of perimeter of shape 

void put() 
{ // display shape details 
    System.out.println(name + " with area " + area() 
+ " and perimeter " + perim() );
}
} 

class Circle extends Shape implements Comparable
{
private static String name;
private int radius;

Circle(String n, int r)
{
    super(n);
    radius = r;
}

public double area()
{
    return Math.PI * radius * radius;
}

public double perim()
{
    return 2 * Math.PI * radius;
}

public int compareTo(Circle c)
{
    if(c.name == name && c.radius == radius)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}
}

Edit: Thanks I was forgetting something: Circle is not abstract and does not overide abstract method compareTo(Object) in Comparable

Thanks for the help on that, now that I have gotten down to testing the class, when try to add a circle to the treeset this exception pops up any ideas,

Exception in thread "main" java.lang.NullPointerException
        at Circle.compareTo(Shape.java:47)
        at Circle.compareTo(Shape.java:23)
        at java.util.TreeMap.compare(Unknown Source)
        at java.util.TreeMap.put(Unknown Source)
        at java.util.TreeSet.add(Unknown Source)
        at CircleTreeSet.main(CircleTreeSet.java:24)
Bungalo Soldier
  • 154
  • 1
  • 2
  • 11
  • Would be useful with more information on what errors you get. – Niklas Lindblad Aug 11 '13 at 17:44
  • You need to declare it as `class Circle extends Shape implements Comparable`. Also read [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). Also, as currently implemented there will never be a negative return value, which is needed for _smaller_ circles. – jlordo Aug 11 '13 at 17:46

2 Answers2

3

You are never returning -1 in this method. If one of the circles is "greater" in comparision to the other, that one should return 1 and the other one, if compared to the greater one, should return -1. You must make sure that your circle follows transitive properties and some other guildelines.

Take a look at this reference to the compareTo().

Jsdodgers
  • 5,253
  • 2
  • 20
  • 36
0

You must return -1 if the current instance is less than c, 1 if the current instance is greater than c, and 0 if the instances are equal.

That's how compareTo works. Right now it you're treating it more like an equality check. comparTo does more than that. It should determine if an item is equal, less than, or greater than another item.

This code will group objects with the same name in order of size when sorted. Objects also will be sorted alphabetically by name.

public int compareTo(Circle c)
{
    if(c.name.equals(name)){
        if(c.radius < radius)
        {
            return 1;
        }
        else if(c.radius>radius)
        {
            return -1;
        }
        return 0;
    }
    //names aren't the same compare alphabetically.
    return this.name.compareTo(c.name);
}
William Morrison
  • 10,953
  • 2
  • 31
  • 48
  • [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – jlordo Aug 11 '13 at 17:50
  • Well my face is red, thanks @jlordo that was a small, dumb mistake. Fixed. – William Morrison Aug 11 '13 at 17:51
  • Also, always returning `1` for unequal names seems wrong. As `a` would be bigger then `b` and `b` would be bigger than `a` at the same time. The `SortedSet` would have a hard time inserting. – jlordo Aug 11 '13 at 17:51
  • Agreed, but there's really no correct behavior for that case. – William Morrison Aug 11 '13 at 17:53
  • For grouping when names aren't equal? This specific case is what I mean. Group and sort by names, what happens when 2 names are compared and aren't equal? – William Morrison Aug 11 '13 at 17:56
  • Who's talking about grouping? From the question: "_I'm trying to fill a TreeSet with various sizes of circles_" I would `return name.compareTo(c.name);` in that case to sort it alphapetically. – jlordo Aug 11 '13 at 17:58
  • But by checking that the names are equal, he *is* grouping and sorting by name. – William Morrison Aug 11 '13 at 18:00
  • It doesn't matter what way they are grouped or sorted, My reasoning was that you couldn't store objects in a Set without implementing compareTo within the class so that the set can distinguish between the objects. – Bungalo Soldier Aug 11 '13 at 18:12
  • Great, hopefully my solution will help you. – William Morrison Aug 11 '13 at 18:18
  • @BungaloSoldier: You can just use a `HashSet` instead of a `TreeSet`. In that case, you need to override `hashCode()` and `equals()`. – jlordo Aug 11 '13 at 18:53