| | |
beginner help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 9
Reputation:
Solved Threads: 0
Hello - I'm a brand new student of programming and I'm needing help. I'm stonewalled. I've got an assignment that requires me to create a class named Dice that another program will use to roll dice.
The instructions are:
-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 below: [description of display not included here - I've got that in my code, and it works right]
Here is my code thus far. It compiles/runs fine - I think it does what it's supposed to do, BUT... I don't know how to create or implement or include this method diceRoll(), which professor's program needs to make hers work. This part confuses me. I take this course online - the book is helpful, but I have had little or no communication or help otherwise. Thanks in advance for any ideas:
The instructions are:
-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 below: [description of display not included here - I've got that in my code, and it works right]
Here is my code thus far. It compiles/runs fine - I think it does what it's supposed to do, BUT... I don't know how to create or implement or include this method diceRoll(), which professor's program needs to make hers work. This part confuses me. I take this course online - the book is helpful, but I have had little or no communication or help otherwise. Thanks in advance for any ideas:
import java.util.Random; // Needed for random dice numbers.Java Syntax (Toggle Plain Text)
public class Dice { public static void main (String[] args) { int face; // The face of the die. // A random object for the program. Random randomNumbers = new Random(); // Die face number is set equal to a random number. face = randomNumbers.nextInt(6)+ 1; // Display results. if (face == 1) System.out.println("One:\n\n *"); else if (face == 2) System.out.println("Two:\n*\n\n *"); else if (face == 3) System.out.println("Three:\n*\n *\n *"); else if (face == 4) System.out.println("Four:\n* *\n\n* *"); else if (face == 5) System.out.println("Five:\n* *\n *\n* *"); else if (face == 6) System.out.println("Six:\n* *\n* *\n* *"); } }
Java Syntax (Toggle Plain Text)
private void diceRoll() {}
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
The method Math.random() returns a random double between 0 and 1.
0 < Math.Random <1;
So one variation of jwenting's post :
0 < Math.Random <1;
So one variation of jwenting's post :
private int diceRoll() {
double r=Math.Random();
//do stuff
return /* An int number between 1 and 6 */
} Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Feb 2008
Posts: 9
Reputation:
Solved Threads: 0
Thank you - this is so helpful. But unfortunately I still get errors when I put this in. This is what I added (in red), but I got the errors that you see below:
code:
And here are the 2 errors:
Dice.java:10: illegal start of expression
private int diceRoll() {
^
Dice.java:10: ';' expected
private int diceRoll() {
I keep getting something like this when I try to add this method.
code:
private int diceRoll() { // A random object for the program. Random randomNumbers = new Random(); // Die face number is set equal to a random number. face = randomNumbers.nextInt(6)+ 1; // Display results. if (face == 1) System.out.println("One:\n\n *"); else if (face == 2) System.out.println("Two:\n*\n\n *"); else if (face == 3) System.out.println("Three:\n*\n *\n *"); else if (face == 4) System.out.println("Four:\n* *\n\n* *"); else if (face == 5) System.out.println("Five:\n* *\n *\n* *"); else if (face == 6) System.out.println("Six:\n* *\n* *\n* *"); } } }
And here are the 2 errors:
Dice.java:10: illegal start of expression
private int diceRoll() {
^
Dice.java:10: ';' expected
private int diceRoll() {
I keep getting something like this when I try to add this method.
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
Looks like you tried to add this function inside of your main function. You want it OUTSIDE of main, like this:
JAVA Syntax (Toggle Plain Text)
public class Dice { private int diceRoll() { double r=Math.Random(); //do stuff return /* An int number between 1 and 6 */ } public static void main (String[] args) { // main function contents // call diceRoll from here, but don't put the function in here. } }
•
•
Join Date: Feb 2008
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
Thank you - this is so helpful. But unfortunately I still get errors when I put this in. This is what I added (in red), but I got the errors that you see below:
code:
private int diceRoll() { // A random object for the program. Random randomNumbers = new Random(); // Die face number is set equal to a random number. face = randomNumbers.nextInt(6)+ 1; // Display results. if (face == 1) System.out.println("One:\n\n *"); else if (face == 2) System.out.println("Two:\n*\n\n *"); else if (face == 3) System.out.println("Three:\n*\n *\n *"); else if (face == 4) System.out.println("Four:\n* *\n\n* *"); else if (face == 5) System.out.println("Five:\n* *\n *\n* *"); else if (face == 6) System.out.println("Six:\n* *\n* *\n* *"); } } }
And here are the 2 errors:
Dice.java:10: illegal start of expression
private int diceRoll() {
^
Dice.java:10: ';' expected
private int diceRoll() {
I keep getting something like this when I try to add this method.
Java Syntax (Toggle Plain Text)
public static void main (String[] args)
Java Syntax (Toggle Plain Text)
private int diceRoll()
Somebody please help me! I'm really trying.
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
Well I'd say you should first test to make sure that your java JDK and JRE are installed right and that you can run/compile a simple program. Here's a Hello World program. Save it under filename "HelloWorld.java".
This should compile and run without errors. It should simply display "Hello World!" to the console window. If you get either a compiler or runtime error, something other than your code is the problem. If it does work as it should, please repost your entire program. I'm not sure where you are trying to place everything.
JAVA Syntax (Toggle Plain Text)
// HelloWorld.java public class HelloWorld { public static void main( String[] args) { System.out.println( "Hello World!" ); } }
This should compile and run without errors. It should simply display "Hello World!" to the console window. If you get either a compiler or runtime error, something other than your code is the problem. If it does work as it should, please repost your entire program. I'm not sure where you are trying to place everything.
•
•
Join Date: Feb 2008
Posts: 9
Reputation:
Solved Threads: 0
I did the HelloWorld. It did fine.
I tried your suggestion. It seemed right to me, but I'm still doing something(s) wrong. Is it because I'm not generating the random numbers the way you are? But also, when I run this code, I am still getting the same error: java.lang.NoSuchMethodError: main
Exception in thread "main"
Plus, should I get rid of the whole "face" variable? I have to display each of the six faces of the die in a certain way, and the code shows the way I did it, and it worked before I tried to create and install this diceRoll() method. Also, unlike before, I also got an error saying non-static variable face cannot be referenced from a static context
Here's what I did, trying to put the method outside of the main function ... and PS thanks so much for helping. I'm very confused, but don't want to give up:
I tried your suggestion. It seemed right to me, but I'm still doing something(s) wrong. Is it because I'm not generating the random numbers the way you are? But also, when I run this code, I am still getting the same error: java.lang.NoSuchMethodError: main
Exception in thread "main"
Plus, should I get rid of the whole "face" variable? I have to display each of the six faces of the die in a certain way, and the code shows the way I did it, and it worked before I tried to create and install this diceRoll() method. Also, unlike before, I also got an error saying non-static variable face cannot be referenced from a static context
Here's what I did, trying to put the method outside of the main function ... and PS thanks so much for helping. I'm very confused, but don't want to give up:
Java Syntax (Toggle Plain Text)
import java.util.Random; // Needed for random dice numbers. public class Dice { int face; private int diceRoll()// method to simulate the rolling of one die, //generating random number 1 through 6. { // A random object for the program. Random randomNumbers = new Random(); // Die face number is set equal to a random number. face = randomNumbers.nextInt(6)+ 1; return face; } public static void main(String[] args) { // Display results. if (face == 1) System.out.println("One:\n\n *"); else if (face == 2) System.out.println("Two:\n*\n\n *"); else if (face == 3) System.out.println("Three:\n*\n *\n *"); else if (face == 4) System.out.println("Four:\n* *\n\n* *"); else if (face == 5) System.out.println("Five:\n* *\n *\n* *"); else if (face == 6) System.out.println("Six:\n* *\n* *\n* *"); } }
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
Yeah, the static thing can be a drag when you are first starting. One way around it is to basically create some other function within your class and put everything there that you were originally putting in main. You can't do the function calls from main since it is static, so create a new function that isn't static and have it do all the work that you originally intended main to do. You also can't call that function directly from main either so instead you can create a variable of that class in main and have IT call the new function. Finally, Java being Java, it doesn't like it when you don't initialize and use the "new" command, so I did. It actually let me get away without defining a null constructor, but I did it anyway since I think that's a better practice. Of course that function is empty so it does nothing. Thus you are not done yet since you have to somehow assign "face" a value and this program has not (as your last posted program did not either) done so, so you'll need to do that. Basically I don't think you need to touch main after this. Anything you wanted to put in main before, now put in the PlayDiceGame function or somewhere else. Note that you have not called your diceRoll function. This program should compile and run but not really do anything.
Good luck.
Good luck.
JAVA Syntax (Toggle Plain Text)
import java.util.Random; // Needed for random dice numbers. public class Dice { int face; private int diceRoll()// method to simulate the rolling of one die, //generating random number 1 through 6. { // A random object for the program. Random randomNumbers = new Random(); // Die face number is set equal to a random number. face = randomNumbers.nextInt(6)+ 1; return face; } public Dice () { } private void PlayDiceGame () { // Display results. if (face == 1) System.out.println("One:\n\n *"); else if (face == 2) System.out.println("Two:\n*\n\n *"); else if (face == 3) System.out.println("Three:\n*\n *\n *"); else if (face == 4) System.out.println("Four:\n* *\n\n* *"); else if (face == 5) System.out.println("Five:\n* *\n *\n* *"); else if (face == 6) System.out.println("Six:\n* *\n* *\n* *"); } public static void main(String[] args) { Dice aDie = new Dice (); aDie.PlayDiceGame(); } }
•
•
Join Date: Feb 2008
Posts: 9
Reputation:
Solved Threads: 0
You have helped me a great deal. I probably should have done this ages ago, but I'd like to include the program code that is supposed to call my diceRoll function. I am not allowed to alter this code at all, per assignment. It's called SnakeEyes, and it successfully calls the diceRoll function, but I still cannot get the display of the faces of the die to work. Do you see anywhere in this SnakeEyes code where it calls for this part of the Dice class that I've created? I know the faces of the die are supposed to display, but I can't get it to work. Should I somehow create another method for that part of the "dice" code? I tried to include it in the diceRoll funtion also, but it was not successful. Here is the SnakeEyes code that's suposed to call my diceRoll() - which I THINK it's supposed to also display the faces of the die that I have in my Dice class:
Java Syntax (Toggle Plain Text)
//******************************************************************** // SnakeEyes.java // // This program rolls a pair of dice 1000 times and counts the number of // snake eyes (both dies roll a one) rolled. The Dice class must be used // to simulate the roll of a die. //******************************************************************** import java.util.Scanner; public class SnakeEyes { public static void main (String[] args) { int rolls, roll1, roll2, noRolls, noSnakeEyes = 0; Scanner input = new Scanner(System.in); // Create two separate dice objects to create a pair of dice Dice die1 = new Dice(); Dice die2 = new Dice(); // get from the user the number of dice rolls System.out.print ("Enter the number of dice rolls: "); noRolls = input.nextInt(); // loop for the number of rolls requested by the user for (rolls = 1; rolls <= noRolls; rolls++) { System.out.println("_______________Roll #: " + rolls + " _________________"); roll1 = die1.diceRoll(); roll2 = die2.diceRoll(); if (roll1 == 1 && roll2 == 1) noSnakeEyes++; } // Display the dice roll results System.out.println ("Number of Dice Rolls: " + noRolls); System.out.println ("Number of Snake Eyes Rolled: " + noSnakeEyes); System.out.println ("\nGame Ended"); } }
![]() |
Similar Threads
- What program language should a beginner use? (IT Professionals' Lounge)
- C++ Beginner - #include recursion problem (C++)
- MS Access Beginner (MS SQL)
- PHP Beginner Here (PHP)
- beginner (C++)
- Beginner's questions: C++ and databases (C++)
- beginner needs help with game programming (Game Development)
Other Threads in the Java Forum
- Previous Thread: problem
- Next Thread: Red Black Tree Frustration!
| Thread Tools | Search this Thread |
Tag cloud for Java
account actuate android api apple applet application arguments array arrays automation banking binary bluetooth character chat class classes client code component constructor crashcourse database design draw eclipse error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide idea if_statement image input integer interface j2me java javaarraylist javadoc javaee javamicroeditionuseofmotionsensor javaprojects jetbrains jmf jni jpanel julia linux list loop map method methods mobile multithreading netbeans newbie number oracle print printing problem program programming project property recursion remove scanner screen server set size sms socket software sort splash sql stop string swing test textfield thread threads time tree validation windows






