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).