Can you answer my Java questions?
OK Guys,
After finding lot of Java users here, I thought of starting this thread where questions on Java (from simple ones to higher ones ) will be asked, not by me ๐. Anyone can ask and anyone can answer.Questions asked will be on random basis, no specific topics will be taken.OK
Only rule is explanation of the answer should be given clearly. Not just answer is enough. No off the topic discussions should be made here.
So here goes a question from my side
After finding lot of Java users here, I thought of starting this thread where questions on Java (from simple ones to higher ones ) will be asked, not by me ๐. Anyone can ask and anyone can answer.Questions asked will be on random basis, no specific topics will be taken.OK
Only rule is explanation of the answer should be given clearly. Not just answer is enough. No off the topic discussions should be made here.
So here goes a question from my side
public class Person{Thanks for following rules also.
private String name, comment;
private int age;
public Person(String n, int a, String c){
name =n; age=a;comment=c;
}
public boolean equals(Object o){
if(!(o instanceof Person) return false;
Person p =(Person) o;
return age==p.age && name.equals(p.name);
}
}
What is the appropriate definition of the hashcode method in class Person.?
Options
a) return super.hashcode();
b) return name.hashcode() + age*7;
c) return name.hashcode() + comment.hashcode()/2;
d) return name.hashcode() + comment.hashcode()/2 -age*3;
Replies
-
ms_csWhere is the hashcode method in the class Person?
-
shalini_goel14
Good question, If I would have given you hashcode() method in Person class, this problem was of no use then. ๐ms_csWhere is the hashcode method in the class Person?
Dear, you have to give hashcode() method's definition yourself out of 4 options, which one will be appropriate definition according to you and why. ๐ -
ms_csMy answer is a
-
shalini_goel14
Please explain your answer ms_cs .ms_csMy answer is a
Hint: Option (a) is wrong.
Other CEans , where are you? Is this question so difficult? It requires only your basic underastanding about Java. ๐ Tough ones I cannot post here because even I also cannot explain them.๐ -
ms_csWhat is wrong in my answer...
-
Yamini LI think appropriate definition of hashcode method should be
b) return name.hashcode() + age*7;
In Person class,equals() method is overridden to find the equality of object.I guess hashcode() method should also be overridden using all the attributes in equals() method. -
shalini_goel14Only two answers * Surprised * ๐
Where are other Java users ? Guys, post your answer ๐
Thanks -
shalini_goel14ok miniy, your answer is correct.
An appropriate hashcode() is one which increases the chances of searching speed . ๐. So option 2 is correct option.
So here goes next question from my side:
Explanation is also expected along with answer ๐import java.util.*; class Flubber { public static void main(String[] args){ List
And the output:x =new ArrayList (); x.add(" x"); x.add("xx"); x.add("Xx"); //insert code here for(String s :x ) System.out.println(s); } }
xx
Xx
[space]x
Which code inserted at //insert code here, will produce the preceding output? (Choose all that apply)
a) Collections.sort(x);
b) Comparable c =Collections.reverse();
Collections.sort(x,c);
c) Comparator c =Collections.reverse();
Collections.sort(x,c);
d) Comparable c =Collections.reverseOrder();
Collections.sort(x,c);
e) Comparator =Collections.reverseOrder();
Collections.sort(x,c);
Thanks -
ms_cse) is the answer.
reason: Comparator instructs ,,,to sort in reverse order.. -
ms_cs
Shall I know how here that second option increases the chances of searching speed?shalini_goel14An appropriate hashcode() is one which increases the chances of searching speed . -
ms_csWhether my answer is right or wrong???
-
ms_cs@Shalini: No updates in this thread...?????
-
shalini_goel14
Yes your answer is 100% right. ๐ms_cse) is the answer.
reason: Comparator instructs ,,,to sort in reverse order..
Natural ordering will produce output in reverse sequence to that listed. The Collections.reverseOrder() method takes a Comparator not a Comparable to re-sort a collection. -
shalini_goel14
Can you prove it why option 1 increases searching speed as compared to option 2 or rest?ms_csShall I know how here that second option increases the chances of searching speed?
Do you know the definition of hashcode() made in super class? Tell?
What would be the seraching speed if Super class has hashcode() definition something like below :
public int hashcode(){
return [some integer ]
} -
ms_csI dont know the definition of that method in super class...I want the reason for that answer...You specified that it increases the speed..That's why I asked How it increases the speed..
,,,,post the definition of the hashcode in supercclass here...
then only I can understand the reason... -
shalini_goel14
Ok , not a problem * Look like you are not clear about this hashcode() concept * ๐ms_csI dont the definition of that method in super class...I want the reason for that answer. Then only I can understand the reason...
As such there is no super class that is directly inherited by Person class(in Qn 1) so as it extends by default java.lang.Object class Ok. Below is the definition of hashcode() method in java.lang.Object (done by Java guys ok , not me ๐ )
/**
Now though the above definition gives us gurantee that it will always return different int value for two different objects and same int values for one object called everytime but still we are given a facility to override this method in our own class(like Person here) so that we can write efficient hashcode() method based on our class and can make the searching process much faster(I think you are getting what i mean by searching here- searching in collections where actually hashcode() is used ๐ )
* Returns a hash code value for the object. This method is
* supported for the benefit of hashtables such as those provided by
*java.util.Hashtable
.
*
* The general contract ofhashCode
is:
*- Whenever it is invoked on the same object more than once during
* an execution of a Java application, the hashCode method
* must consistently return the same integer, provided no information
* used in equals comparisons on the object is modified.
* This integer need not remain consistent from one execution of an
* application to another execution of the same application.
* - If two objects are equal according to the equals(Object)
* method, then calling thehashCode
method on each of
* the two objects must produce the same integer result.
* - It is not required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the hashCode method on each of the
* two objects must produce distinct integer results. However, the
* programmer should be aware that producing distinct integer results
* for unequal objects may improve the performance of hashtables.
*
*
*
* As much as is reasonably practical, the hashCode method defined by
* class Object does return distinct integers for distinct
* objects. (This is typically implemented by converting the internal
* address of the object into an integer, but this implementation
* technique is not required by the
* JavaTM programming language.)
*
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.util.Hashtable
*/
public native int hashCode();
Hope, I have given you now some idea. If still not and you are more curious to know about it, I would say go through this topic once from a good book. ๐
[ PS: A suggestion for you (Take it or Ignore it -Your wish)
If you want to stick to your points/answers while arguing, say from your side also rather than cutting off other points/answers everytime. ๐
]
Thanks - Whenever it is invoked on the same object more than once during
-
ms_cs๐
I wont say, my answer is correct and yours is wrong...right...In my point of view,,,If I used the method super.hashcode then it will return an integer...As you said it will return an distinct integer...from definition we can know that it will return an integer from internal representation of object...So I get an integer...Overriding is possible in java...
For producing that integer value is costs much than super.hashcode...do you agree that?
Ok..Let me come to searching ..Your option will also going to give an integer value...My option will also going to give an integer value...Dont angry...I am ๐ that how here it will increases the searching speed..?????Still ,,,,note that,,I wont say your answer is wrong and my answer is only the correct answer...As you said I have explained my point of view... -
shalini_goel14
Now I need to say, you have to go through this topic through a good book or wait by the time I will give a class in Java thread ๐. You are really very very confused and if I tried to clear your confusion, i will forget my concepts ,Ha ha ๐ms_cs๐
I wont say, my answer is correct and yours is wrong...right...In my point of view,,,If I used the method super.hashcode then it will return an integer...As you said it will return an distinct integer...from definition we can know that it will return an integer from internal representation of object...So I get an integer...Overriding is possible in java...
For producing that integer value is costs much than super.hashcode...do you agree that?
Ok..Let me come to searching ..Your option will also going to give an integer value...My option will also going to give an integer value...Dont angry...I am ๐ that how here it will increases the searching speed..?????Still ,,,,note that,,I wont say your answer is wrong and my answer is only the correct answer...As you said I have explained my point of view... -
ms_csSure...Let me see...How your class going to clear my ๐...
-
shalini_goel14
If you are going to behave like this - no one will ever clear your doubts OK Mr ms_cs . ๐ก ๐ก ๐กms_csSure...Let me see...How your class going to clear my ๐... -
ms_csOh...sorry...ok......I am just waiting for your classes...That's it...
You are reading an archived discussion.
Related Posts
Hello folks, I've been looking for some decent wheelchair lift sketches or planes for my project.
Could anyone be kind enough to help me look for those?
Me and my...
hay friends i have written this programme to print prime noumber but it did not give me desired result so can any one herlp me.๐
Private Sub Form_Load()
'this programm...
So I went autocrossing this weekend, first one of the season in the DC area. Got 11 runs in and was able to shave quite a bit of time off...
When we mesh in ansys sometimes it shows mesh in purpole color and sometimes it shows mesh in green color is there any difference in these cases!!!
what are the basic differences between an all terrain car and an ordinary car in terms of
steering
braking system
suspension
if possible please forward me a link or an...