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
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.

Replies

  • ms_cs
    ms_cs
    Where is the hashcode method in the class Person?
  • shalini_goel14
    shalini_goel14
    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
    ms_cs
    My answer is a
  • shalini_goel14
    shalini_goel14
    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
    ms_cs
    What is wrong in my answer...
  • Yamini L
    Yamini L
    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
    shalini_goel14
    Only two answers * Surprised * ๐Ÿ˜
    Where are other Java users ? Guys, post your answer ๐Ÿ˜€

    Thanks
  • shalini_goel14
    shalini_goel14
    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 x =new ArrayList();
    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
    ms_cs
    e) is the answer.

    reason: Comparator instructs ,,,to sort in reverse order..
  • ms_cs
    ms_cs
    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
    ms_cs
    Whether my answer is right or wrong???
  • ms_cs
    ms_cs
    @Shalini: No updates in this thread...?????
  • shalini_goel14
    shalini_goel14
    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
    shalini_goel14
    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
    ms_cs
    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
    shalini_goel14
    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
    * java.util.Hashtable.
    *


    * The general contract of hashCode 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 the hashCode 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();

    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
    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
    shalini_goel14
    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
    ms_cs
    Sure...Let me see...How your class going to clear my ๐Ÿ˜•...
  • shalini_goel14
    shalini_goel14
    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
    ms_cs
    Oh...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...