The stages of this lab are:
For this part you will create a simple program called DiceGames.java which will ultimately contain two methods and a main() for testing it.
Taking what we know from class and from the text, create a class DiceGames containing a simple method rollOneDie() which takes no parameters and returns an integer in the range 1..6.
That is, the declaration for this method should look like
public static int rollOneDie()You, of course, have to provide the internals.
public static void main(String[] args) { int i; int roll; System.out.println("Testing rollOneDie()"); for ( i = 0; i < 10 ; i++ ) { roll = rollOneDie(); System.out.println("On roll " + i + " you rolled a " + roll); } }
public static int rollDice(int num)Your method should use the rollOneDie() method (code reuse is good, after all) to roll a die n times and sum up the results. Return the sum at the end.
Now that you have a parameter, though, life gets a little more complicated: Error checking comes into play. What do you do if you are called with 0? What about -3?
Make a decision about how you are going to handle such cases and handle them. A typical thing to do is to print an error message and return an impossible result (like -1, for example.)
Document your parameter requirements and responses as part of your method. For example:
public static int rollDice(int num){ // Returns the result of summing up num rolls of six-sided dice // if num <= 0, prints an error and returns -1 [... your code ...] }
This part (and the other "testing" part) of the lab are really redundant. You have to do this anyhow to ensure that your methods are working, don't you?
Now, go ahead and make the number of sides on each die a parameter so that you can change that one also. Allow the "user" to do this (of course, get the user input on both parameters, the number of sides on each die and the number of rolls they would like, you give them back the sum.)
When you are done, print your code and documentation, then show your instructor or lab assistant your program running. We'll check it off.
At this point, I expect that your commenting, your indentation, and your header for the classes are well written and follow the standards in the book and discussed in class. Things need to be neat and readable. We'll begin to take points for lack of good style and explanation from now on.