• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner sand timer program help  RSS feed

 
bob kenny
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 h*h grains (=1+3+5+...+(2*h-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


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 so

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You have a lot of information and you haven't done much. I'd like to see you ShowSomeEffort in getting the project done and then ask a specific question. JavaRanch is NotACodeMill.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

The instructions you have been given are very precise. The terms general constructor and default constructor are however incorrect. You seem to have achieved that part, anyway.
How far have you got with the dropGrain method? Do you need a count of grains? Write down on paper what you think that method means. Then you can work out how to turn it into code.

Or better, write on this screen what you think No 5 means. Then we can all help.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob kenny wrote: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.

Neither I suspect, since the method is only required to "drop" a grain of sand.

Step 6 : is it a simple return variable;

Again: no. And I actually think this method may be a bit trickier than some of the others; so maybe it's better to leave it until later.

I think what's worrying you is that you think you need to know everything before you start ... and you DON'T.

It's actually perfectly possible to write a class (especially when you've been given all the basic instructions on what it needs) without knowing how you're going to to implement ANY of its methods; it just takes a bit of technique. And this page shows you one way to do it.

That's not to say that it's not a good idea to think about how you're going to code stuff - so keep a notebook handy - just don't panic if you don't get it all right at the beginning.

Hope it helps.

Winston
 
bob kenny
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies and tips on how to tackle programming as this .
I believe step 5 requires to check if the bottom half is full. so if its the bottom half should be cleared and the grains should be moved to the top half.
so it would need a counter for the grains in the top and bottom half.
Once the grains are in the top, it shall then drop a single grain down to the bottom.

that is my understanding of step 5.

I am struggling to get started with it. so do i need a variable which hold the amount of grains in each half.?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob kenny wrote: so do i need a variable which hold the amount of grains in each half.?

see your instructions:
2. Create an attribute to store the height, and one to store the amount of sand in the bottom half of the hourglass.
 
bob kenny
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i use the bottomhalf variable to check if it is full, check to see if it is 9 or less. if it is less than 9, then add 1 grain.

if it is full then set it 0 and then add 1.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does 9 come from?

What happens when you turn an hourglass over? Write that down preferably with diagram, then you can work out what you need to do when your method turns it.
 
bob kenny
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i assumed the stars represent the grains in the hourglass.

When the hour glass is turned. the grains begin to fall the other way.
how would i determine the amount of grains so i know the half is full.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are making your questions too complicated. Try these:-
How many grains are there in your hourglass?
When it has run through how many are there in the top half and how many in the bottom half?
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your diagrams with stars look better with code tags, which I have added.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
again, check your specs:

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.

at this point, we're not thinking of a traditionally shaped hourglass, but a VERY tall, VERY skinny one. Each grain of sand is stacked directly on top of the next. so if the hourglass is 1000 units tall, it has 500 grains of sand in it. If it is 120 units tall, it holds 60 grains of sand. etc.
 
bob kenny
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You are making your questions too complicated. Try these:-
How many grains are there in your hourglass?
When it has run through how many are there in the top half and how many in the bottom half?



9 grains in each half. in step 6 its says a triangle of height h contains h*h grains. As the height is 3, would there not be 3 x 3 grains.

when it has run through there will be no grains in the top half
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob kenny wrote:
9 grains in each half. in step 6 its says a triangle of height h contains h*h grains. As the height is 3, would there not be 3 x 3 grains.

when it has run through there will be no grains in the top half

Hoisted on my own petard. I should have read more of the specs.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meng Xicheng, welcome to the Ranch

If you look here, you will read this:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
If you give the OP an answer like that, you will deprive him of the opportunity to learn it himself. If it is assessed work and he uses your solution, it will be marked as plagiarised work and will attract a final mark of 0. Please don't be annoyed with me but I have pulled rank and deleted your posts. The forum software records them however.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob kenny wrote: . . .
9 grains in each half. in step 6 its says a triangle of height h contains h*h grains. As the height is 3, would there not be 3 x 3 grains.

when it has run through there will be no grains in the top half
You have two contradictory assertions there. Are you sure about 9 grains in each half?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!