I am working a Hash Table project. So I have an array which will hold the Strings. ArrayList<String> buckets = new ArrayList<>();
I want to create a method hash
but I am stuck on how to make that method takes in a String and loops over the individual characters in that string and sums their number values all together, and then return the integer sum.
The method hash
will use for the test below:
@Test
public void testToString() {
HashTable h= new HashTable(10);
org.junit.Assert.assertEquals( "Empty hash output" ,
"(0)\t\n"
+"(1)\t\n"
+"(2)\t\n"
+"(3)\t\n"
+"(4)\t\n"
+"(5)\t\n"
+"(6)\t\n"
+"(7)\t\n"
+"(8)\t\n"
+"(9)\t\n"
, h+""/*.toString()*/
);
}
@Test
public void hashFunction() {
HashTable h= new HashTable(10);
org.junit.Assert.assertEquals( "hash function test" ,
4
, h.hash("abc") % h.size()
);
org.junit.Assert.assertEquals( "hash function test backwards string" ,
4
, h.hash("cba") % h.size()
);
Thank you in advances!
Aucun commentaire:
Enregistrer un commentaire