Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Hourglass using recursion

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hourglass using recursion

    I have to write a recursive program that makes an hourglass that suites the size of the user's input. For example, if the user types 4, this should print:

    * * * *
     * * *
      * *
       *
      * *
     * * *
    * * * *

    and so far I got this far:

    * * * *
    * * *
    * *
    *
    * *
    * * *
    * * * *

    so I just need to figure out how to get the spaces but I'm not sure how to do this. Thanks for the help.

    import java.util.InputMismatchException;
    import java.util.Scanner;
     
    public class Hourglass {
     
    	public static void main(String[] args) {
     
    		Scanner console = new Scanner(System.in);
    		System.out.println("Enter an integer greater than or equal to 1");
    		int n = 0;
    		try {
    			n = console.nextInt();
    		} catch (InputMismatchException e) {
    			e.getMessage();
    		}
    		if (n < 1) {
    			System.out.println("Invalid input");
    		} else {
    			System.out.println("");
    			hourglass(n, 0);
    			System.out.println("");
    		}
     
    	}
     
    	public static void hourglass(int n, int count) {
    		recursion(n, count);
    		if(n > 1) {
    			hourglass(n-1, count);
    			recursion(n, count);
    		}
    	}
     
    	public static void recursion(int n, int count) {
    		System.out.print("* ");
    		if (n > 1)
    			recursion(n - 1, count);
    		else
    			System.out.println();
    	}
    }
    Last edited by nWeid1; May 1st, 2012 at 04:40 PM.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Hourglass using recursion

    Hello nWeid1!
    I can't see the difference between the output as it should be and the one you actually get. What do you mean by "figure out how to get the spaces"?

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hourglass using recursion

    I just updated it. for some reason it won't display the spaces unless its

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Hourglass using recursion

    Where in your code do you have these spaces? I can only see two print statements in the recursion code -one for stars and the other for an empty line.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hourglass using recursion

    Well, I don't. I don't know where to implement the code to have them print out properly which is my problem.

  6. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Hourglass using recursion

    For this kind of problems you should write down the logic of your program and *test* it in a piece of paper before you implement code.
    Also, what is the variable count? Did you think of a way to use it to accomplish what you want?

  7. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hourglass using recursion

    Yeah I figured I could use count so that every line of stars there should add 1 space before the next line of stars. I also thought of creating a separate method just for the spaces but I don't know if that will work out.

  8. #8
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Hourglass using recursion

    Quote Originally Posted by nWeid1 View Post
    Yeah I figured I could use count so that every line of stars there should add 1 space before the next line of stars. I also thought of creating a separate method just for the spaces but I don't know if that will work out.
    You should try it and see what is happening. But again I suggest you writing down the logicof the program.
    Hint: not sure if you are allowed to use them, but for loops is a very common approach in such kind of problems.

  9. #9
    Junior Member
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hourglass using recursion

    Ok I'll try to write it down. And yeah my teacher said we can't use any for loops, hence the reason I'm using recursion in the first place

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Hourglass using recursion

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/59150-hourglass-using-recursion.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  11. #11
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Hourglass using recursion

    One more: Hourglass Using Recursion - Java | Dream.In.Code

    db

    edit The OP is presently throwing a temper tantrum on the thread linked by copeg
    Last edited by Darryl.Burke; May 1st, 2012 at 10:35 PM. Reason: Additional information

  12. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    copeg (May 1st, 2012)

Similar Threads

  1. Recursion Help
    By WeaKeN in forum Algorithms & Recursion
    Replies: 10
    Last Post: May 27th, 2011, 08:17 AM
  2. [SOLVED] Recursion help
    By Actinistia in forum Java Theory & Questions
    Replies: 3
    Last Post: March 21st, 2011, 12:26 PM
  3. Recursion
    By javapenguin in forum Algorithms & Recursion
    Replies: 12
    Last Post: October 18th, 2010, 03:42 PM
  4. Recursion Help
    By vmr in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 1st, 2010, 11:27 PM
  5. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 11:50 PM