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

Write a program ( HashCodeForString.java ) that displays the hash code for each

ID: 3915194 • Letter: W

Question

Write a program ( HashCodeForString.java ) that displays the hash code for each of the

elements in an array list. Write a method that returns a hash code for a string using the approach described in Section 27.3.2 with b value 31. The function header is as follows:

public static int hashCodeForString(String s) Write another method that returns an ArrayList from a set with the following function

header:

public static <E> ArrayList<E> setToList(Set<E> s)

You can start with the following code:

Import java.util.ArrayList;

import java.util.Set;

import java.util.HashSet;

public class HashCodeForString {

public static void main(String[] args) {

// Create a hash set

Set<String> set = new HashSet<>();

// Add strings to the set

set.add("CIS 315");

set.add("Java");

set.add("Programming");

set.add("John Doe"); // Replace "John Doe" with your own name

ArrayList<String> myArrayList =

setToList

(set);

// Display the hash code for each element in the array list

for (String str : myArrayList) {

System.out.println("The hash code for "" + str + "" = " + hashCodeForString(str));

}

}

// Return a hash code for a string

public static int hashCodeForString(String s) {

}

// Return an ArrayList from a set

public static <E> ArrayList<E> setToList(Set<E> s) {

}

}

========================================================================

The output of your code should look exactly like the following except the “John Doe”

name line:

The hash code for "CIS 315" = 1500788644

The hash code for "Java" = 2301506

The hash code for "John Doe" = -1367319387

The hash code for "Programming" = -1615787847

Note: Make sure to replace “John Doe” with your own name.

Submit the source code file - HashCodeForString.java

Explanation / Answer

import java.util.ArrayList;

import java.util.List;

import java.util.Set;

import java.util.HashSet;

public class HashCodeForString {

public static void main(String[] args) {

// Create a hash set

Set<String> set = new HashSet<>();

// Add strings to the set

set.add("CIS 315");

set.add("Java");

set.add("Programming");

set.add("John Doe"); // Replace "John Doe" with your own name

ArrayList<String> myArrayList =

setToList

(set);

// Display the hash code for each element in the array list

for (String str : myArrayList) {

System.out.println("The hash code for "" + str + "" = " + hashCodeForString(str));

}

}

// Return a hash code for a string

//h(s)=s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

public static int hashCodeForString(String s) {

int h=0;

for (int i = 0; i < s.length(); i++) {

h = 31 * h + s.charAt(i);

}

return h;

}

// Return an ArrayList from a set

public static <E> ArrayList<E> setToList(Set<E> s) {

ArrayList<E> list = new ArrayList<E>(s);

return list;

}

}

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