Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. (5 points) A hashCode function is used to compute a 32-bit integer representi

ID: 3919943 • Letter: 5

Question

5. (5 points) A hashCode function is used to compute a 32-bit integer representing an Object. Is the following hashCode function poorly written? yes or no and why. COverride public int hashCodeO int temp = 17; //returns a random integer return temp (new Random)).nextIntO 6. (5 points) An equals function is used to compare two objects for equality. Is the following hashCode function poorly written? yes or no and why. 0Override public boolean equals(Object o) if (thiso) return true; return this.xo.x kk this . y o.y && this.angle o.angle k this.currentHealtho.currentHealth;

Explanation / Answer

If you post more than 1 question, as per chegg guidelines I have to solve only first question.

Ques 5.

Yes , the function is poorly written. It is because, the random variable is created directly in the return statement. For a well written java code, the Random class object should be create seprately using

Random rand = new Random();

instead of just using

new Random()

directly in the return statement.

Also, the random number should be generated seprately rather using it in the return statement directly. Like

int x = rand.nextInt();

where

nextInt()

is a function to generate a random integer;

So, the correctly written function should be

@Override

public int hashCode(){

    int temp = 17;

   

    // generate a Random class object

    Random rand = new Random();

   

    // generate a random integer

    int x = rand.nextInt();

   

    return temp * x;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote