Click here to Skip to main content
16,005,206 members
Home / Discussions / Java
   

Java

 
GeneralRe: help: equals() method to compare the attributes Pin
David Skelly6-Oct-11 22:17
David Skelly6-Oct-11 22:17 
GeneralRe: help: equals() method to compare the attributes Pin
TorstenH.7-Oct-11 0:18
TorstenH.7-Oct-11 0:18 
GeneralRe: help: equals() method to compare the attributes Pin
mastdesi7-Oct-11 4:09
mastdesi7-Oct-11 4:09 
GeneralRe: help: equals() method to compare the attributes Pin
Nagy Vilmos7-Oct-11 4:36
professionalNagy Vilmos7-Oct-11 4:36 
GeneralRe: help: equals() method to compare the attributes Pin
mastdesi7-Oct-11 5:20
mastdesi7-Oct-11 5:20 
GeneralRe: help: equals() method to compare the attributes Pin
Nagy Vilmos7-Oct-11 7:12
professionalNagy Vilmos7-Oct-11 7:12 
GeneralRe: help: equals() method to compare the attributes Pin
mastdesi7-Oct-11 8:52
mastdesi7-Oct-11 8:52 
AnswerRe: help: equals() method to compare the attributes Pin
Nagy Vilmos6-Oct-11 22:43
professionalNagy Vilmos6-Oct-11 22:43 
There is a simple style for equals you should learn, for Vehicule it should be:
Java
public boolean equals(Object obj) {
    // if obj is null or a different type, it can't be equal:
    if (obj == null || this.getClass() != obj.getClass()) {
        return false;
    }

    // caste obj for comparison:
    final Vehicule other = (Vehicule) obj;

    // compare attributes:
    return this.fuelT.equals(other.fuelT) &&
            this.consum == other.consum &&
            this.price == other.price;
}


If you override equals, you should always override hashcode, use the same attributes as are compared in equals:

Java
public int hashCode() {
    int hash = 7;  // any low magnitude prime;
    hash = 47 * hash + (this.fuelT != null ? fuelT.hashcode() : 0);
    hash = 47 * hash + (Double)this.consum.hashCode();
    hash = 47 * hash + this.price;

   return hash;
}


The methods must match so that if they are equal, they should have the same hash code, but the same hash code does not mean two objects are equal.
Similarly if you need to implement Comparable, then the compareTo method must match equals so that if two objects are equal then compareTo returns 0 and if comapreTo is non-zero then the objects are not equal.


Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

GeneralRe: help: equals() method to compare the attributes Pin
mastdesi7-Oct-11 5:17
mastdesi7-Oct-11 5:17 
GeneralRe: help: equals() method to compare the attributes Pin
Nagy Vilmos7-Oct-11 7:14
professionalNagy Vilmos7-Oct-11 7:14 
QuestionPlease help with java assignment Pin
mastdesi6-Oct-11 4:43
mastdesi6-Oct-11 4:43 
AnswerRe: Please help with java assignment Pin
Nagy Vilmos6-Oct-11 5:46
professionalNagy Vilmos6-Oct-11 5:46 
GeneralRe: Please help with java assignment Pin
mastdesi6-Oct-11 6:19
mastdesi6-Oct-11 6:19 
GeneralRe: Please help with java assignment Pin
TorstenH.6-Oct-11 19:44
TorstenH.6-Oct-11 19:44 
GeneralRe: Please help with java assignment Pin
Nagy Vilmos6-Oct-11 23:31
professionalNagy Vilmos6-Oct-11 23:31 
GeneralRe: Please help with java assignment Pin
mastdesi6-Oct-11 6:33
mastdesi6-Oct-11 6:33 
GeneralRe: Please help with java assignment Pin
Nagy Vilmos6-Oct-11 23:58
professionalNagy Vilmos6-Oct-11 23:58 
AnswerRe: Please help with java assignment Pin
I.explore.code12-Oct-11 4:31
I.explore.code12-Oct-11 4:31 
QuestionMultilevelqueue Pin
jhencer1111085-Oct-11 12:40
jhencer1111085-Oct-11 12:40 
AnswerRe: Multilevelqueue Pin
Richard MacCutchan5-Oct-11 21:49
mveRichard MacCutchan5-Oct-11 21:49 
AnswerRe: Multilevelqueue Pin
Nagy Vilmos5-Oct-11 22:35
professionalNagy Vilmos5-Oct-11 22:35 
QuestionTroubble with "new" Pin
Tor Danielsen4-Oct-11 11:00
Tor Danielsen4-Oct-11 11:00 
AnswerRe: Troubble with "new" Pin
DaveAuld4-Oct-11 12:03
professionalDaveAuld4-Oct-11 12:03 
GeneralRe: Troubble with "new" Pin
Tor Danielsen4-Oct-11 12:28
Tor Danielsen4-Oct-11 12:28 
GeneralRe: Troubble with "new" Pin
DaveAuld4-Oct-11 13:37
professionalDaveAuld4-Oct-11 13:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.