CrazyEngineers
  • Can you answer my Java questions?

    shalini_goel14

    shalini_goel14

    @shalini-goel14-ASmC2J
    Updated: Oct 22, 2024
    Views: 1.1K
    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.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • ms_cs

    MemberMar 28, 2009

    Where is the hashcode method in the class Person?
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberMar 28, 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. 😀
    Are you sure? This action cannot be undone.
    Cancel
  • ms_cs

    MemberMar 28, 2009

    My answer is a
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberMar 29, 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.😉
    Are you sure? This action cannot be undone.
    Cancel
  • ms_cs

    MemberMar 29, 2009

    What is wrong in my answer...
    Are you sure? This action cannot be undone.
    Cancel
  • Yamini L

    MemberMar 29, 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.
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberMar 30, 2009

    Only two answers * Surprised * 😐
    Where are other Java users ? Guys, post your answer 😀

    Thanks
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberApr 5, 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
    Are you sure? This action cannot be undone.
    Cancel
  • ms_cs

    MemberApr 6, 2009

    e) is the answer.

    reason: Comparator instructs ,,,to sort in reverse order..
    Are you sure? This action cannot be undone.
    Cancel
  • ms_cs

    MemberApr 6, 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?
    Are you sure? This action cannot be undone.
    Cancel
  • ms_cs

    MemberApr 7, 2009

    Whether my answer is right or wrong???
    Are you sure? This action cannot be undone.
    Cancel
  • ms_cs

    MemberApr 8, 2009

    @Shalini: No updates in this thread...?????
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberApr 8, 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.
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberApr 8, 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 ]

    }
    Are you sure? This action cannot be undone.
    Cancel
  • ms_cs

    MemberApr 8, 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...
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberApr 8, 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
    Are you sure? This action cannot be undone.
    Cancel
  • ms_cs

    MemberApr 8, 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...
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberApr 8, 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 😁
    Are you sure? This action cannot be undone.
    Cancel
  • ms_cs

    MemberApr 8, 2009

    Sure...Let me see...How your class going to clear my 😕...
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberApr 8, 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 . 😡 😡 😡
    Are you sure? This action cannot be undone.
    Cancel
  • ms_cs

    MemberApr 8, 2009

    Oh...sorry...ok......I am just waiting for your classes...That's it...
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register