compareTo() implementation problems

2019-08-31 01:14发布

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)

2条回答
我命由我不由天
2楼-- · 2019-08-31 01:45

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().

查看更多
三岁会撩人
3楼-- · 2019-08-31 02:03

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);
}
查看更多
登录 后发表回答