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

Java Code A Game of 21 Write a program that uses the Die class that was presente

ID: 674361 • Letter: J

Question

Java Code

A Game of 21
Write a program that uses the Die class that was presented in Chapter 4 to write a program
that lets the user play against the computer in a variation of the popular blackjack card
game. In this variation of the game, two six-sided dice are used instead of cards. The dice
are rolled, and the player tries to beat the computer’s hidden total without going over 21.
Here are some suggestions for the game’s design:
Each round of the game is performed as an iteration of a loop that repeats as long as
the player agrees to roll the dice, and the player’s total does not exceed 21.
At the beginning of each round, the program will ask the user whether he or she wants
to roll the dice to accumulate points.

During each round, the program simulates the rolling of two six-sided dice. It rolls
the dice first for the computer, and then it asks the user if he or she wants to roll. (Use
the Die class that was demonstrated in Chapter 4 to simulate the dice).
The loop keeps a running total of both the computer and the user’s points.
The computer’s total should remain hidden until the loop has finished.
After the loop has finished, the computer’s total is revealed, and whoever the player
with the most points without going over 21 wins.

Die Class
import java.util.Random;

/**
* The Die class simulates a six-sided die.
*/

public class Die
{
   private int sides;   // Number of sides
   private int value;   // The die's value
  
   /**
    * The constructor performs an initial
    * roll of the die. The number of sides
    * for the die is passed as an argument.
    */
  
   public Die(int numSides)
   {
      sides = numSides;
      roll();
   }
  
   /**
    * The roll method simlates the rolling of
    * the die.
    */
  
   public void roll()
   {
      // Create a Random object.
      Random rand = new Random();
     
      // Get a random value for the die.
      value = rand.nextInt(sides) + 1;
   }
  
   /**
    * The getSides method returns the
    * number of sides for the die.
    */
  
   public int getSides()
   {
      return sides;
   }
  
   /**
    * The getValue method returns the
    * value of the die.
    */
  
   public int getValue()
   {
      return value;
   }
}

/**
* This program simulates the rolling of dice.
*/

public class DiceDemo
{
   public static void main(String[] args)
   {
      final int DIE1_SIDES = 6; // Number of sides for die #1
      final int DIE2_SIDES = 12; // Number of sides for die #2
    
      // Create two instances of the Die class.
      Die die1 = new Die(DIE1_SIDES);
      Die die2 = new Die(DIE2_SIDES);
    
      // Display initial information.
      System.out.println("This simulates the rolling of a " +
                         DIE1_SIDES + " sided die and a " +
                         DIE2_SIDES + " sided die.");

      // Roll the dice.
      System.out.println("Rolling the dice.");
      die1.roll();
      die2.roll();
       
      // Display the values of the dice.
      System.out.println(die1.getValue() + " " +
                         die2.getValue());
   }
}

This is the code given

Explanation / Answer

Methods to support program:

Main Program:

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