Write a program that simulates the rolling of a pair of dice. First, write a cla
ID: 3800429 • Letter: W
Question
Write a program that simulates the rolling of a pair of dice. First, write a class called Dice (file Dice.java) that simulates a single dice (the singular for dice is die but in modern English dice is an acceptable singular so I will call the class Dice). The Dice class should have the following fields and methods (with these names and caps): Once you have designed the class, design a program/driver class (file [YourName1-Assignment; replace [Your Name] with your actual name) that creates two objects/instances of the Dice class to simulate a pair of dice. The program should simulate the rolling of the 2 dice and display their values (using the Output Dice method, and, if you did the extra credit, also using the Draw Dice method).Explanation / Answer
// Dice.java
import java.util.Random;
public class Dice {
int numberSide;
int value;
Random rn = new Random();
Dice(int n)
{
numberSide = n;
}
Dice()
{
this(6);
}
int getValue()
{
return value;
}
void setValue(int value)
{
this.value = value;
}
void outputValue()
{
switch(value)
{
case 1:
System.out.println("ONE");
break;
case 2:
System.out.println("TWO");
break;
case 3:
System.out.println("THREE");
break;
case 4:
System.out.println("FOUR");
break;
case 5:
System.out.println("FIVE");
break;
case 6:
System.out.println("SIX");
break;
}
}
int roll()
{
int value = rn.nextInt(numberSide) + 1;
setValue(value);
return value;
}
public void draw()
{
int n = getValue();
System.out.println(" ---------");
System.out.print("|");
if (n == 2| n == 3 || n == 4 || n == 5|| n == 6)
{
System.out.print("*");
}
if (n == 1)
{
System.out.print(" ");
}
System.out.print(" ");
if (n == 4 || n == 5 || n == 6)
{
System.out.print("*");
}
System.out.println("|");
System.out.print("|");
if (n == 5)
{
System.out.print(" ");
}
if (n == 1 || n == 3 || n == 5)
{
System.out.print(" * ");
}
if (n == 6)
{
System.out.print("*");
System.out.print(" ");
System.out.print("*");
}
if (n == 2 || n == 4)
{
System.out.print(" ");
}
if (n == 4)
{
System.out.print(" ");
}
System.out.println("|");
System.out.print("|");
if (n == 6 || n == 5 || n == 4)
{
System.out.print("*");
}
System.out.print(" ");
if (n == 6 || n == 5 || n == 4 || n == 3 || n == 2)
{
System.out.print("*");
}
if (n == 1)
{
System.out.print(" ");
}
System.out.println("|");
System.out.println(" ---------");
}
}
// Assignment4Dice.java rename accordingly
public class Assignment4Dice {
public static void main(String[] args)
{
Dice d1 = new Dice();
Dice d2 = new Dice();
d1.roll();
d2.roll();
System.out.print("Value of first dice: ");
d1.outputValue();
System.out.println();
System.out.print("Value of second dice: ");
d2.outputValue();
System.out.println();
d1.draw();
d2.draw();
}
}
/*
Sampe run
Value of first dice: FOUR
Value of second dice: FOUR
---------
|* *|
| |
|* *|
---------
---------
|* *|
| |
|* *|
---------
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.