473,473 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

beginner assignment

6 New Member
I'm new to all programming. I'm stonewalled, and I'll show what I have.My assignment is this:
Write a Java Dice class which will simulate the rolling of one die and
display the die face.
Follow these requirements for the Dice class:
· The class must be named Dice.
· A method named diceRoll() must be included.
· A random number between 1 and 6 inclusive must be generated.
· The random number value generated must be returned.
· The face of the die must be displayed as below:
Requirements:
 The Dice class should be named Dice.java.
 Include comments defining the purpose of the code.
Six:
* *
* *
* *
Five:
* *
*
* *
Four:
* *
* *
Three:
*
*
*
Two:
*
*
One:
*
Here's my code:
Expand|Select|Wrap|Line Numbers
  1. import java.util.Random; // Needed for random dice numbers.
  2.  
  3. public class Dice
  4. {
  5. public static void main (String[] args)
  6. {
  7. int face; // The face of the die.
  8.  
  9. // A random object for the program.
  10. Random randomNumbers = new Random();
  11. // Die face number is set equal to arandom number.
  12. face = randomNumbers.nextInt(6)+ 1;
  13. face = diceRoll();
  14. // Display results.
  15. if (face == 1)
  16. System.out.println("One:\n *");
  17. else if (face == 2)
  18. System.out.println("Two:\n*\n\n  *");
  19. else if (face == 3)
  20. System.out.println("Three:\n*\n *\n  *");
  21. else if (face == 4)
  22. System.out.println("Four:\n*  *\n\n*  *");
  23. else if (face == 5)
  24. System.out.println("Five:\n*  *\n *\n*  *");
  25. else if (face == 6)
  26. System.out.println("Six:\n*  *\n*  *\n*  *");
  27. }
  28. public int diceRoll()
  29. {
  30. return diceRoll();
  31. }
  32.  
  33. }
Gives me this error:
Dice.java:14: non-static method diceRoll() cannot be referenced from a static context
Feb 15 '08 #1
11 2298
BigDaddyLH
1,216 Recognized Expert Top Contributor
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Feb 15 '08 #2
BigDaddyLH
1,216 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. public int diceRoll() {
  2.     return diceRoll();
  3. }
  4.  
