Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two thoughts.</p> <ol> <li><p>I'm not sure what purpose the <code>name</code> variable would actually <em>serve</em>, since there's no way to access it from outside of the class, nor is it used <em>inside</em> the class. If it's not used, remove it.</p></li> <li><p>Your <code>compareTo</code> is simply incorrect. Think of a comparison like this.</p> <ul> <li>How do I determine equivalence? How are two <code>Circles</code> deemed equivalent?</li> <li>How do I determine the natural ordering? In what way are <code>Circles</code> ordered by?</li> </ul></li> </ol> <p>A <code>TreeSet</code> cares about the <a href="http://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html" rel="nofollow">natural ordering</a> of its elements.</p> <hr> <p>Let's define some laws about <code>Circles</code>, then.</p> <ul> <li>A <code>Circle</code> is equivalent to another <code>Circle</code> if and only if their radius is equivalent.</li> <li>A <code>Circle</code> is lesser in rank to another <code>Circle</code> if and only if their radius is lesser rank to another <code>Circle</code>.</li> <li>A <code>Circle</code> is higher in rank to any nonexistent <code>Circle</code>.</li> </ul> <p>Let's move forward with those laws, and define <code>compareTo</code>. To fully complete this, though, we need a getter for <code>radius</code>:</p> <pre><code>public Integer getRadius() { return Integer.valueOf(radius); } </code></pre> <p>I'm leveraging <code>Integer</code> instead of <code>int</code>, since <code>Integer</code> is also <code>Comparable</code>. It makes our <code>compareTo</code> a little easier.</p> <pre><code>public int compareTo(final Circle other) { if(other == null) { return 1; } else { return Integer.valueOf(radius).compareTo(other.getRadius()); } } </code></pre> <p>An alternative way, as pointed out in the comments, would also allow you to take the difference of our radius, and the other object's radius, which would satisfy the general contract for <code>Comparable</code> - if the difference is 0, they're equivalent; if it's greater than 0, then it's larger; is it's less than 0, then it's smaller.</p> <p>To do that, we change our getter to return <code>int</code>:</p> <pre><code>public int getRadius() { return radius; } </code></pre> <p>...and modify our <code>compareTo</code> as such:</p> <pre><code>public int compareTo(final Circle other) { if(other == null) { return 1; } else { return radius - other.getRadius(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload