CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@shalini_goel14 • 29 Mar, 2009
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
public class Person{
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;
Thanks for following rules also.
@ms_cs • 29 Mar, 2009 Where is the hashcode method in the class Person?
@shalini_goel14 • 29 Mar, 2009
ms_cs
Where is the hashcode method in the class Person?
Good question, If I would have given you hashcode() method in Person class, this problem was of no use then. 😉

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_cs • 29 Mar, 2009 My answer is a
@shalini_goel14 • 29 Mar, 2009
ms_cs
My answer is a
Please explain your answer ms_cs .

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_cs • 29 Mar, 2009 What is wrong in my answer...
@Yamini L • 29 Mar, 2009 I 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_goel14 • 30 Mar, 2009 Only two answers * Surprised * 😐
Where are other Java users ? Guys, post your answer 😀

Thanks
@shalini_goel14 • 05 Apr, 2009 ok 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:

import java.util.*;
class Flubber {
public static void main(String[] args){
List<String> x =new ArrayList<String>();
x.add(" x");
x.add("xx");
x.add("Xx");

//insert code here

for(String s :x )
System.out.println(s);
}
}
And the output:
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);
Explanation is also expected along with answer 😐

Thanks
@ms_cs • 06 Apr, 2009 e) is the answer.

reason: Comparator instructs ,,,to sort in reverse order..
@ms_cs • 06 Apr, 2009
shalini_goel14
An appropriate hashcode() is one which increases the chances of searching speed .
Shall I know how here that second option increases the chances of searching speed?
@ms_cs • 07 Apr, 2009 Whether my answer is right or wrong???
@ms_cs • 08 Apr, 2009 @Shalini: No updates in this thread...?????
@shalini_goel14 • 08 Apr, 2009
ms_cs
e) is the answer.

reason: Comparator instructs ,,,to sort in reverse order..
Yes your answer is 100% right. 😀


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 • 08 Apr, 2009
ms_cs
Shall I know how here that second option increases the chances of searching speed?
Can you prove it why option 1 increases searching speed as compared to option 2 or rest?

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_cs • 08 Apr, 2009 I 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 • 08 Apr, 2009
ms_cs
I dont the definition of that method in super class...I want the reason for that answer. Then only I can understand the reason...
Ok , not a problem * Look like you are not clear about this hashcode() concept * 😀

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 😉 )

/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hashtables such as those provided by
* <code>java.util.Hashtable</code>.
* <p>
* The general contract of <code>hashCode</code> is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the <tt>hashCode</tt> method
* must consistently return the same integer, provided no information
* used in <tt>equals</tt> 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.
* <li>If two objects are equal according to the <tt>equals(Object)</tt>
* method, then calling the <code>hashCode</code> method on each of
* the two objects must produce the same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the <tt>hashCode</tt> 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.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class <tt>Object</tt> 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
* Java<font size="-2"><sup>TM</sup></font> 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();
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 😀 )

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
@ms_cs • 08 Apr, 2009 😎
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 • 08 Apr, 2009
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...
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 • 08 Apr, 2009 Sure...Let me see...How your class going to clear my 😕...
@shalini_goel14 • 08 Apr, 2009
ms_cs
Sure...Let me see...How your class going to clear my 😕...
If you are going to behave like this - no one will ever clear your doubts OK Mr ms_cs . 😡 😡 😡
@ms_cs • 08 Apr, 2009 Oh...sorry...ok......I am just waiting for your classes...That's it...
7.5k views

Related Posts

@Sarathkumar Chandrasekaran · Oct 15, 2013

Why can’t the shape of the car tyres be revamped or changed to a spherical one? This question araised in me after I stumbled upon the car concept from Audi...
18.4k views

@Ankita Katdare · Jun 1, 2013

Just a couple of weeks back, we told you about ASUS's Teaser For Computex 2013. Though not a lot of fanfare, but ASUS did make its mark and created a...
3.7k views

@Sahithi Pallavi · Jan 8, 2012

Concept of Design of Lacing System in Design of Steel Structures was explained by Dr. Damodar Maity, Department of Civil Engineering , IIT Guwahati. Feel free to ask your questions...
4.7k views

@Manish Goyal · Nov 21, 2009

Today there was my internal viva of dbms in which i was asked 3 questions that i would like to share with you 1 what is partial key? 2 what...
6.7k views

@sheena rewari · Aug 20, 2012

could you please tell me 5 difference between a macro and pre-processor??😀
6.6k views