Rethink your code. This is an infinite loop!
Feb 15 '08 #3
pjbroome
6 New Member
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
OK, sorry about that. Thanks
Feb 15 '08 #4
BigDaddyLH
1,216 Recognized Expert Top Contributor
OK, sorry about that. Thanks
No problem. Indent your code, too.
Feb 15 '08 #5
pjbroome
6 New Member
Thanks. I am just lost because I'm supposed to use the method diceRoll, which is supposed to then be able to be used by other programs which involve rolling one die or two dice, getting the random outcomes that my created class provides. So I'm supposed to create this class, generating and returning the random die results (1 thru 6), and using the function or method diceRoll to do it. I'm gathering by the responses that I'm just way off. I'm in an online class, the book is helpful, but there is no interaction or help with this. But thanks for your help. I've taken it and will continue working. Perhaps somebody out there may know how impossible this is when you are brand new to it all!!
Feb 15 '08 #6
BigDaddyLH
1,216 Recognized Expert Top Contributor
Think about the design of class Dice (isn't the singular Die, by the way?). What is the observable state of a Die? It'\s current face. What can you do with a Die? Roll it, which may change its face...

Expand|Select|Wrap|Line Numbers
  1. public class Die {
  2.     ...
  3.     public int getFace() {...}
  4.     public void roll() {...}
  5. }
What is the implemented state of a Die? The current state and a Random object to simulate rolling:

Expand|Select|Wrap|Line Numbers
  1. public class Die {
  2.     private int face;
  3.     private Random rnd;
  4.     ...
  5. }
Is that how you think of code?
Feb 15 '08 #7
pjbroome
6 New Member
Think about the design of class Dice (isn't the singular Die, by the way?). What is the observable state of a Die? It'\s current face. What can you do with a Die? Roll it, which may change its face...

Expand|Select|Wrap|Line Numbers
  1. public class Die {
  2.     ...
  3.     public int getFace() {...}
  4.     public void roll() {...}
  5. }
What is the implemented state of a Die? The current state and a Random object to simulate rolling:

Expand|Select|Wrap|Line Numbers
  1. public class Die {
  2.     private int face;
  3.     private Random rnd;
  4.     ...
  5. }
Is that how you think of code?
Yes, thank you for this. The professor says we must name it dice instead of die, even though this is about one die - I'll get points off if I call it die but I wondered the same thing.
I've got the code now so that it generates the random numbers 1 through 6. I've also got it printing out the way the teacher wants it. The program is doing what she wants it to do - with one exception. She wants me to use "diceRoll()" method in this program, so she can use this class (dice) in another game program that rolls two dice.
I'm wondering, in your example above, you have:

Expand|Select|Wrap|Line Numbers
  1. public class Die {
  2.     ...
  3.     public int getFace() {...}
  4.     public void roll() {...}
  5. }
Should I be using the name "rollDice()" instead of your "getFace"? Would renaming that help to get me closer?
Feb 15 '08 #8
BigDaddyLH
1,216 Recognized Expert Top Contributor
What is the requirement for rollDice()? What does it do, what are its parameters and its return type? Is is a static method?
Feb 15 '08 #9
pjbroome
6 New Member
What is the requirement for rollDice()? What does it do, what are its parameters and its return type? Is is a static method?
Good questions! These are my questions! Her instructions are, I quote:
"The class must be named Dice
A method named diceRoll() must be included
A random number between 1 and 6 inclusive must be generated
The random number value generated must be returned
The face of the die must be displayed as below:...[display not included]"

So I guess the rollDice() method is supposed to initiate the "roll of the die", which is the generation of a new random number, and then the display of the number. One must "roll a die" before a new number is seen, right?
Feb 16 '08 #10
pjbroome
6 New Member
Good questions! These are my questions! Her instructions are, I quote:
"The class must be named Dice
A method named diceRoll() must be included
A random number between 1 and 6 inclusive must be generated
The random number value generated must be returned
The face of the die must be displayed as below:...[display not included]"

So I guess the rollDice() method is supposed to initiate the "roll of the die", which is the generation of a new random number, and then the display of the number. One must "roll a die" before a new number is seen, right?
Here's my code now. Professor has a program that rolls 2 dice and it calls on the "diceRoll()" method that's supposed to be in my code to do it. I don't know how to get it to do that. The following code does what she wants "diceRoll()" to do, but how do I insert this method in this code??:

Expand|Select|Wrap|Line Numbers
  1. import java.util.Random; // Needed for random dice numbers.
  2.  
  3. public class Dice
  4. {
  5.   public static void main (String[] args)
  6.   {    
  7.     int face; // The face of the die.
  8.  
  9.  
  10.       // A random object for the program.
  11.    Random randomNumbers = new Random();
  12.    // Die face number is set equal to a random number.
  13.    face = randomNumbers.nextInt(6)+ 1;
  14.  
  15.    // Display results.
  16.    if (face == 1)
  17.     System.out.println("One:\n\n *");
  18.     else if (face == 2)
  19.     System.out.println("Two:\n*\n\n  *");
  20.     else if (face == 3)
  21.     System.out.println("Three:\n*\n  *\n    *");
  22.     else if (face == 4)
  23.     System.out.println("Four:\n*  *\n\n*  *");
  24.     else if (face == 5)
  25.     System.out.println("Five:\n*   *\n  *\n*   *");
  26.     else if (face == 6)
  27.     System.out.println("Six:\n*  *\n*  *\n*  *");
  28.  
  29.     } 
  30. }
  31.  
Feb 16 '08 #11
Laharl
849 Recognized Expert Contributor
Here's a hint: main() is a method. You need another method, called diceRoll(), that returns an integer. The 'void' keyword in main() means that it has no return type, but diceRoll() needs to return an int. How would you change the first line of main() (public static void main(String[] args)) to represent a method that takes no input and returns an integer?
Feb 16 '08 #12

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

Similar topics

13
by: Hadi | last post by:
Hi, I have two files and the array that contains 20 words in it. I'm trying to write python script that suppose achieve the following: -Open file-1, read the line that contains 20 words separated...
6
by: Alan Little | last post by:
a=1 b= class C(): def __init__(self): a=2 b.append(3) c = C() print b
5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
2
by: Pierre Couderc | last post by:
What is the difference between these 2 ways of writing : myClass a; a=b; and myClass a=b; I wrongly thought they were identical (MSVC6). Thank you in advance.
17
by: C_beginner | last post by:
Am I did the following program corectly according to the question? Question: /* 1:You have just been employed by MacroMuscle, Inc. (Software for Hard Bodies). The company is entering the...
33
by: aaron | last post by:
I have a question in my class.. hoping to get some help I need to create a program that will print firstName middleName lastName then their initials User will input: John Smith Doe Output:...
3
by: Luke | last post by:
I'm pretty stuck at the moment and wondering if anyone can spot the problem. Trying to create a function that will read a text file into a list and return that list. I wrote the following...
4
by: Orphic | last post by:
I'm hoping someone can help me with my homework assignment, or at least steer me in the right direction. I've got ZERO programming experience and am struggling bigtime, so I'm sorry if my questions...
6
by: RaulAbHK | last post by:
Dear all, I guess this is a basic question with an easy answer but I am a beginner and I would much apreciate your feedbacks. Let's say I have a library with some functionality to perform some...
5
by: mac010904 | last post by:
i'm new to java an have an assignment. i need to write a program that will calculate the sides of a triangle using x and y coordinates.also, it should provide the area, perimeter and lengths of the...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.