in java.. Create a class called StringHashCode which besides a main method provi
ID: 3691745 • Letter: I
Question
in java..
Create a class called StringHashCode which besides a main method provides the following method: public static int strHashCode(String str) This method should return the integer hash code of the string parameter str using the formula given below: Hashcode = str[0]^*31^(n-l) + str[1]^*31^(n-2) +... + str[n-2]^*31^(1)+ str(n-1] Here str[0] means the first character in str and str[ 1 ] means second character of str and so on. With n we denote the length of the string str,^* means multiplication and^represents the power (exponent). For example if str = "abc", then we have: hashcode = 'a'*31^2 + 'b'*31^1 + 'c' Note: Using some character such as 'b' in arithmetic calculations automatically replaces it with its ASCII code. That is, you don't need to worry about it, java will handle it automatically. In the main method of your class, write code to ask the user to enter a string, then pass this string input to the method strHashCode and print the hash code returned by the method on the monitor screen. Then, your program should print to the user: Do you want to enter a new string (Y/N). Based on the use choice, program should proceed. Following is an example run of the program: Enter a string: Adam Hash code for Adam is: 2035631 Do you want to enter a new string (Y/N): y Enter a string: Eve Hash code for Eve is: 70068 Do you want to enter a new string (Y/N): Y Enter a string: SEU Hash code for SEU is: 81987 Do you want to enter a new string (Y/N): n Thank you. Exiting program execution...Explanation / Answer
public static int strHashCode(String str)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.