By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Rolling a 6 sided die

P: 1
the question is 1) build a Die class (6 sided). Then create a driver that tests your class. Start with one die in your driver then add a second die and notify the user and count when he or she rolls 7, 11, or a double. When the user is done rolling, output the results of the session.

Create a child class from the Die class called MultisidedDie. This die should allow the programmer using the class to decide how many sides the die has.


so far i have

Expand|Select|Wrap|Line Numbers
  1. import java.util.Random;
  2.  
  3. public class DiceTest
  4. {
  5.   int num;
  6.   int sides;
  7.  
  8.   Dice()
  9.   {
  10.     num = 1;
  11.     sides = 6;
  12.   }
  13.  
  14.   Dice(int number)
  15.   {
  16.     num = 1;
  17.     sides = 6;
  18.     if(number > 1)
  19.     {
  20.       num = number;
  21.     }
  22.   }
  23.  
  24.   Dice(int number, int num_sides)
  25.   {
  26.     num = 1;
  27.     sides = num_sides;
  28.     if(number > 1)
  29.     {
  30.       num = number;
  31.     }
  32.   }
  33.  
  34.   int roll()
  35.   {
  36.     java.util.Random r = new java.util.Random();
  37.     int sum = 0;
  38.     for(int i = 0; i < num; i++)
  39.     {
  40.       sum += r.nextInt(sides)+1;
  41.     }
  42.     return sum;
  43.   }
  44. }

however when i compile i get a "invalid method declaration; return type required" on line 8,14,24....any advice or help woud be greatly aprecciated!!!
Sep 25 '08 #1
Share this Question
Share on Google+
2 Replies


Expert 10K+
P: 11,448
You have named your class 'DiceTest'; your constructors must have that same name.

kind regards,

Jos
Sep 26 '08 #2

Ganon11
Expert 2.5K+
P: 3,652
Your code would also be more efficient if, instead of creating a new Random object every time you call roll(), you had a private Random object in the class dedicated to generating your random rolls.

In addition, what purpose does num serve? Should that be a class member or a function parameter?

JosAH's suggestion will get your code compiling and working, but consider mine if you'd like to make your code a bit 'cleaner'.
Sep 26 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.