Ads 468x60px

freak2code is the blog about latest geek news,software reviews ,trends in technology and more informative stuff......

Monday, 13 August 2012

Java Programming Fix - using recursion to print integer in reverse order vertically?


Java Programming Fix - using recursion to print integer in reverse order vertically?

Hey so here's the program I have so far - instead of printing on one line though, I need to print the integers vertically - any ideas? I included the full prompt if that helps anyone!

/* Write a short recursive program (name the class that contains the main method Activity1) that calls a
recursive method (named writeVertical), which in turn takes one (nonnegative) int argument from the
user (prompted at run-time) and writes that int to the screen with the digits going down the screen one per
line from least significant to most significant. Your recursive method should not return any values.
Sample Input to the recursive method:
writeVertical(1234);
Sample Output (from the program):
4
3
2
1
Your implementation should hard-code at least three different calls to the writeVertical method using
different values of differing lengths. One of the calls must be of length (or size) 1 (that is, one digit long).
The other two calls can be of any other lengths, so long as they are of differing lengths. */

Code :

public class Activity1
{
public static void main(String[]args)
{ 
int intValue=3052;
 
String stringValue=Integer.toString(intValue);
 
writeVertical input1 =new writeVertical();
input1.reverseResult(stringValue,0);
}
}
 
class writeVertical {
public int reverseResult(String b,int c) {
if(c==b.length()-1) {
System.out.print(b.charAt(c));
return 0;
}
else {
int temp=c;
c=c+1;
reverseResult(b,c);
System.out.print(b.charAt(temp));
return 0;
}
}
}

0 comments:

Post a Comment

Recent Posts