Results 1 to 8 of 8
  1. #1
    PrincessKenny is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Recursion help please

    I am working on program and have been struggling to get around step 5 and 6 given below.


    Recursion help please-steps.png

    I have got on with the first couple of points. I have no little idea on where to begin with steps 5 and 6.

    Java Code:
    class Hourglass {
    	
    	int height;
    	int bottomHalf;
    
    
    	public Hourglass (int h) {
    		height =h;
    	}
    	
    	
    	public Hourglass (){
    		height=3;
    	}
    		
    	/*Write a method dropGrain that simulates one grain of sand falling into the bottom
    	half of the Hourglass. If all the sand is already at the bottom before a 
    	grain is dropped, this method should cause the hourglass to be flipped, 
    	meaning that all the sand will be in the top again.
    	Then, one grain of sand should fall. */	
    	//Hint: this method can be quite short. All you need to do is update one attribute.
    	
            public void dropGrain(){
    		
    	}
    	
    	/*Write a method getHeapHeight() which returns the height of the heap of sand in 
    	the bottom of the hourglass.
    	Hint: a triangle of height h contains h*h grains (=1+3+5+...+h). 
    	So determining the height when the amount of sand in the
    	bottom half is a square (1,4,9,16,...) is easy.
    	Think about what happens if the amount of sand is not exactly a square.*/
    	
    	public int getHeapHeight() {
    		
    	}
    Last edited by PrincessKenny; 11-18-2014 at 03:34 PM. Reason: typo, with the steps

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Recursion help please

    Can you copy the program's requirements into the code as comments so they are readable and useful for help in writing the code?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Recursion help please

    Where does the recursion show up?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Recursion help please

    Well, you've done step 4 already.

    What exactly are you stuck on for step 5?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    PrincessKenny is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Re: Recursion help please

    Quote Originally Posted by JosAH View Post
    Where does the recursion show up?

    kind regards,

    Jos
    I assumed it would be when dropping a single grain. I am not to sure to be honest.

  6. #6
    PrincessKenny is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Re: Recursion help please

    Well, you've done step 4 already.

    What exactly are you stuck on for step 5?

    I re-read my original post for the third time and saw my mistake, it was meant to be step 5 and 6. I will change this in the post.

    Step 4 : i am not sure how to get a single grain of sand to fall. Would a for loop work for this?

    Step 5 : working out the height for the bottom half.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Recursion help please

    Quote Originally Posted by PrincessKenny View Post
    I re-read my original post for the third time and saw my mistake, it was meant to be step 5 and 6. I will change this in the post.

    Step 4 : i am not sure how to get a single grain of sand to fall. Would a for loop work for this?

    Step 5 : working out the height for the bottom half.
    Well, the step 5 gives a hint.
    You only need to change a single value.
    But first you need to check that value to see if all the grains have fallen.
    I have no idea how you're supposed to know how much sand there is in there as I don't see a 'grainCount' variable anywhere, or a way to calculate it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    PrincessKenny is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Re: Recursion help please

    Quote Originally Posted by Tolls View Post
    Well, the step 5 gives a hint.
    You only need to change a single value.
    But first you need to check that value to see if all the grains have fallen.
    I have no idea how you're supposed to know how much sand there is in there as I don't see a 'grainCount' variable anywhere, or a way to calculate it.

    in step two i was given this;:
    Create an attribute to store the height, and one to store the amount of sand in the bottom half of the hourglass.

    i have called this other variable bottomHalf.

    I looked through the page again and found a link to another page at the bottom, which shows this;

    The Hourglass class simulates an hourglass. The default hourglass has size 3, meaning that both the top and bottom half have height 3. It is shown below together with the line numbering (which will be useful in your program, but should not be shown in your output). Stars represent sand, minuses represent empty space.
    -3 *****
    -2 .***.
    -1 ..*.. An hourglass of size 3 when it is just created.
    1 ..-..
    2 .---.
    3 -----

    As sand starts to fall into the bottom half, the heap of sand at the bottom always consists of a triangle with some extra grains of sand at the sides. We assume that grains fall to the left and to the right of the heap in an alternating fashion, beginning with the left. At the top of the hourglass, the sand will disappear from the center first; there will be an empty space which exactly mirrors the heap at the bottom.
    Example: after the first grain has dropped, the hourglass looks as follows.

    After one grain has dropped: **-**
    .***.
    ..*..
    ..-..
    .---.
    --*--

    The second grain will fall to the left of the first one, and the third will fall to the right. After the fourth grain, the hourglass looks like this:
    After four grains have dropped:
    *---*
    .*-*.
    ..*..
    ..-..
    .-*-.
    -***-

    Second Example: If the hourglass currently looks like this:

    Current hourglass:
    *-----*
    .*--**.
    ..***..
    ...*...
    ...-...
    ..---..
    .-**--.
    -*****-
    then the next five grains will fall in the locations given by the numbers 1-5 below (see the bottom half of the hourglass), and they will disappear from those same locations at the top:
    3-----4
    .5--1*.
    ..*2*..
    ...*...
    ...-...
    ..-2-..
    .5**1-.
    3*****4

Similar Threads

  1. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:26 PM
  2. Recursion
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 04-09-2009, 03:26 AM
  3. Recursion
    By jachandru in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 01:52 PM
  4. Recursion help
    By rjg_2186 in forum New To Java
    Replies: 1
    Last Post: 01-02-2009, 09:03 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •