• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Simple Dice Rolling Project

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Through the first six weeks of my class I've had no problems with simple single source file programs. This project for some reason is confusing me. Can anyone help me setup the structure of the project?
I don't understand why it has to be so many files? Also, can it be done without out if, else statements?


Define a class Die that represents one n-sided die. The default value for n is 6. You should be able to roll the die and discover the value of its upper face. Thus, give the class the method roll, which returns a random integer ranging from 1 to n, and the method getFaceValue, which returns the current face value of the die. Note that a call to roll will return the same value as a subsequent call to getFaceValue. Demonstrate your class by creating several Die objects and rolling them.
Often, games depend on the roll of two dice. Using your class Die, create a class TwoDice. An object of TwoDice represents two six-sided dice that are rolled together. Include at least the following methods: rollDice, getFaceValueDieOne, getFaceValueDieTwo, isMatchingPair, isSnakeEyes, toString, and getValueOfDice, which returns the sum of the two upper faces. Demonstrate your class by creating a TwoDice object and rolling it several times.

Die.java - a class of Die definition.
TwoDice.java - a class of TwoDice definition.
DieTest.java - a test program which test both objects of the class Die and TwoDice.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Jack Burton wrote: Can anyone help me setup the structure of the project?
I don't understand why it has to be so many files? Also, can it be done without out if, else statements?


What do you mean by "setup the structure"? Can you please elaborate?
Why do you want to avoid if/else statements? Is that a requirement?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jack Burton wrote:Through the first six weeks of my class I've had no problems with simple single source file programs. This project for some reason is confusing me. Can anyone help me setup the structure of the project?
I don't understand why it has to be so many files? Also, can it be done without out if, else statements?.



It has to be so many files because your intructor is trying to teach you good code reuse practices. Seperation of concerns is somethIng you must understand or you will have a very hard time with object oriented programming. Think of it like this, your game may start with only two dice, what if I want to add two more?
 
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you need an if-else to roll dice?

Just beware: never call the method throw() or throws().
 
Jack Burton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:Welcome to the Ranch.

Jack Burton wrote: Can anyone help me setup the structure of the project?
I don't understand why it has to be so many files? Also, can it be done without out if, else statements?


What do you mean by "setup the structure"? Can you please elaborate?
Why do you want to avoid if/else statements? Is that a requirement?


I'm having trouble figuring out what goes where into each file. It's hard for me to understand because up until this point everything has pretty much single source files with the exception of JPane program. Am I just coding the programs in three separate files and importing them to work with the main program? Not using if/else statements is not a requirement as far as I know, but I wasn't sure if I'd lose points for using them, because at this point we hadn't covered them. I did use them in another program earlier however and wasn't.
 
Campbell Ritchie
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would be surprised if you lose marks for using a construct you haven’t covered yet, unless you are specifically prohibited from using it.

Divide and rule.
Put a Die on the table in front of yourself. Look at it. What does it have? It has faces (usually six of them) each displaying a different number. What does it do? It might display a number, or it might be rolled to get a new number, which is random. Implement a class which models a Die, and try its methods, with something like this:You can add to that code and roll the Die several times, or create a d2 Die. Get that working. Then think about how to create a PairOfDice class.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:


You may lose marks if you don't follow the specification exactly
 
Campbell Ritchie
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did say, “something like.”
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you make the die.java correctly. To make twodie.java, all you need to do is extend it and then modify what's needed...
You can probably make a constructor to make all the possible sides. (probably array of size n) and then proceed to make other methods based on that....

Correct me if I'm wrong... I'm still relatively new.
 
Campbell Ritchie
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Disagree. A PairOfDice class does not extend Die at all. You cannot say, “A pair-of-dice IS A die,” but you can say, “A pair-of-dice HAS [TWO] dice.”
 
Tim Chaung
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, ok that makes sense... Should have known better too... I had to make a program that had cards, and a deck.... deck wasn't extended from card... deck was just an array of cards...

So in this case, twodice would simply contain two die. Literally.

again correct me if I'm wrong.
 
Campbell Ritchie
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct.
Also that means, you can get the Die class working before writing any of the TwoDice class.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Chaung wrote:You can probably make a constructor to make all the possible sides. (probably array of size n)


The other issue is that rolling two dice gives a bell-curve distribution of possible totals, whereas a single die will give you a flat distribution.
 
Campbell Ritchie
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it is actually a triangular distribution of probabilities.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
triangular/bell curve...either way, it's still not flat.
 
Master Rancher
Posts: 5161
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's flat... from a certain point of view.
 
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More generally:

Wikipedia wrote:A simple example of the central limit theorem is rolling a large number of identical, biased dice. The distribution of the sum (or average) of the rolled numbers will be well approximated by a normal distribution.

 
Campbell Ritchie
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It’s triangular for two dice and becomes more Gaussian with more dice.
 
Mike Simmons
Master Rancher
Posts: 5161
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes - for N dice, it's a "curve" (spline actually) with N sections, each of which is a polynomial of degree N-1. So for one die, it's one section of degree 0, flat. Two dice, it's two sections of degree 1, two lines forming a triangle. Three dice, it's three quadratic sections that match up to look more like a Gaussian. Four dice, it's four cubic sections that look even more like a Gaussian. And so on.

Well, actually it's a series of discrete points sampled from such smooth curvy constructs. But same basic idea.
 
Jack Burton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I would be surprised if you lose marks for using a construct you haven’t covered yet, unless you are specifically prohibited from using it.

Divide and rule.
Put a Die on the table in front of yourself. Look at it. What does it have? It has faces (usually six of them) each displaying a different number. What does it do? It might display a number, or it might be rolled to get a new number, which is random. Implement a class which models a Die, and try its methods, with something like this:You can add to that code and roll the Die several times, or create a d2 Die. Get that working. Then think about how to create a PairOfDice class.


Wouldn't it be the same for d1 and d2? Then I would just use import statements. What about using the random class?
 
Campbell Ritchie
Marshal
Posts: 80623
470
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jack Burton wrote: . . . Wouldn't it be the same for d1 and d2? Then I would just use import statements. What about using the random class?

  • 1: Yes, I hope it would be the same. But seeing it the same would give additional confirmation your class is working.
  • 2: What are you importing? If you have the Die class and PairOfDice in the same package, there is no need for an import.
  • 3: That is an implementation detail inside the Die class.
  • It is not for me to tell you how to implement a Die class. Random is one way to do it, but there are others. You do have to import Random.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic