Question: Java hashing algorithms (ONLY EDIT WHERE PROMPTED) Directions The four implementations of the hashing algorithm are specified by a String parameter to Hasher.make(String type). The

Java hashing algorithms (ONLY EDIT WHERE PROMPTED)

Directions

The four implementations of the hashing algorithm are specified by a String parameter to Hasher.make(String type). The four algorithms are:

FIRST: specifies a hashing function that returns the ASCII value of the first character of the string. This is not a very effective hashing function!

SUM: specifies a hashing function that returns the sum of the ASCII values of all characters in the string. This is better but still not very effective.

PRIME: specifies a hashing function that returns a value computed as follows. Initialize the hash code to 7, then iterate the string from start to end, multiplying the previous value of the hash code by 31 and adding the ASCII value of the current character.

JAVA: specifies a hashing function that returns the hashCode value from Java, i.e. just call String.hashCode().

WARNING

the hash code may overflow the maximum integer in Java, and thus end up negative. For this reason you should always call Math.abs on the hash code before returning it.

Complete each algorithm for computing the hash code and test them using the main() method in Hasher.java.

Run java Hasher with no parameters and the program will print a usage message.

Then run again specifying the hashing algorithm and the string to hash.

Note: Java has the wonderful capability of being able to specify a main() function for every class, so many developers choose to write test code directly in the class.

-----------------------------------------------------------

@FunctionalInterface

interface HashFunction {

int hash(String key);

}

public class Hasher {

// Hashing algorithms, see specification

/**

* Hashing algorithms, see provided documentation in assignment

* @param hashFunction FIRST, SUM, PRIME, OR JAVA

* @return the corresponding HashFunction

*/

public static HashFunction make(String hashFunction) {

switch (hashFunction) {

case "FIRST":

// YOUR CODE HERE

return null;

case "SUM":

// YOUR CODE HERE

return null;

case "PRIME":

// YOUR CODE HERE

return null;

case "JAVA":

// YOUR CODE HERE

return null;

default:

usage();

}

return null;

}

// Usage message

private static void usage() {

System.err.println("Usage: java Hasher ");

System.exit(1);

}

// Test code for hasher

public static void main(String[] args) {

args = Debug.init(args);

if (args.length != 2)

usage();

HashFunction sh = make(args[0]);

int hashCode = sh != null ? sh.hash(args[1]) : 0;

System.out.printf("'%s' hashes to %d using %s ", args[1], hashCode, args[0]);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!