My set fails

Discussion in 'Java' started by -Rick-, Mar 23, 2008.

  1. -Rick-

    -Rick- Guest

    Could anyone tell me why? How do I fix it?


    package setTest;
    import java.util.*;

    public class SetTest implements Comparable<SetTest> {
    public String toString(){
    return this.s1 + " " + this.x1;
    }
    public static void display(){
    for(SetTest s : set){
    System.out.println(s);
    }
    }
    public static void populate(){
    for (int i = 1; i < 3; i++){
    set.add(new SetTest(1, "One"));
    set.add(new SetTest(2, "One"));
    set.add(new SetTest(1, "Two"));
    }
    }
    public String getX1(){
    return x1;
    }
    public int getS1(){
    return s1;
    }

    public boolean equals(Object o) {
    if (!(o instanceof SetTest))
    return false;
    SetTest s = (SetTest)o;
    if(s.getS1() == this.getS1() && s.getX1() == this.getX1())
    return true;
    else
    return false;
    }
    public int compareTo(SetTest s){
    if(this == s)
    return 1;
    else
    return -1;
    }

    public SetTest(int s, String x){
    s1 = s;
    x1 = x;
    }
    public static void main(String[] args){
    populate();
    System.out.println("The size of set is: " + set.size());
    display();
    }
    private int s1;
    private String x1;
    static protected Set<SetTest> set = new TreeSet<SetTest>();
    }
    -Rick-, Mar 23, 2008
    #1
    1. Advertising

  2. -Rick-

    rossum Guest

    On Sun, 23 Mar 2008 00:49:21 -0700 (PDT), -Rick-
    <> wrote:

    >Could anyone tell me why? How do I fix it?

    How does it fail? What is it doing that you do not want it to do?
    What is it not doing that you want it to do?


    >
    >package setTest;
    >import java.util.*;
    >
    >public class SetTest implements Comparable<SetTest> {
    > public String toString(){
    > return this.s1 + " " + this.x1;
    > }
    > public static void display(){
    > for(SetTest s : set){
    > System.out.println(s);
    > }
    > }
    > public static void populate(){
    > for (int i = 1; i < 3; i++){
    > set.add(new SetTest(1, "One"));
    > set.add(new SetTest(2, "One"));
    > set.add(new SetTest(1, "Two"));
    > }
    > }
    > public String getX1(){
    > return x1;
    > }
    > public int getS1(){
    > return s1;
    > }
    >
    > public boolean equals(Object o) {
    > if (!(o instanceof SetTest))
    > return false;
    > SetTest s = (SetTest)o;
    > if(s.getS1() == this.getS1() && s.getX1() == this.getX1())
    > return true;
    > else
    > return false;
    > }
    > public int compareTo(SetTest s){
    > if(this == s)
    > return 1;
    > else
    > return -1;
    > }

    This implementation of compareTo is almost certainly wrong. compareTo
    returns a negative, zero or positive. Your version here can never
    return zero. You need to check the documentation first.


    >
    > public SetTest(int s, String x){
    > s1 = s;
    > x1 = x;
    > }
    > public static void main(String[] args){
    > populate();
    > System.out.println("The size of set is: " + set.size());

    What is set.size()? You have not declared anything called "set" and
    you have not defined a method called"size()".

    rossum

    > display();
    > }
    > private int s1;
    > private String x1;
    > static protected Set<SetTest> set = new TreeSet<SetTest>();
    >}
    rossum, Mar 23, 2008
    #2
    1. Advertising

  3. -Rick-

    Lew Guest

    -Rick- wrote:
    >> public boolean equals(Object o) {
    >> if (!(o instanceof SetTest))
    >> return false;
    >> SetTest s = (SetTest)o;
    >> if(s.getS1() == this.getS1() && s.getX1() == this.getX1())
    >> return true;
    >> else
    >> return false;


    How come you don't just
    return s.getS1() == this.getS1() && s.getX1() == this.getX1();
    ?

    >> }


    >> public static void main(String[] args){
    >> populate();
    >> System.out.println("The size of set is: " + set.size());


    rossum wrote:
    > What is set.size()? You have not declared anything called "set" and
    > you have not defined a method called"size()".


    -Rick- wrote:
    >> static protected Set<SetTest> set = new TreeSet<SetTest>();
    >> }


    And that is the reason I excoriate placing member declarations at the bottom.

    The standard is to place them before method declarations:
    <http://java.sun.com/docs/codeconv/html/CodeConventions.doc2.html#1852>

    To the OP: You cause confusion when you deviate from the standard or the few
    allowable variations (e.g., the opening brace on its own line indented the
    same as its control statement).

    --
    Lew
    Lew, Mar 23, 2008
    #3
  4. -Rick-

    -Rick- Guest

    On Mar 23, 12:37 pm, Lew <> wrote:
    > -Rick- wrote:
    > >> public boolean equals(Object o) {
    > >> if (!(o instanceof SetTest))
    > >> return false;
    > >> SetTest s = (SetTest)o;
    > >> if(s.getS1() == this.getS1() && s.getX1() == this.getX1())
    > >> return true;
    > >> else
    > >> return false;

    >
    > How come you don't just
    > return s.getS1() == this.getS1() && s.getX1() == this.getX1();
    > ?
    >
    > >> }
    > >> public static void main(String[] args){
    > >> populate();
    > >> System.out.println("The size of set is: " + set.size());

    > rossum wrote:
    > > What is set.size()? You have not declared anything called "set" and
    > > you have not defined a method called"size()".

    > -Rick- wrote:
    > >> static protected Set<SetTest> set = new TreeSet<SetTest>();
    > >> }

    >
    > And that is the reason I excoriate placing member declarations at the bottom.
    >
    > The standard is to place them before method declarations:
    > <http://java.sun.com/docs/codeconv/html/CodeConventions.doc2.html#1852>
    >
    > To the OP: You cause confusion when you deviate from the standard or the few
    > allowable variations (e.g., the opening brace on its own line indented the
    > same as its control statement).
    >
    > --
    > Lew


    @Rossum: I put my class variables at the bottom. All of my java and c
    ++ literature puts them at the bottom. I'm hoping some of their
    genius rubs off on me ;)
    @Lew: I browsed your link and it talks of the order of writing
    variables, not about putting them above or below your method code.

    My error is that it outputs the results twice, ie:
    Compiling 1 source file to C:\myJavaFiles\SetTest\build\classes
    compile:
    run:
    The size of set is: 6
    1 One
    1 Two
    1 Two
    2 One
    2 One
    1 One
    BUILD SUCCESSFUL (total time: 5 seconds)

    Last time I saw this error, it was a super constructor issue. But I
    don't know about this one.
    I abused the compareTo() because I'm trying to compare ints and the
    compiler tells me that I ints can't be dereferenced.

    @Lew: return s.getS1() == this.getS1() && s.getX1() == this.getX1();
    doesn't work because it returns boolean and the compiler wants an int
    (int compareTo()).

    Could someone please help me out in this issue?
    -Rick-, Mar 23, 2008
    #4
  5. -Rick-

    Lew Guest

    -Rick- wrote:
    >>>> public boolean equals(Object o) {
    >>>> if (!(o instanceof SetTest))
    >>>> return false;
    >>>> SetTest s = (SetTest)o;
    >>>> if(s.getS1() == this.getS1() && s.getX1() == this.getX1())
    >>>> return true;
    >>>> else
    >>>> return false;


    Lew wrote:
    >> How come you don't just
    >> return s.getS1() == this.getS1() && s.getX1() == this.getX1();
    >> ?


    -Rick- wrote:
    > @Lew: return s.getS1() == this.getS1() && s.getX1() == this.getX1();
    > doesn't work because it returns boolean and the compiler wants an int
    > (int compareTo()).


    Please note: I did not make that suggestion for compareTo().

    --
    Lew
    Lew, Mar 23, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Saunders
    Replies:
    1
    Views:
    651
    John Saunders
    Nov 18, 2003
  2. Peter Strøiman
    Replies:
    0
    Views:
    528
    Peter Strøiman
    Dec 6, 2004
  3. guyhey
    Replies:
    2
    Views:
    5,966
    zvasanth
    Jan 12, 2008
  4. Replies:
    2
    Views:
    278
    Grant Wagner
    Dec 23, 2004
  5. dayo
    Replies:
    11
    Views:
    325
    Ilya Zakharevich
    Dec 16, 2005
Loading...

Share This Page