• Register
First time here? Checkout the FAQ!
x
Welcome to Find4Answers.com

Where you can Ask Questions, Find Answers Or Receive Answers from other members of the community And Share in Social networking sites like facebook, linkedin, twitter.
3cx phone system assignment writing service bandar bola best waikiki restaurants boiler business car car insurance quotes car insurance quotes car insurance quotes car lease deals car leasing uk chwilówka przez internet zapewne company convey convey law complaints convey law reviews convey law service conveylaw cosmetic dentist csr classics hack csr classics hack csr classics hack data data recovery maidenhead deals design development double glazing leeds electrician electrician manchester electricians emergency farm filmy bez limitu filmy online finlock finlock solutions general genral golf holidays spain graduate jobs in london graduate jobs london heap how to jump higher how to jump higher how to jump higher infrared sauna infrared sauna saunas inline skating java judi bola law law firm in leeds lease leasing led lights led bulbs leeds legal smoke life insurance quotes log london manchester mercedes lease deals mercedes leasing mezzanine movies museums and art nikogo szybka chwilówka none none none ny male revue outsource link building paid search agency performance car hire personal personal injury lawyer porcelain veneers ramię szybkie chwilówki recovery restaurant seo seo agencies seo company seo company london seo las vegas seo services services sky diamond seo solutions steel synchronization szybka chwilówka przykład szybka chwilówka żaden szybkie chwilówki lekko temp cover car insurance temp cover car insurance temp cover car insurance thread tymczasem chwilówki przez internet upvc windows manchester viagra kamagra videos vinyl flooring suppliers wait web development company window repairs london windows zobaczyć szybka chwilówka

Which of the following would be valid hashCode methods? [poll]

0 votes

Given the following class definition

public class ValuePair implements Comparable{
    private int  iLookUp;
 
        
    public ValuePair(int iLookUp, String sValue){
        this.iLookUp=iLookUp;     
    }
    
  
    public void setLookUp(int iLookUp){
        this.iLookUp = iLookUp;
    }
    public int getLookUp(){
        return iLookUp;
    }
  
    public boolean equals(Object o){
      if( o instanceof ValuePair){
    ValuePair vp = (ValuePair) o;   
         if(iLookUp == vp.getLookup()){
        return true;
    }
    return false;
}

    
   
    public int compareTo(Object o) {
        ValuePair vp = (ValuePair) o;
        Integer iwLookUp= new Integer(vp.getLookUp());
        if(iwLookUp.intValue() < iLookUp){
            return -1;
        }
        
        if(iwLookUp.intValue() > iLookUp){
            return +1;
        }
        return 0;
    }
   
}

Which of the following would be valid hashCode methods?

1)

public int hashCode() { 
    return (int) System.currentTimeMillis(); 
}

2)

public char hashCode(){
    reutrn (char) iLookUp;
}

3)

public int hashCode(){
    return iLookUp;
}

4)

public int hashCode(){
    return iLookup * 100;
}
Choices:
Ans- 1) (0 votes)
Ans- 2) (0 votes)
Ans- 3) (1 vote)
Ans- 4) (1 vote)
asked Jul 26, 2012 in Java/J2EE by anonymous
    

1 Answer

0 votes

3)

public int hashCode(){
    return iLookUp;
}

4)

public int hashCode(){
    return iLookup * 100;
}

The value returned by the hashCode method must be an integer which rules out option 2 which returns a char value. Option 1 returns a version of the time in milliseconds which is certain to return a different value during a single run of the program, thus breaking one of the specific requirements of the contract for hashCode. The correct options, 3 and 4 may not be particularly good versions of the hashCode method but they are consistent with the equals value and return the correct data type

answered Jul 26, 2012 by sarada Hot Users (2,340 points)

Related questions

0 votes
1 answer 203 views
0 votes
1 answer 197 views
0 votes
1 answer 212 views
...