• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Recursion Word Pyramid?  RSS feed

 
Aarden Axford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I was asked to create a word pyramid program using recursion. I understand the concept of recursion (i.e. a method calling itself with the problem it is solving getting simpler and simpler each time) but I am having issues writing the program.

I have figured out a way to do it without using recursion, and I am sort of getting stuck there... Here is my code:


So, if I input HORSE the output should be:
HORSE
ORS
R

Thanks for any help!
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem is within your loop, you are continuously updating word value. So each time substring is running on updated value not original value. I suppose this
much hint is enough. I have solved this problem using loop but can't share code before you tried first.

For recursion, share some of your code, i will give some suggestion.
 
Aarden Axford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem is within your loop, you are continuously updating word value. So each time substring is running on updated value not original value. I suppose this
much hint is enough. I have solved this problem using loop but can't share code before you tried first.


The program itself appears to be working fine... I am unsure of what you mean by this? Could you maybe explain some more?

My issue is specifically only with the recursion part of the program. As is, the code works as it is supposed to. I have used recursion (tentatively) before, but I am getting stuck on exactly HOW to get started using recursion in this program. Any hints?
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you start by going through the forum with “search” because there have been similar questions in the last week or so.
Three things about recursion:
  • 1: You end up calling the same method again.
  • 2: Each call must be slightly “smaller” than the previous one.
  • For words and their lengths, you would want something like this:-Now, you will have difficulty getting that to compile unless you can find a minus method in the String class. But you could start with the palindrome tester. Here is its logic:-So a word if a palindrome iff it is one letter or less long, OR: its first and last letters are the same AND its middle part is a palindrome. The code block should give you enough information to write a recursive method. You are allowed one return statement and you don't need any other statements.Line 3 is a declaration and initialisation, not a statement, and you can write a correct method without it. Try it with input like "Madam, I'm Adam!" and "A man, a plan, a canal. Panama!". But it is conventional to miss out non‑word‑characters and to be case‑insensitive, so use "madamimadam" or "amanaplanacanalpanama".
     
    Aarden Axford
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Here is my attempt at some recursive code. I am still trying to figure this out, and have gone back and read many posts on here. As of yet, the code is not working, but I am still fiddling with it. If anyone has any suggestions, please feel free.
     
    Campbell Ritchie
    Marshal
    Posts: 56545
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Start with the base case. Start by writing a method which writes one line with the fewest words possible. (Maybe 0 words or 0 letters.)

    That method has a loop in. Don't mix loops and recursion. You can actually prove that a particular loop and a certain recursion are equivalent to each other, but you want recursion so use recursion only.
     
    Campbell Ritchie
    Marshal
    Posts: 56545
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Go back to that loop and consider a word like Axford. It has six letters in. Work out what you are going to print for the different values for i in that loop, which I think will be 0…5. Just as well you aren't using that loop, isn't it.

    By the way: don't use /* ... */ for commenting‑out. Prefix every line with // because otherwise there is the risk of */ appearing somewhere in the commented‑out code and you will end up with code which won't compile.
     
    Aarden Axford
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Here is my updated code. It works perfectly - and recursively. Thank you for your help!

    I just have one more question (I know, *sigh*).

    My teacher asked me to print this into JOptionPane. How do I print out the entire finish pyramid into one variable? If I create a variable to hold the final returned String of the method, it just stores the final single letter. For example, if I typed in the word APPLE, the final printout through JOptionPane would be:
    P

    Any suggestions?
     
    Campbell Ritchie
    Marshal
    Posts: 56545
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Option Pane is a very old‑fashioned programming style, but it still works. You can read about it in the Java® Tutorials.

    You might do well, rather than returning the individual words, to put them in a StringBuilder. Either append the word to the end of it, or insert it at position 0.
    myStringBuilder.insert(0, System.lineSeparator()).insert(0, word); OR:
    myStringBuilder.append(word).append(System.lineSeparator());
    ...
    JOptionPane.showXYZDialog(null, myStringBuilder.toString());

    Using insert and append will give you pyramids narrowing upwards or narrowing downwards … but I can't be find the energy to work out which is which
     
    Aarden Axford
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    myStringBuilder.insert(0, System.lineSeparator()).insert(0, word); OR:
    myStringBuilder.append(word).append(System.lineSeparator());


    Lucky you were tired, cause' it gave me a chance to figure it out...
    Wow, thanks for the help! I'll mark this as resolved.
     
    Campbell Ritchie
    Marshal
    Posts: 56545
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!