beginner help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 9
Reputation: brinze is an unknown quantity at this point 
Solved Threads: 0
brinze brinze is offline Offline
Newbie Poster

beginner help

 
0
  #1
Feb 15th, 2008
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:

import java.util.Random; // Needed for random dice numbers.

  
  1. public class Dice
  2. {
  3. public static void main (String[] args)
  4. {
  5. int face; // The face of the die.
  6.  
  7.  
  8. // A random object for the program.
  9. Random randomNumbers = new Random();
  10. // Die face number is set equal to a random number.
  11. face = randomNumbers.nextInt(6)+ 1;
  12.  
  13. // Display results.
  14. if (face == 1)
  15. System.out.println("One:\n\n *");
  16. else if (face == 2)
  17. System.out.println("Two:\n*\n\n *");
  18. else if (face == 3)
  19. System.out.println("Three:\n*\n *\n *");
  20. else if (face == 4)
  21. System.out.println("Four:\n* *\n\n* *");
  22. else if (face == 5)
  23. System.out.println("Five:\n* *\n *\n* *");
  24. else if (face == 6)
  25. System.out.println("Six:\n* *\n* *\n* *");
  26.  
  27. }
  28. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: beginner help

 
0
  #2
Feb 16th, 2008
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,711
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: beginner help

 
0
  #3
Feb 16th, 2008
The method Math.random() returns a random double between 0 and 1.
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 9
Reputation: brinze is an unknown quantity at this point 
Solved Threads: 0
brinze brinze is offline Offline
Newbie Poster

Re: beginner help

 
0
  #4
Feb 16th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: beginner help

 
0
  #5
Feb 16th, 2008
Looks like you tried to add this function inside of your main function. You want it OUTSIDE of main, like this:
  1. public class Dice
  2. {
  3. private int diceRoll()
  4. {
  5. double r=Math.Random();
  6. //do stuff
  7. return /* An int number between 1 and 6 */
  8. }
  9.  
  10. public static void main (String[] args)
  11. {
  12. // main function contents
  13. // call diceRoll from here, but don't put the function in here.
  14. }
  15. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 9
Reputation: brinze is an unknown quantity at this point 
Solved Threads: 0
brinze brinze is offline Offline
Newbie Poster

Re: beginner help

 
0
  #6
Feb 16th, 2008
Originally Posted by brinze View Post
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.
Why can I not seem to leave the main string in:
  1. public static void main (String[] args)
and also put the method in that I need?:
  1. private int diceRoll()
I'm supposed to be building a class, so is it true that I don't need the public static void main....? If I take it out, at least my code compiles with the method in there, but when I try to run it, it says "NoSuchMethodError:main"
Somebody please help me! I'm really trying.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: beginner help

 
0
  #7
Feb 16th, 2008
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".

  1. // HelloWorld.java
  2. public class HelloWorld
  3. {
  4. public static void main( String[] args)
  5. {
  6. System.out.println( "Hello World!" );
  7. }
  8. }

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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 9
Reputation: brinze is an unknown quantity at this point 
Solved Threads: 0
brinze brinze is offline Offline
Newbie Poster

Re: beginner help

 
0
  #8
Feb 17th, 2008
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:
  1. import java.util.Random; // Needed for random dice numbers.
  2.  
  3. public class Dice
  4. {
  5. int face;
  6. private int diceRoll()// method to simulate the rolling of one die,
  7. //generating random number 1 through 6.
  8. {
  9. // A random object for the program.
  10. Random randomNumbers = new Random();
  11. // Die face number is set equal to a random number.
  12. face = randomNumbers.nextInt(6)+ 1;
  13. return face;
  14. }
  15.  
  16. public static void main(String[] args)
  17. {
  18. // Display results.
  19. if (face == 1)
  20. System.out.println("One:\n\n *");
  21. else if (face == 2)
  22. System.out.println("Two:\n*\n\n *");
  23. else if (face == 3)
  24. System.out.println("Three:\n*\n *\n *");
  25. else if (face == 4)
  26. System.out.println("Four:\n* *\n\n* *");
  27. else if (face == 5)
  28. System.out.println("Five:\n* *\n *\n* *");
  29. else if (face == 6)
  30. System.out.println("Six:\n* *\n* *\n* *");
  31.  
  32. }
  33. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: beginner help

 
0
  #9
Feb 17th, 2008
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.
  1. import java.util.Random; // Needed for random dice numbers.
  2.  
  3. public class Dice
  4. {
  5. int face;
  6. private int diceRoll()// method to simulate the rolling of one die,
  7. //generating random number 1 through 6.
  8. {
  9. // A random object for the program.
  10. Random randomNumbers = new Random();
  11. // Die face number is set equal to a random number.
  12. face = randomNumbers.nextInt(6)+ 1;
  13. return face;
  14. }
  15.  
  16.  
  17. public Dice ()
  18. {
  19. }
  20.  
  21. private void PlayDiceGame ()
  22. {
  23. // Display results.
  24. if (face == 1)
  25. System.out.println("One:\n\n *");
  26. else if (face == 2)
  27. System.out.println("Two:\n*\n\n *");
  28. else if (face == 3)
  29. System.out.println("Three:\n*\n *\n *");
  30. else if (face == 4)
  31. System.out.println("Four:\n* *\n\n* *");
  32. else if (face == 5)
  33. System.out.println("Five:\n* *\n *\n* *");
  34. else if (face == 6)
  35. System.out.println("Six:\n* *\n* *\n* *");
  36. }
  37.  
  38.  
  39.  
  40. public static void main(String[] args)
  41. {
  42. Dice aDie = new Dice ();
  43. aDie.PlayDiceGame();
  44. }
  45. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 9
Reputation: brinze is an unknown quantity at this point 
Solved Threads: 0
brinze brinze is offline Offline
Newbie Poster

Re: beginner help

 
0
  #10
Feb 17th, 2008
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:
  1. //********************************************************************
  2. // SnakeEyes.java
  3. //
  4. // This program rolls a pair of dice 1000 times and counts the number of
  5. // snake eyes (both dies roll a one) rolled. The Dice class must be used
  6. // to simulate the roll of a die.
  7. //********************************************************************
  8.  
  9. import java.util.Scanner;
  10.  
  11. public class SnakeEyes
  12. {
  13. public static void main (String[] args)
  14. {
  15. int rolls, roll1, roll2, noRolls, noSnakeEyes = 0;
  16. Scanner input = new Scanner(System.in);
  17.  
  18. // Create two separate dice objects to create a pair of dice
  19. Dice die1 = new Dice();
  20. Dice die2 = new Dice();
  21.  
  22. // get from the user the number of dice rolls
  23. System.out.print ("Enter the number of dice rolls: ");
  24. noRolls = input.nextInt();
  25.  
  26. // loop for the number of rolls requested by the user
  27. for (rolls = 1; rolls <= noRolls; rolls++)
  28. {
  29. System.out.println("_______________Roll #: " + rolls + " _________________");
  30. roll1 = die1.diceRoll();
  31. roll2 = die2.diceRoll();
  32.  
  33. if (roll1 == 1 && roll2 == 1)
  34. noSnakeEyes++;
  35. }
  36.  
  37. // Display the dice roll results
  38. System.out.println ("Number of Dice Rolls: " + noRolls);
  39. System.out.println ("Number of Snake Eyes Rolled: " + noSnakeEyes);
  40. System.out.println ("\nGame Ended");
  41.  
  42. }
  43. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC