Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-24-2011, 11:33 PM   PM User | #1
wolveine903
New to the CF scene

 
Join Date: Mar 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
wolveine903 is an unknown quantity at this point
Recursion help

Got this problem I have been struggling on for hours that is extra credit for my class:

Write a recursive method that returns the currency format of the given integer value (val ). All inter-mediate operations must be carried out as integer arithmetic. The only time you are allowed to use string operations is to return the result. You may use the String.valueOf() or Long.toString() methods. Recall that 27341 % 1000 is 341 and 27341 / 1000 is 24.

public String formatAsCurrency(long val) { }

For this question, make sure your method works correctly for numbers that are multiple of 1000 or has consecutive zeros. Examples:

formatAsCurrency(5000481L) returns "5,000,481"
formatAsCurrency(434L) returns "434"
formatAsCurrency(76L) returns "76"
wolveine903 is offline   Reply With Quote
Old 03-25-2011, 12:33 AM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by wolveine903 View Post
Got this problem I have been struggling on for hours that is extra credit for my class:

Write a recursive method that returns the currency format of the given integer value (val ). All inter-mediate operations must be carried out as integer arithmetic. The only time you are allowed to use string operations is to return the result. You may use the String.valueOf() or Long.toString() methods. Recall that 27341 % 1000 is 341 and 27341 / 1000 is 24.

public String formatAsCurrency(long val) { }

For this question, make sure your method works correctly for numbers that are multiple of 1000 or has consecutive zeros. Examples:

formatAsCurrency(5000481L) returns "5,000,481"
formatAsCurrency(434L) returns "434"
formatAsCurrency(76L) returns "76"
if val < 1000 you need to return val as string else you need to contatenate the result of applying formatAsCurrency, which have as argument (val - (val % 1000))/1000, with a comma and the rest val % 1000.

best regards
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
wolveine903 (03-25-2011)
Old 03-25-2011, 01:29 AM   PM User | #3
wolveine903
New to the CF scene

 
Join Date: Mar 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
wolveine903 is an unknown quantity at this point
Thanks, it got me started but still stuck, hopefully i can get some credit. Its an upcoming topic in my class and is extra credit on homework since we haven't really learned it yet.
wolveine903 is offline   Reply With Quote
Old 03-25-2011, 01:39 AM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by wolveine903 View Post
Thanks, it got me started but still stuck, hopefully i can get some credit. Its an upcoming topic in my class and is extra credit on homework since we haven't really learned it yet.
ok, you say you are stuck, what is the problem? i hope not writing in java because my knowledge of java are very poor,

best regards
oesxyl is offline   Reply With Quote
Old 03-25-2011, 02:01 AM   PM User | #5
wolveine903
New to the CF scene

 
Join Date: Mar 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
wolveine903 is an unknown quantity at this point
I know some what how Recursive writing works for Java. I know that first is the base case, but then i get confused to how to set up the recursive call(s) after that.
wolveine903 is offline   Reply With Quote
Old 03-25-2011, 02:19 AM   PM User | #6
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by wolveine903 View Post
I know some what how Recursive writing works for Java. I know that first is the base case, but then i get confused to how to set up the recursive call(s) after that.
when you call formatAsCurrency(5000481L) and you implement what i said, first step will be to check if val is less then 1000L.
obvious in this case the test will fail then you will return the result of concatenation between the result of calling formatAsCurrency(5000L) with a comma, ",", and the rest 481L.
formatAsCurrency(5000L) will do same thing because 5000L > 1000L but this time will call formatAsCurrency(5L) wich will return '5' a string because is less then 1000L.
so after each return and concatenation will have 5,000,481

Edit:
Code:
return formatAsCurrency(5000481L)
              |
              \  return formatAsCurrency(5000L) + "," + "481";
                               |
                               \ return formatAsCurrency(5L) + "," + "000";
                                               |
                                               \ return "5";


best regards

Last edited by oesxyl; 03-25-2011 at 02:28 AM..
oesxyl is offline   Reply With Quote
Old 03-25-2011, 02:24 AM   PM User | #7
wolveine903
New to the CF scene

 
Join Date: Mar 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
wolveine903 is an unknown quantity at this point
Ok now can you write it out for me?
wolveine903 is offline   Reply With Quote
Old 03-25-2011, 02:32 AM   PM User | #8
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by wolveine903 View Post
Ok now can you write it out for me?
no,
three reasons:
1. you can't learn if i write
2. is a homework, read forum rules, i can help you but not this way
3. my knowledge of java are poor, as i said.

order doesn't matter,

best regards
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 07:13 AM.


Advertisement
Log in to turn off these ads.