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

java eclipse write a class called Dice. The class has only one data member.lengt

ID: 3609850 • Letter: J

Question

java eclipse

write a class called Dice. The class has only one data member.length (int). Provide set and get fir the data member. Also providea method that returns randomly a number from 1 to the length of thedice. Write a test "main" for this class that creates a gamebetween two players and uses two dices. Each player through thedices 5 times. The one who gets more, wins.


thanks,,

Explanation / Answer

/* This code asks for the value of length1 and length2 Enter integer values for length1 and length2 */ import java.io.*; import java.util.*; import java.lang.*; public class Dice{ private int length; /* get and set methods for length */ public int getLength(){ return this.length; } public void setLength(int length) { this.length = length; } public int randomGenerator() { Random r = new Random();    int value = r.nextInt(this.getLength()) + 1;    //System.out.println(value);    return value; }    public static void main(String args[]){ Dice dice1 = new Dice(); Dice dice2 = new Dice(); System.out.print("Enter the length for dice1: "); int length1=0; try{     length1 = Integer.parseInt (newBufferedReader(new InputStreamReader(System.in)).readLine()); } catch (Exception e){     e.printStackTrace(); } dice1.setLength(length1); System.out.println(); System.out.print("Enter the length for dice2: "); int length2=0; try{     length2 = Integer.parseInt (newBufferedReader(new InputStreamReader(System.in)).readLine()); } catch (Exception e){     e.printStackTrace(); } dice2.setLength(length2); System.out.println(); int player1 = 0; int player2 = 0; for(int i =0; i player2) System.out.println("Player1 wins"); if(player2 > player1) System.out.println("Player2 wins"); if(player1 == player2) System.out.println("Player1 andPlayer2 have the same score"); }//end of main } //end of Dice class