How can I compare class objects for equality?

  • Thread starter Thread starter russel.arnold
  • Start date Start date
  • Tags Tags
    Class
AI Thread Summary
To compare multiple instances of a class for equality based on their attributes, it's essential to override the equals method in the class. This method should compare the string attributes (s and g) and the integer attribute (i) of the objects. Using a loop is not suitable for direct comparison of two objects; instead, the equals method should handle the logic for checking if the attributes of two instances are identical. This approach provides a clear and efficient way to determine equality without unnecessary complexity.
russel.arnold
Messages
38
Reaction score
0
Hi,

I have a class, say myclass and it takes two strings (let s and g ) and one int ( say i) as its arguments.

It looks like, public class {

String s , g;
int i ;

public void myclass(String sname, String gname, int k){

s = sname;
g = gname;
i = k;
}
}

Now, I have several objects of this class with different values of s, g and i. I want to compare them for equality. i.e if two class objects have same s g and i value, then i want true as my output.

I know this can be done by a loop but is there any shorter way to do this?
 
Technology news on Phys.org
russel.arnold said:
Hi,

I have a class, say myclass and it takes two strings (let s and g ) and one int ( say i) as its arguments.

It looks like, public class {

String s , g;
int i ;

public void myclass(String sname, String gname, int k){

s = sname;
g = gname;
i = k;
}
}

Now, I have several objects of this class with different values of s, g and i. I want to compare them for equality. i.e if two class objects have same s g and i value, then i want true as my output.

I know this can be done by a loop but is there any shorter way to do this?

I'm pretty sure object equality has to be done manually by overriding the equality operator and comparing each field of the two objects.
 
russel.arnold said:
Now, I have several objects of this class with different values of s, g and i. I want to compare them for equality. i.e if two class objects have same s g and i value, then i want true as my output.

I know this can be done by a loop but is there any shorter way to do this?
A loop isn't appropriate at all. There is no way you can write a loop that will compare two strings and an int in one class instance with variables of the same types in another class instance. You will need to do a comparison in the way that DavidSnider suggests.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top