aspose file tools*
The moose likes Beginning Java and the fly likes Actually Rolling the dice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Actually Rolling the dice" Watch "Actually Rolling the dice" New topic
Author

Actually Rolling the dice

D Doemer
Ranch Hand

Joined: Apr 18, 2014
Posts: 36
Could someone PLEASE tell me what code I should add to my program in order to get these dice to roll. The program, according to my teacher, is CORRECT so far, so I'm not going to change anything. I just need to know how to roll the 2 dice I added in. PLEASE no hint-dropping or "what if this..."'s. I just want to get it done. No math.Random.


The direction is: Roll each of the Dice by invoking the roll method on each Dice in the array."



My code:







Thank you.
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 1021
    
  16

This code prints each of your dices. How do you roll each dice? Your Dice instance in a loop is named diceArray1. Your method is named roll(). How do you call a method roll() on an instance diceArray1?


Formely Pawel Pawlowicz
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1823
    
    7

Well, if the objective is to get each of the Dice to roll, you're pretty much done as it stands, because the constructor used to create the Dice invokes the roll() method. If you want to invoke it a second time before printing the Dice's toString() result, simply add another roll() invocation in the body of the last for-loop.

Edit: woah, how's that for timing


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
D Doemer
Ranch Hand

Joined: Apr 18, 2014
Posts: 36
I thought that they were already rolling -- I didn't understand why my teacher was saying I just needed to add the last two for loops to finish it. I'm getting an output that says :

run:
Dice with3 sides is showing3
Dice with4 sides is showing4
BUILD SUCCESSFUL (total time: 1 second)


Of course the "sides is showing" part changes with each compilation due to the random factor.




For the final bullet point, the teacher asks "Keep track of the totals sum of all the dice in the array. Be sure to roll the dice array at least 10000 times." Could this possibly be where he's talking about making 2 more for loops? He is so vague, I am just not clear on what he wants.

Thank you both so much.
Mike. J. Thompson
Ranch Hand

Joined: Apr 17, 2014
Posts: 293
    
    5
You're going to have to write a loop that runs 10000 times, and for each iteration of the loop you have to roll each of your dice.

You already know how to loop over your dice and print them out. To roll them you need to change that to call the roll method instead.

Will also need a variable to track the sum of each dice roll.
D Doemer
Ranch Hand

Joined: Apr 18, 2014
Posts: 36
Thanks. I've tried for hours to get this thing to work. It's frustrating to the point of dropping it altogether. There are no examples to follow or anything. This should NOT be this hard.
Rico Felix
Ranch Hand

Joined: Mar 08, 2014
Posts: 304
    
    4

Using the code you posted all you have to do is this;

Seeing that the constructor rolls the dice upon object creation, that means that the two die will have values already generated which you must add to the total sum after the for loop that initializes them



Next you roll each dice 10_000 times and add the generated value to the total sum



All that's left to do after this point is to print the output to the user



I hope this can steer you in the right direction showing you how you break the problems down logically
D Doemer
Ranch Hand

Joined: Apr 18, 2014
Posts: 36
Rico --

THANK YOU so very, very much. Everything makes much more sense for me when it's in black and white. I can't tell you how much I appreciate your help.



Diana
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

Diana Oswald wrote:I just need to know how to roll the 2 dice I added in. PLEASE no hint-dropping or "what if this..."'s. I just want to get it done.

Sounds like you're telling us how to do our job, and simply want to force what you've written to work, even if it's flawed; and I'm afraid that's not a very good way to proceed.

So I'm afraid I am going to give you some hints:
1. 'Dice' is plural, so what your Dice class is mimicking is actually a Die.
2. Since the requirements ask you to roll each dice (??) in your array. I suspect I would have created a Dice or DiceGroup class with the same API as a Die (singular) and put my "Dice array" in it. Then, my main() method would only have to "roll" my DiceGroup 10,000 times.
3. getNumberShowing() is arguably redundant, since your roll() method already returns that value. Now it's possible that you were already given that method, but in general, classes should only contain the methods required to do their job.

However, very glad to hear that you got your program working.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 1021
    
  16

Winston Gutkowski wrote:'Dice' is plural

Omigod! I always thought it is one dice and two dices... It's always good to learn something ;).

Winston Gutkowski wrote:3. getNumberShowing() is arguably redundant, since your roll() method already returns that value. Now it's possible that you were already given that method, but in general, classes should only contain the methods required to do their job.

