1. Member
Join Date
Nov 2014
Posts
4
Rep Power
0

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

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. ## 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?

3. ## Re: Recursion help please

Where does the recursion show up?

kind regards,

Jos

4. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
26

Well, you've done step 4 already.

What exactly are you stuck on for step 5?

5. Member
Join Date
Nov 2014
Posts
4
Rep Power
0

Originally Posted by JosAH
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. Member
Join Date
Nov 2014
Posts
4
Rep Power
0

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. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
26

Originally Posted by PrincessKenny
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.

8. Member
Join Date
Nov 2014
Posts
4
Rep Power
0

Originally Posted by Tolls
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