Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: First Java Program -- Single Die

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default First Java Program -- Single Die

    Hi there,

    I'm hoping someone could help me out. I have absolutely no Java experience, and I need to work on this "Dice" program. I've done it twice already, but clearly I still have no idea what I'm doing. I've also been pouring over examples on here and elsewhere online, but none of them exactly match what I'm doing, as they all say "Pair of Dice".

    Mine says: "Create a class called Dice to represent a SINGLE cube". It should have a method called roll() that randomly selects a number from 1-6 for the value of the dice."

    It has to use java.util.random, not math.java, and it has to have numberShowing:int, roll():int, and main() all in it.

    The last part reads "Create a test main method for the Dice class that creates a dice and rolls it many times. Can you keep track of how many times a number comes up? Describe how or implement it in the program."


    I have started at this computer for hours, and read as much info as I can. I can't tell you how high my anxiety level is right now, nor how much I despise this. Please help me get this code written.

    Thank you.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: First Java Program -- Single Die

    paste your code here, tell us what or where you're stuck. if it has compilation errors or runtime errors paste it here also.
    have you tried using the Random class in java.util? its java.util.Random not java.util.random, java is a case sensitive programming language.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: First Java Program -- Single Die

    Thank you, yes we have to use java.util.Random -- I just wrote it incorrectly. This is my code.

    import java.util.Random;

    public class Dice
    {
    public static void main( String[] args)
    {
    Random randomNumbers = new Random(); // Generates random numbers
    int[] array = new int[ 7 ]; // Declares the array

    //Roll the die 36,000 times
    for ( int roll = 1; roll <=36000; roll++ )
    ++array[ 1 + randomNumbers.nextInt ( 6 ) ];

    System.out.printf( "%s%10s\n", "Face", "Frequency" );

    // outputs array values
    for ( int face = 1; face < array.length; face++ )
    System.out.printf( "%4d%10d\n", face, array[ face ] );

    } // end main

    } // end class DiceRolling




    I didn't get any errors, but my teacher wrote this:

    "This is a good start but I don't see any dice. Create a Dice class with the indicated data member and member function. I expect to see the data member (numberShowing) and member function ( roll() ). Each dice has it's own "numberShowing" and ever dice can be roll() ed.

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: First Java Program -- Single Die

    your teacher is right. what he/she wants you to create is a Dice class. in your case, you have created a simple program (with main method) which is capable of printing a random number of dice.
    Here is example of what he/she wants:
    public class Dice {
     
           public int numberShowing() {
                 // do some stuff here if necessary before return statement
                 // return the random generated dice
           }
     
           public void roll() {
                /*
                do some stuff here like generating a random numbers from 1 - 6
                */
           }
    }

    then test that Dice class
    public class TestDice {
     
           public static void main(String[] args) {
                  Dice dice = new Dice(); // creating an object and reference variable of the object created
                  // do some stuff here like rolling the dice
           }
    }

    TestDice class is just a testing class to make sure that the Dice class works perfectly.

Similar Threads

  1. A single java program to receive and send plain text through differen ports.
    By Intriguing in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 1st, 2014, 10:02 AM
  2. Die Rolling Program
    By Olympaphibian89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2012, 07:23 PM
  3. Replies: 3
    Last Post: March 17th, 2011, 10:09 AM
  4. client server program on a single machine
    By subhopam in forum Java Networking
    Replies: 1
    Last Post: December 16th, 2009, 02:02 PM
  5. Java program to display single and plural words
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 10th, 2009, 11:09 PM