But they don't do the same. Method roll() generates new number and returns it. Method getNumberShowing() just returns the value previously rolled.
By that logic you could argue that method peek() in Stack is redundant because you can always pop() and then push().
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
No it is one Die and several Dice. Most poeple in this country don't know that either, so you are in good company.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

Pawel Pawlowicz wrote:But they don't do the same. Method roll() generates new number and returns it. Method getNumberShowing() just returns the value previously rolled.
By that logic you could argue that method peek() in Stack is redundant because you can always pop() and then push().

Not true.

First: A Stack is not a Die.

Second: While what you say is true, not having a peek() method forces a client to perform a redundant write operation in order to perform a read, which has implications if you need multi-threading.

Third: I did say "arguably", but I stand by the statement. If you already have a method that returns the "last rolled value", why would you need to read it again? The nature of a die is to be rolled, not to provide the same information over and over.

You could, I suppose, argue that the business of rolling is separate from that of checking the value, but in that case, why not just have roll() return void? It's certainly a possible way to go, but I tend to prefer imperative logic to passive - tell, don't ask.

As with almost everything in programming, there are many ways to do things, but I'd say that providing a minimal API (especially when it comes to public methods) is generally good advice.

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

Winston Gutkowski wrote:...in that case, why not just have roll() return void?

Or actually (and possibly better), the die.

That way, you could roll and check the value with:
roll().getNumberShowing()
but I'd still say it's overkill.

I do quite like "fluent" APIs though.

Winston
Aaron Shawlington
Ranch Hand

Joined: Oct 01, 2013
Posts: 31
'The dice is cast' doesn't have the same ring to it.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1823
    
    7

Dice actually is an accepted singular form, as far as I know, and Oxford Dictionaries agrees with me, so there

Historically, dice is the plural of die, but in modern standard English dice is both the singular and the plural: throw the dice could mean a reference to either one or more than one dice.

And as long as we're on the subject of singular vs. plural, I can't stand it when people say or write "data are", even though it is correct. Data is people, it just is...
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

Jelle Klap wrote:Dice actually is an accepted singular form, as far as I know, and Oxford Dictionaries agrees with me, so there

Actually, the site also states that it is not the Oxford English Dictionary, which is at www.oed.com. Unfortunately, I'm not a subscriber, so can't say whether it backs you up or not (I'd damn well hope not).

However, Merriam-Webster (American) not only agrees with you, it suggests that 'dices' is a reasonable plural.

What's next? Sheeps?

As Bernard-Shaw said: Two continents divided by a common tongue.

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

Jelle Klap wrote:And as long as we're on the subject of singular vs. plural, I can't stand it when people say or write "data are", even though it is correct. Data is people, it just is...

Have to admit I'm with you there; but simply because the singular (datum) is hardly ever used these days. It's a bit like 'an historian' (a relic of the old French silent 'h') and Inspectors-general - correct, but kinda nitpicky.

Of course, I'm arguing against myself here; but the thread was about throwing dice, not a die.

Winston
D Doemer
Ranch Hand

Joined: Apr 18, 2014
Posts: 36
Guys -- I know and realize what all of you are saying. I've spent 5 weeks scouring the internet for examples of what we've been doing in class in those 5 weeks, and the very fact that A) I can't find much remotely related to these assignments, and B) What I do find states some of the same things you've pointed out, it leads me to believe the flaws are within the course itself, and potentially the way it's instructed. That's all I'll say at the risk of someone from my school seeing this and figuring out it's me ;)

I'm sorry -- didn't mean to tell you how to do your job. It's just that the instructor likes what I had, so whatever he says I guess goes. But yes, I'm well aware 1 "dice" is a "die".


Thanks for the help!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

Diana Oswald wrote:I'm sorry -- didn't mean to tell you how to do your job. It's just that the instructor likes what I had, so whatever he says I guess goes. But yes, I'm well aware 1 "dice" is a "die".

Thank God. I thought I was on the "terminally anal retentive list".

And the fact is that you were pretty close to a solution (and maybe my reaction was a bit OTT). However our site isn't just here to provide answers, it's also to help you to learn; and when you run into trouble, the best thing to do is often to StopCoding (←click), because you may be going down a blind alley.
Believe me, we've all been there, and it can be very difficult to trash a program when you've got a lot of time and effort invested in it; but sometimes it's the best way (and it's also best to do while you're learning).

