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
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
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
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
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
}
}
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
