Results 1 to 8 of 8
  1. #1
    Huntersgirl12 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Programming Question - Equality

    Hello! I'm relatively new to Java, and I'm trying to see if the title, numberOfTracks, and playingTime are the same or equal. I currently keep getting an error message saying that the non-static .equals method cannot be referenced from a static type. Any ideas for how I could be able to compare these?

    Here is my code:

    public boolean equals(Object obj)
    {
    if(this == obj){
    return true;
    }
    if(!(obj instanceof CD)){
    return false;
    }
    CD other = (CD) obj;
    return title.equals(super.title)&&
    numberOfTracks == other.numberOfTracks &&
    super.playingTime == other.playingTime;
    }

    public String toString(Object obj)
    {
    CD newCD = (CD) obj;
    if(CD.equals(newCD)){
    System.out.println("You have 2 CD's of this type");
    }
    System.out.println("CD: ");
    return super.toString() + " " + artist +
    "\n tracks: " + numberOfTracks + "\n";
    }

    Thank you!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Programming Question - Equality

    You say you want to compare two instances of CD, other and newCD, correct?

    Then why are you attempting to compare the entire CD class to a single instance, newCD?

    if(CD.equals(newCD)){

    The error message is simply saying that you can't use the equals() method like that.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Huntersgirl12 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Programming Question - Equality

    I'm basically just trying to compare the three categories of one CD to another....which I can see why the CD.equals(newCD) wouldn't work.

    Do you have any ideas as to how I can adjust that to actually work? I'm trying to compare the title, numberOfTracks, and the playingTime.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Programming Question - Equality

    Java Code:
    CD newCD = (CD) obj;
    if (CD.equals(newCD)) {
    That CD in the 'if' is a class. You cannot call an instance method (which is what equals() is) on a class.
    It needs an instance of the class...
    Now, if you want to compare newCD with the instance you have called toString() on, then use the keyword 'this' instead of CD.

    ETA: I really must stop leaving windows open...

  5. #5
    Huntersgirl12 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Programming Question - Equality

    Ok, so I changed the code to:

    public String toString(Object obj)
    {
    CD newCD = (CD) obj;
    if((this.equals(newCD))){
    System.out.println("You have 2 CD's of this type");
    }

    System.out.println("CD: ");
    return super.toString() + " " + artist +
    "\n tracks: " + numberOfTracks + "\n";
    }

    but it doesn't work. It just prints off the artist and number of tracks without the "CD: " or the super.toString.

    Help?!

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Programming Question - Equality

    If you want help, you'll have to provide a short SSCCE that demonstrates exactly what you're doing, for example how exactly you're calling this method.

    But that looks pretty weird for a toString() method. Usually that method simply returns a String, which can then be printed or displayed. It doesn't do any printing itself.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Programming Question - Equality

    This one takes a parameter, but yes..it does look a bit odd.

    I suspect the OP is calling it like this, though:
    Java Code:
    CD myCD = new CD(<some parameters>);
    CD anotherCD = new CD(<some parameters>);
    myCD.toString();

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Programming Question - Equality

    Quote Originally Posted by Tolls View Post
    This one takes a parameter, but yes..it does look a bit odd.

    I suspect the OP is calling it like this, though:
    Java Code:
    CD myCD = new CD(<some parameters>);
    CD anotherCD = new CD(<some parameters>);
    myCD.toString();
    Fair enough. I still think that much of the OP's confusion lies in mixing printing out a String and returning a String. But without that SSCCE, I'm only guessing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. a java programming question
    By akash211 in forum New To Java
    Replies: 1
    Last Post: 10-06-2011, 05:04 PM
  2. Windows Programming Question
    By whenigivein in forum New To Java
    Replies: 3
    Last Post: 07-29-2011, 06:09 PM
  3. programming career advice question
    By silverglade in forum Jobs Discussion
    Replies: 2
    Last Post: 05-23-2011, 07:39 AM
  4. help with simple programming question
    By eezilyBefuddled in forum New To Java
    Replies: 9
    Last Post: 05-18-2011, 08:05 PM
  5. Programming Socket Question
    By paul in forum Networking
    Replies: 1
    Last Post: 07-30-2007, 07:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •