Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By superhaNds

Thread: What's the use of this code ?

  1. #1
    toiletpaper is offline Member
    Join Date
    May 2014
    Rep Power

    Default What's the use of this code ?


    I was scrolling through some classes and i found this piece of code:

    PHP Code:
    public boolean isEqual(Object obj)
    		Die die = (Die) obj;
    		if(die.roll() == die.getFaceValue())
    			return true;
    			return false;
    PHP Code:
    public int roll()
    		facevalue = (int) (Math.random() * MAX) + 1;
    		return facevalue;
    Now i know its creating a method called isEqual that has a boolean return type, but what's happening inside the code exactly ? isn't the die.roll() the same as die.getFaceValue() ? the die.roll() returns the facevalue of the die either way, so whats the use of this method ?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Rep Power

    Default Re: What's the use of this code ?

    You didn't post getFaceValue() yet you ask questions about it. Never mind, your question is easily answered with a counter-question:

    does getFaceValue() contain the same code as roll()? If yes, they're the same. If not, they're not the same. Easy logic. And I'm sorry, but this question warrants the recommendation of reading the following:
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Rep Power

    Default Re: What's the use of this code ?

    Sorry, but I have a few remarks regarding the code:
    - Your Die has a field facevalue. Calling isEqual should not change the object. It should just compare values.
    - calling isEqual should compare the given object with the object that it was called on. But your code compares some return value of the given object with some other return value. The current object is not used at all.
    - Your method gets an Object. Your cast to Die can throw an exception which is not a behaviour that I would expect.

    So some suggestions I would use when implementing isEqual:
    a) In your classes you can first check the references: if (this == obj) return true;
    b) obj could be null - so you can check that, too: if (obj == null) return false;
    c) To avoid the possible exception you can check the class e.g. if (obj.getClass() != this.getClass()) return false;
    d) When these quick checks all wasn't returning a result. then you should go through all your fields that need to be the same for equal elements. So you might want to check if (obj.getFaceValue() == this.getFaceValue()) return true;

    I hope this helped a little bit.

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Rep Power

    Default Re: What's the use of this code ?

    As Kneitzel said, the equals method is poorly implemented. Just some extra notes.

    For the check, you need only to use the instanceof operator on the object to see if it has the correct type, the null check and == are redundant.
    After you perform the check, you can safely cast it to the appropriate type.
    Then, for the fields that you wish to appoint the equality to, do the comparisons. You should not change the state of the object.
    The equals method should be reflexive, transitive and symmetric. Read the documentation for a detailed explanation.
    kneitzel likes this.

Similar Threads

  1. Replies: 1
    Last Post: 03-25-2012, 04:40 AM
  2. Replies: 16
    Last Post: 01-31-2012, 09:36 PM
  3. Replies: 3
    Last Post: 08-10-2011, 10:17 AM
  4. Replies: 0
    Last Post: 08-07-2011, 09:32 PM
  5. Replies: 0
    Last Post: 02-21-2011, 12:50 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