Need help with the following program with Java: public class Dice { //Variable t
ID: 3881139 • Letter: N
Question
Need help with the following program with Java:
public class Dice {
//Variable to name side of dice and value
private String DiceValue;
private String DiceSide;
//default constructor:
public Dice() {
DiceValue="1";
DiceSide="side 1";
}//end constructor
//non default constructor
public Dice(String value, String side) {
DiceValue=value;
DiceSide=side;
}//end non default contructor
//getter method, accessor method
public String getDiceValue() {
return DiceValue;
}
public String getDiceSide() {
return DiceSide;
}//end get method
//Setter Method
public void setDiceValue(String newDiceValue) {
DiceValue=newDiceValue;
}
public void setDiceSide(String newDiceSide) {
DiceSide=newDiceSide;
}
public String toString() {
return DiceValue + " " + DiceSide;
}
}//end class
With the following code I need to write a Dice game between 2 players that will return the higher value of dice. It needs to use Setters and Getters please. I just dont know where to continue after seeing this code written.
Explanation / Answer
Dice.java
import java.util.Random;
public class Dice {
// Declaring instance variables
private int noOfSides;
private int value;
// Parameterized constructor
public Dice(int noOfSides) {
this.noOfSides = noOfSides;
}
// getters and setters
public int getNoOfSides() {
return noOfSides;
}
public void setNoOfSides(int noOfSides) {
this.noOfSides = noOfSides;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int roll() {
// Creating a random Class object
Random r = new Random();
this.value = r.nextInt((noOfSides - 1) + 1) + 1;
return value;
}
}
________________
PlayGame.java
import java.util.Random;
public class PlayGame {
public static void main(String[] args) {
//Declaring variables
int player1,player2;
//Creating a random Class object
Random r = new Random();
/* This while loop continues to execute
* until a player won the game
*/
while(true)
{
int toss=r.nextInt(2) + 1;
Dice d=new Dice(6);
if(toss==1)
{
player1=d.roll();
System.out.println("Player#1 rolled :"+player1);
player2=d.roll();
System.out.println("Player#2 rolled :"+player2);
if(player1>player2)
{
System.out.println("Player#1 won the game.");
break;
}
else if(player1<player2)
{
System.out.println("Player#2 won the game.");
break;
}
else
{
System.out.println("Draw.");
continue;
}
}
else if(toss==2)
{
player2=d.roll();
System.out.println("Player#2 rolled :"+player2);
player1=d.roll();
System.out.println("Player#1 rolled :"+player1);
if(player1>player2)
{
System.out.println("Player#1 won the game.");
break;
}
else if(player1<player2)
{
System.out.println("Player#2 won the game.");
break;
}
else
{
System.out.println("Draw.");
continue;
}
}
}
}
}
_________________
Output:
Player#1 rolled :4
Player#2 rolled :4
Draw.
Player#1 rolled :1
Player#2 rolled :3
Player#2 won the game.
____________
Output#2:
Player#2 rolled :1
Player#1 rolled :2
Player#1 won the game.
_______________Could you plz rate me well.Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.