-5

I posted about this yesterday, but I think I need to make it less involved. 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.

user3530332
  • 1
  • 1
  • 1
  • 3

1 Answers1

5

First, don't panic. It's hard when things are new and don't be discouraged if you don't know where to start.

Here is the documentation for java.util.Random: http://docs.oracle.com/javase/7/docs/api/java/util/Random.html

Your first job is to work out how to read this and understand it. Try Googling "java understanding api documentation" or similar.

It describes how the class you've been told to use works.

You will also need to know how to define a class of your own, called Dice with an instance variable numberShowing and instance methods roll and main. You can research these things by Googling something like "java introduction tutorial create class" or similar for each of these.

Here's a quick rundown of what you need to know (but know the approaches above for future problems):

Declaring a class named Dice:

File: Dice.java (name must match class name below)

public class Dice {
}

Adding an instance variable numberShowing:

You need to know what type the variable needs to be. You have been told to use an int which represents an integer value (a whole number):

File: Dice.java

public class Dice {
    int numberShowing;
}

Adding a method roll():

You need to know what type of value this method returns, and the types of any values it can be given to perform some calculation or action. In this case you have been told it doesn't take any values and returns a value of type int (this is what I assume is mean't by roll():int in your above description - that is, empty parenthesis for no values passed in and :int to indicate the method returns an int):

File: Dice.java

public class Dice {
    int numberShowing;
    public int roll() {
        /* code to perform calculation goes here */
    }
}

You need to place the code to perform a random roll and assign the result into numberShowing at the point where I have the comment above (the comment is denoted text enclosed in /* and */).

You will need to create an object of the java.util.Random class. To do this you will need to import this class. Then you need to create using its constructor, and call an appropriate method - be sure to check the API document to understand how the method works.

File: Dice.java

import java.util.Random;

public class Dice {
    int numberShowing;
    public int roll() {
        Random random = new Random(); /* <-- this is a constructor */
        numberShowing = random.nextInt(6) + 1; /* <-- look at the API doc for nextInt() to see why we give it 6 as and argument, and why we need to add 1 to the result */
    }
}

Adding a static method main():

This is the standard entry point for running a class as a program. You should be able to find an example of this easily by googling any getting started or introduction to java tutorial. (Sorry, I ran out of time).

Mike Tunnicliffe
  • 10,674
  • 3
  • 31
  • 46