473,395 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Rolling a 6 sided die

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
2 6472
JosAH
11,448 Expert 8TB
You have named your class 'DiceTest'; your constructors must have that same name.

kind regards,

Jos
Sep 26 '08 #2
Ganon11
3,652 Expert 2GB
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

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

Similar topics

1
by: lawentzel | last post by:
I am new to PHP and am an avid RPG gamer. I am looking to use PhpMyChat to be able to game with friends all over the US. With that having been said, PhpMyChat seems to offer a lot of what I am...
5
by: traga_2_whiskys | last post by:
Weel i want do make a box with text rolling down to up, i've make-it wiht <marquee> in html, byt the text isn't always rolling. can anibody help me?
1
by: Ganesh | last post by:
Where to find a right sided slide-in JavaScript? My idea is something like this http://www.dynamicdrive.com/dynamicindex1/davidmenu.htm But I need it right-sided, not left-sided... tia
1
by: agb | last post by:
Hi I need an example to understand the client-sided control For example ; I need a (I think a javascript code) in a ascx file Then in a web form I want to write the...
101
by: Elijah Cardon | last post by:
Let's say I have m dice having n sides, such that n^m is not going to bust int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1, 2}. What is a good way to represent this in c so that...
17
by: Jose Durazo | last post by:
Hello, I'm doing an exercise to simulate rolling a pair of dice 36,000 times, then count and display how many times the simulation rolls each possible sum. For some reason each time I run my...
3
by: farhadtarapore | last post by:
I have a very large C++ application that has been converted into a windows service. This application writes a lot of statements to the console i.e. cout and cerr. I have used std::ofstream...
4
by: vegtard | last post by:
simple question. is it possible to write a script that rolls a six sided dice? if this is possible, is it possible to tell it to roll the six sided dice 4 times and ignore the dice that rolled...
2
by: brent78 | last post by:
I have some buttons on my form to preview my report or to just print it directly. Is it possible to have it print double sided on the fly? Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.