• Post Reply Bookmark Topic Watch Topic
  • New Topic

Assignment Question  RSS feed

 
Mario Jorge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi lads - need help with a question if possible - the exact question is as follows:

a) Using recursion, compute the sum of all numeric values of each character in a string that are in the range a-z, where a=1, b=2, … z=26. Any other character has no value. The method is case-sensitive, so uppercase letters have no value. Call the recursive method from main().

b) Now add a class called StringConverter in its own .java file, which has a recursive static method like the one just created but has an extra parameter called caseSensitive which if set to false will treat uppercase letters as lowercase letters. Call this new recursive method statically from main(). For example if this is called on the string abCde with caseSensitive set to false, it will return 15. With caseSensitive set to true it will return 12.


I have code as follows below - when i run nothing happens... I have 1 hour to submit any help would be greatly appreciated...



&



thanks,
Mario
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mario Jorge wrote:
I have code as follows below - when i run nothing happens.


Perhaps, it would be a good idea to elaborate how you are compiling, running, etc. -- as "nothing happens" doesn't really tell us much.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mario Jorge wrote:
a) Using recursion, compute the sum of all numeric values of each character in a string that are in the range a-z, where a=1, b=2, … z=26. Any other character has no value. The method is case-sensitive, so uppercase letters have no value. Call the recursive method from main().

b) Now add a class called StringConverter in its own .java file, which has a recursive static method like the one just created but has an extra parameter called caseSensitive which if set to false will treat uppercase letters as lowercase letters. Call this new recursive method statically from main(). For example if this is called on the string abCde with caseSensitive set to false, it will return 15. With caseSensitive set to true it will return 12.



You assignment asked for recursive solutions.... and there is nothing recursive about your solutions.

Henry
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if you are using a for loop, don't create a variable to use for counting, e.g. len in line 6. Use word.length() directly there.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And add this line to the 'main' method,

just before the line

Then something will happen, but as Henry wrote, it is not recursive.
A recursive way would be that the requested sum of a string is the value of the first character plus the sum of the rest of the string.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use ints as flags; good grief, you are not writing C. Don't use ==0 like that; you shou‍ld use the bang sign ! instead. But use a boolean. And don't use underscores _ like that: not case_sensitive but caseSensitive.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!