Anyway, it seems you now have a solution, so WELL DONE. Maybe the latter part of this thread (dictionary stuff excluded) is something you can think about next time you run into something similar.

Winston
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
Winston Gutkowski wrote:Believe me, we've all been there, and it can be very difficult to trash a program when you've got a lot of time and effort invested in it; but sometimes it's the best way (and it's also best to do while you're learning).

Many moons ago I was a young design engineer. This was in the days when engineering drawings were all done with ink or pencil on A1/A0 sized polyester film and my boss at the time would sometimes come along look at my work and suggest the equivalent of stopCoding. On a few occasions, having spent several days on the particular drawing, I would press on rubbing bits out and adding new bits. He would then come along with a big soft pencil and draw a heavy line diagonally through my work and say now you've got to start again so I suggest you think about what you need to do first. It was gutting to see my work ruined but it did eventually (I was a slow learner) teach me the value of starting again. In fact even later years whenever I'd got to stage where I realised my boss would have been drawing a line through my work I would do it myself just to remove any temptation to continue and force a restart.

In software development when I get to this stage I copy the Java file(s) to a backup device (just in case there is something in it I need to refer to later), delete the originals and start again from scratch.

Jelle Klap wrote:Dice actually is an accepted singular form,

Whilst I grudgingly accept that 'dice' is so often used to mean the singular 'die' it probably has become valid these days, I agree with Winston that 'Dices' is absolutely not the plural.
D Doemer
Ranch Hand

Joined: Apr 18, 2014
Posts: 36
Thanks for the link, Winston. I'll definitely keep that in mind for the future. This class went from 0-100 in a week, so at this point it's sink or swim. I guess I'll take a course elsewhere in the future that actually explains things correctly.
D Doemer
Ranch Hand

Joined: Apr 18, 2014
Posts: 36
Quick question everyone -- am I allowed to keep a running dialogue on this thread in regards to my classwork? I'll probably need more help soon if anyone is willing -- but I totally understand if I get on anyone's nerves. It probably sucks being the one to help the newbie ;)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

D Doemer wrote:Quick question everyone -- am I allowed to keep a running dialogue on this thread in regards to my classwork? I'll probably need more help soon if anyone is willing -- but I totally understand if I get on anyone's nerves.

I don't think there are any hard and fast rules about it, but the general rule is UseOneThreadPerQuestion (←click).

On the other hand, if it's simply a follow-on from what we've been discussing...

Basically: Don't worry about it too much. If one of the moderators here thinks it's too much, they'll split it into a separate topic and let you know.

It probably sucks being the one to help the newbie ;)

Not at all. That's why we're here.

And good luck.

Winston
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 687
    
  11
If you have a question, just open a new topic.
Otherwise, this topic will grow and grow, and people would have to scroll
miles before reaching your latest question.

And don't feel ashamed to ask whatever you want. I just had the pleasure
to put a probably stupid question myself, but hey: if these guys and galls out here
can help you, well, what more could you want?

Greetz,
Piet
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

Tony Docherty wrote:Many moons ago I was a young design engineer. This was in the days when engineering drawings were all done with ink or pencil on A1/A0 sized polyester film...

Engineering drawings? We didn't 'ave nowt like that...fancy polyester film...it were cups of cold coffee and coding sheets when I were a lad. Not to mention gettin' thrashed to sleep every night...memories.

Winston
D Doemer
Ranch Hand

Joined: Apr 18, 2014
Posts: 36
Thanks everyone -- your sense of humor and willingness to help is very reassuring, and much appreciated. I posted the first part of the new assignment here: http://www.coderanch.com/t/633479/java/java/Week-assignment-Polymorphic-containers-text#2901307. Just trying to make sure that I follow the rules.

Thanks again.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
Winston Gutkowski wrote:
Tony Docherty wrote:Many moons ago I was a young design engineer. This was in the days when engineering drawings were all done with ink or pencil on A1/A0 sized polyester film...

Engineering drawings? We didn't 'ave nowt like that...fancy polyester film...it were cups of cold coffee and coding sheets when I were a lad. Not to mention gettin' thrashed to sleep every night...memories.

Winston

Don't get me started on all the times I had to lick road clean wit tongue
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Actually Rolling the dice