Java class HashIt with a static recursive method and a static iterative method
hi, i could use some help with this problem: Write a Java class HashIt with a static recursive method and a static iterative (using while- or
for-loop) method to compute the hash function ∑ ASCII-value(ai) mod 50, i=1 to n. Be sure to include the main method and test cases. This is what i've got so far, but not getting the correct ascii values...
public class HashItTestCases
{
public static void main (String[] args)
{
HashIt hashtest1 = new HashIt ();
HashIt hashtest2 = new HashIt ();
String string = "abc";
hashtest1.hashItR(string);
hashtest2.hashItI(string);
System.out.println(hashtest1);
System.out.println(hashtest2);
}
}
//
class HashIt
{
//private static char firstChar;
//private static String restStr;
// static recursive method
public static int hashItR(String s)
{
char firstChar;
String restStr;
if( s.length()== 0)
return 0 ;
else
{ firstChar = s.charAt(0);
restStr = s.substring(1, s.length());
return ( ((int) firstChar + hashItR(restStr)) % 50 );
}
}
// tail recursive method
public static int hashItI(String s)
{
int asciiVal = 0;
int count = 0;
if (s.length() == 0)
return 0;
else
while (count < s.length())
{
asciiVal = ((int) s.charAt(count)) + asciiVal;
count++;
}
return asciiVal % 50;
}
}
output:
HashIt@1fb8ee3
HashIt@61de33