#13 Modified Version Task 2- Create a Die “programmer created” class. Inside the
ID: 3572322 • Letter: #
Question
#13 Modified Version
Task 2- Create a Die “programmer created” class. Inside the “programmer created class” create 2 instance variables, each instance variable will be an integer type.
Create a void method that returns the value of a die roll. (random number between 1-6)
Create a void method the output the value of two dice being rolled.
All calculations and integer assignment will take place in the “programmer created” class. main in your program will only operate the execution of the program.
Output will be the value of one random die roll and then the value of 2 random dice being rolled.
Sample Output
1 die Roll = 5
2 dice Roll = 7
Example of main in the program.//main only contains calls to methods/objects created in the “programmer created” class
public class DieRollClassDemo {
public static void main(String[] args) {
Die rollingdice = new Die();
rollingdice.dieroll();
rollingdice.diceroll();
Explanation / Answer
Die.java
import java.util.Random;
public class Die {
//Declaring variables
private int num1;
private int num2;
//Zero argumented constructor
public Die() {
}
//This method displays the randomly generated dice value
public void getNums()
{
//Creating an object of Random class Object
Random r = new Random();
//Getting random value for dice 1
num1=r.nextInt(6) + 1;
//Getting random value for dice 1
num2=r.nextInt(6) + 1;
//Displaying the values
System.out.println("1 Die Roll ="+num1);
System.out.println("2 Dice Roll ="+num2);
}
}
_________________
DieRollClassDemo.java
public class DieRollClassDemo {
public static void main(String[] args) {
//Creating an Die class object
Die rollingDice =new Die();
//Calling the method getNums() on the Dice class object
rollingDice.getNums();
}
}
_____________________
Output:
1 Die Roll =2
2 Die Roll =6
_______Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.