Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help on how to create this, I have been working on this for a few days bu

ID: 3881681 • Letter: I

Question

I need help on how to create this, I have been working on this for a few days but my code is a mess. Could you also give a brief explanation on how and why your code works?

For this assignment you will need to make a class to represent a Dice object. The instance variables for the Dice object will be: the value of the dice (this is the value when you roll the dice) the number of sides of the dice (the default value will be 6 - for a normal type dice) Make the following methods for this Dice object class: the default constructor a constructor that has 1 parameter of the number of sides of the dice the setters and getters for your 2 instance variables the toString method a method to roll the dice - this method should return the value of the roll Then make a test class. The test class should have 2 players roll the dice object and output which player rolled the higher value.

Explanation / Answer

Please find my implementation.

######## Dice ########

import java.util.Random;

public class Dice {

   private int side;

  

   private static Random random = new Random();

   // default constructor

   public Dice() {

       side = 6;

   }

   // parameterized constructor

   public Dice(int s) {

       side = s;

   }

  

   // getters

   public int getSide() {

       return side;

   }

  

   // setter

   public void setSide(int s) {

       side = s;

   }

  

   // return roll value

   public int roll(){

       return random.nextInt(side)+1; // random value from 1 - side

   }

}

########### DiceTest ###########

public class TestDice {

  

   public static void main(String[] args) {

      

       Dice dice1 = new Dice(10);

       Dice dice2 = new Dice(10);

      

       int roll1 = dice1.roll();

       int roll2 = dice2.roll();

      

       System.out.println("Roll1: "+roll1+", Roll2: "+roll2);

      

       if(roll1 < roll2) {

           System.out.println("Second player won");

       }else{

           System.out.println("First player won");

       }

   }

}

/*

Sample output:

Roll1: 2, Roll2: 10

Second player won

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote