hey,I have been working on a program, or at least trying to get started, but i am having some difficulty.

On step 5 i am unsure on weather i would need to use a for loop of a if statement. and how would i do this.

Step 6 : is it a simple return variable;

Below are the steps which are guiding me:

1. Create a new class called Hourglass.

2. Create an attribute to store the height, and one to store the amount of sand in the bottom half of the hourglass.

3. Write the general constructor so that it creates a Hourglass instance of the specified height. When a new hourglass is created, all the sand should be in the top half. The amount of sand in an hourglass is exactly enough to fill one half of the hourglass.

4. Write the default constructor so that it creates a Hourglass instance of height 3.

5. 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.

6. 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 hh grains (=1+3+5+...+(2h-1)). 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.

7. Write a class method starMinusLine which takes two arguments specifying a number of stars and minuses respectively and returns a String with the stars centered between the minuses. Example: starMinusLine(3,4) should return "--***--".

8. Write a class method starMinusLine which takes three arguments specifying a number of stars and minuses as well as a true/false value and returns a String with the stars centered between the minuses, or, if the boolean is false, the minuses should be centered between the stars. Example: starMinusLine(3,4,true) should return "--*(3 stars)--". starMinusLine(3,4,false) should return "---**". Thus, the first argument is always the number of symbols in the centre. Hint: determine at the start of the method what character will be in the center and what will be at the sides, and put these into 1-character Strings containing "*" and "-" respectively.

9. Write a method getExtraGrains that calculates how much sand has fallen since the last time that the heap of sand was a triangle, and how much of that sand has ended up at or above the line that is the argument of this method. Hint: You know the height and the total amount of grains in the bottom half of the hourglass. (Note that the height of the heap is the same as the height of the triangle at the last time that the heap was exactly a triangle.) In every line below the current line (i.e., a line with a higher line number), two extra grains can be placed, one left and one right.

10. Write a method getSandInLine that calculates how much sand there is on a particular POSITIVE line of the picture of the hourglass (that is, the bottom half). The javadoc notes above show how the lines are numbered. Hints: Above the heap, there is no sand, so check for this first (how?). In the heap, you can first calculate the width of the triangle at the current line (The triangle that formed the heap the last time that the number of grains in the bottom half of the hourglass was a square.) Then you need to check if there are still grains to the left and to the right at this height. How many grains are not in the triangle? See the method getExtraGrains(int).

11. Write a method toString that produces a String containing a picture of the current state of the HourGlass according to the description given in the javadoc. Each line should be drawn using the starMinusLine and getSandInLine methods. Use '.' for the background (outside the hourglass). Hint: you start a new line inside a String with the code \n. If you can print the positive lines of the hourglass (the bottom half) correctly, then you only need to make a small change for the top half.

The Hourglass should look like this

-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:
-
.***.
....
..-..
.---.
--
--

This is all i have

``````class Hourglass {
//step 2 Create an attribute to store the height, and one to store the amount of sand in the bottom half of the hourglass.
int height;
int bottomHalf;

public Hourglass (int h) {
height =h;
}

// step 4 Write the default constructor so that it creates a Hourglass instance of height 3.
public Hourglass (){
height=3;
}

/* step 5Write 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(){

}

/* step 6Write 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() {

}
``````

You use a loop when you want to do something a number of times. You use an `if` if you want to do something only under some circumstances. Reading the description of step 5, there's nothing repetitive there, but there is something you have to do only when all the sand is at the bottom.

Step 6 describes the formaula you need to calculate the return value, so no, it's not just returning an existing variable.

okay, so for step 5, i woulf first need to check if the bottom half is full.
How would i get a grain to drop.

You have a variable that holds the amount of sand in the bottom half. I guess that would be measured in grains? What happens to that variable when 1 grain drops to the bottom? (If the answer seems too easy don't worry. It really is that easy!)

So i simply add one to the bottom half.
And to check it is full i use an IF statement to check if it is = to 9.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.