Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2010, 02:44 AM
Member
 
Join Date: Feb 2010
Posts: 3
Rep Power: 0
kezkez is on a distinguished road
Default 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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-09-2010, 03:18 AM
Senior Member
 
Join Date: Nov 2009
Posts: 205
Rep Power: 1
collin389 is on a distinguished road
Default
You are printing out the object, not the hash. You need to make a method in HashIt called toString() and have it return the hash value.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-09-2010, 03:22 AM
Senior Member
 
Join Date: Nov 2009
Posts: 205
Rep Power: 1
collin389 is on a distinguished road
Default
Oh, just noticed your methods are static, therefore, there is no need to even create hashtest1 and hashtest2, just say:
System.out.println(HashIt.hashItR("abc"));
System.out.println(HashIt.hashItI("abc"));
In the main method. I got
44
44
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-09-2010, 06:22 AM
Member
 
Join Date: Feb 2010
Posts: 3
Rep Power: 0
kezkez is on a distinguished road
Default
that worked well, thanks.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
ascii, java, recursion

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
non-static method cannot be referenced from a static context. blackstormattack New To Java 5 05-07-2009 05:05 AM
make static ref to non-static method? McChill New To Java 7 02-23-2009 06:48 AM
static method sparks error on overriding non-static method MuslimCoder New To Java 1 02-10-2009 11:03 AM
Non-Static method in static context error wizmang New To Java 4 04-24-2008 09:51 AM
Error: Non-static method append(char) cannot be referenced from a static context paul Advanced Java 1 08-07-2007 06:05 AM


All times are GMT +2. The time now is 12:19 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org