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

This question is relatewd to java field thank you Question 4: (2 Marks) Create a

ID: 3689370 • Letter: T

Question

This question is relatewd to java field

thank you

Question 4: (2 Marks) Create a class called StringHashCode which besides a main method provides the following method: public static int strHashCode(String str) This method should retun the integer hash code of the string parameter str using the formula given below: Hashcode = str[0] *3 1 "in-I)-str[ 1] *31 Yo-2)-... + str[n-2] *31Y1) + str[n-1] Here str[0] means the first character in str and str[ 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 = "abe", ten 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 (YN). Based on the use choice, program should proceed

Explanation / Answer

import java.util.*;
import java.io.*;
public class StringHashCode{
public static int strHashCode(String str){
     int n=str.length();
     char[] s=new char[n];
     int Hashcode=0;
     s=str.toCharArray();
     for(int i=n,j=0;i>0;i--,j++)
     Hashcode+=s[j]*Math.pow(31,(n-1));
   
     Hashcode+=s[n-1];
    return Hashcode;
      }
     public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        char a;
        do
        {
            System.out.println("Enter any string");
         String str=sc.next();
         int code=StringHashCode.strHashCode(str);//calling static method using class name
         System.out.println("Hash code of given string:"+str+"is"+code);
         System.out.println("Do You Want to Continue(Y/N)");
        a = sc.next().trim().charAt(0);// to read a character from console

} while(a=='Y'|| a=='y');
System.out.println("Thank you exiting...");     
     }
}

output;

sh-4.3$ javac StringHashCode.java                                                                    

sh-4.3$ java StringHashCode                                                                          

Enter any string                                                                                     

Adam                                                                                                 

Hash code of given string:Adamis11052570                                                             

Do You Want to Continue(Y/N)                                                                         

y                                                                                                    

Enter any string                                                                                     

Eve                                                                                                  

Hash code of given string:Eveis276869                                                                

Do You Want to Continue(Y/N)                                                                         

n                                                                                                    

Thank you exiting...                                                                                 

sh-4.3$

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