473,503 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Die program

13 New Member
The questions asks write an application to roll a die and display the results. It also says let the user pick the number of sides on the die. I wrote a program Die2 which rolls a die and display the results, but i don't how to do the second part. thanks

import java.util.Random;
public class myDie
{
private final int MIN_FACES = 4;

private static Random generator = new Random();
private int numFaces; // number of sides on the die
private int faceValue; // current value showing on the die

// Defaults to a six-sided die. Initial face value is 1.

public myDie ()
{
numFaces = 6;
faceValue = 1;
}
// Explicitly sets the size of the die. Defaults to a size of
// six if the parameter is invalid. Initial face value is 1.
public myDie (int faces)
{
if (faces < MIN_FACES)
numFaces = 6;
else
numFaces = faces;

faceValue = 1;
}
// Rolls the die and returns the result.
public int roll ()
{
faceValue = generator.nextInt(numFaces) + 1;
return faceValue;
}
// Returns the current die value.
public int getFaceValue ()
{
return faceValue;
}
}



public class Die2
{
public static void main (String [] args)
{
Die myDie=new Die();
System.out.println("Rolling Die...");
myDie.roll();
System.out.println("It's " +myDie.getFaceValue());
}
}
Dec 10 '06 #1
5 9438
DeMan
1,806 Top Contributor
I notice that your class is declared myDie but you are trying to initialise a Die object.

Expand|Select|Wrap|Line Numbers
  1. public class Die2
  2. {
  3.   public static void main (String [] args)
  4.   {
  5.     myDie thisDie=new myDie();
  6.     System.out.println("Rolling Die...");
  7.     thisDie.roll();
  8.     System.out.println("It's " +thisDie.getFaceValue());
  9.   }
  10. }
  11.  
Dec 10 '06 #2
sallyk57
13 New Member
the program works but what does it mean to have the user pick the number of sides on the die
Dec 10 '06 #3
DeMan
1,806 Top Contributor
I think it means you need to be able to adjust the program to return a random number between 1 and n (where n can be selected by the user). In reality, dice with more than 6 sides exist (for Dungeons & Dragons type games mainly). You can think of a 2 sided die as a coin (other die with less than 6 sides are hard to picture because they probably aren't possible in the real world).

Anyway, I think what they want you to do is add a method to your program to modify the number of faces (Which you already give an option for through your constructor). You have written the code well, so that such a modification is trivial (and in fact is almost identical to your constructor).
Dec 10 '06 #4
sallyk57
13 New Member
would this work

import cs1.Keyboard;
public class Die2
{
public static void main (String [] args)
{
System.out.println("Enter the number of faces you want a die to have ");
int guess= Keyboard.readInt();

myDie thisDie=new myDie();
System.out.println("Rolling Die...");
thisDie.roll();
System.out.println("It's " +thisDie.getFaceValue());
}
}
Dec 10 '06 #5
DeMan
1,806 Top Contributor
I think they mean something more like :
Expand|Select|Wrap|Line Numbers
  1. import cs1.Keyboard;
  2. public class Die2 
  3. {
  4.   public static void main (String [] args)
  5.   { 
  6.   System.out.println("Enter the number of faces you want a die to have ");
  7.   int getFace= Keyboard.readInt();
  8.  
  9.   myDie thisDie=new myDie(getFace); //We use the other for of constructor to create a die that can cope with random numbers other than 6.  
  10. //An eight faced die for example, a two faced die (coin).
  11.   System.out.println("Rolling Die...");
  12.   thisDie.roll(); 
  13.   System.out.println("It's " +thisDie.getFaceValue());
  14.   }
  15. }
  16.  
Dec 10 '06 #6

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

Similar topics

2
14143
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
22
3574
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html ...
0
6075
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
11
2580
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >>...
1
3248
by: Eric Whittaker | last post by:
hi all, im trying to write my first c++ program. a success, but i can't get the window to stay open after user enters input. it just automatically closes. right now the end of my program looks...
9
4519
by: Hemal | last post by:
Hi All, I need to know the memory required by a c program. Is there any tool/utility which can give me the memory usage in terms of DATA segment, TEXT segment, BSS segment etc. I am working...
7
13247
by: ibtc209 | last post by:
I just started programming in C, and I need some help with this problem. Your program will read the information about one MiniPoker hand, namely the rank and suit of the hand’s first card, and...
2
19320
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
0
13289
amitpatel66
by: amitpatel66 | last post by:
There is always a requirement that in Oracle Applications, the Concurrent Program need to be execute programatically based on certain conditions/validations: Concurrent programs can be executed...
0
7086
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
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
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
5578
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,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.