Recursion help needed...

I'm trying to write a recursive program which makes a hill of astericks based on the input.

I.e. if the input was "3" the output would be something like:

*

**

***

***

**

*

So far, what I have in my code does half of it (the bottom half), but it also keeps going until it hits negative numbers so it throws the exception as well.

I'm also unsure on how to approach the top half. How do I work myself up from 1 asterick to "n" astericks without it going on for infinity. I tried doing printPattern(n+1) but that usually starts with say 3 astericks, then 4, 5, etc.

Thanks

ublicclass PatternProducer{

publicstaticvoid printRow (int stars){

if ( stars < 0)

thrownew IllegalArgumentException ("No Negative Numbers!");

if (stars < 1)

System.out.println();

else{

System.out.print("*");

printRow(stars-1);

}

}

publicstaticvoid printPattern (int n){

if ( n < 1)

System.out.println();

printRow(n);

printPattern(n-1);

}

publicstaticvoid main(String[] args){

printPattern(5);

}

}

[2098 byte] By [Hailfirea] at [2007-10-3 5:39:23]
# 1
Youre going about it the wrong way. Use for loops.
CaptainMorgan08a at 2007-7-14 23:47:04 > top of Java-index,Java Essentials,Java Programming...
# 2
I specifically can't use for/while loops.
Hailfirea at 2007-7-14 23:47:04 > top of Java-index,Java Essentials,Java Programming...
# 3
Nevermind.Message was edited by: CaptainMorgan08
CaptainMorgan08a at 2007-7-14 23:47:04 > top of Java-index,Java Essentials,Java Programming...
# 4

I would note two things:

1. Each number of asterisks needs to be printed exactly twice and in a specific order. The first printed must be the last printed. What does that tell you about where your recursive call might be located relative to the two print expressions?

2. The first thing printed should be the minimum number of asterisks. The last invocation of the method, which will be the very middle of the printing due to #1, needs to be the maximum number of asterisks.

Considering those two things should lead to an answer.

kablaira at 2007-7-14 23:47:04 > top of Java-index,Java Essentials,Java Programming...
# 5
There was a post about this recently, if you can find it. Anyway... you're checking to make sure that the number of stars being printed is not negative, but perhaps using negative numbers can be useful here...
Mr_Evila at 2007-7-14 23:47:04 > top of Java-index,Java Essentials,Java Programming...
# 6
Is #1 in regards to the printRow method?
Hailfirea at 2007-7-14 23:47:04 > top of Java-index,Java Essentials,Java Programming...
# 7
> Is #1 in regards to the printRow method?I don't know what you mean by that. However, I would say that if you're asking if by "print expression" or "print statement" or whatever I said if I mean printRow() then yes that would be an equivalent.
kablaira at 2007-7-14 23:47:04 > top of Java-index,Java Essentials,Java Programming...