Question

In: Computer Science

(please do the all questions. Thanks) A Dice Game a) You have been asked to create...

(please do the all questions. Thanks)

A Dice Game

a) You have been asked to create a Die class which will be used to represent dice in computer games.

The Die class should have the following fields:

  • sides. The number of sides the die will have.
  • value. The current value the die is displaying.

In addition, the class should have a following methods.

  • A Constructor which sets the number of sides and performs an initial roll of the die.
  • A roll method which is a void method that will simulate a random roll of the die.
  • Accessor methods for both of the fields.

b) Write a program that uses the Die class to play a simple dice game between the computer and the user. The program should create two instances of the Die class (each a 6-sided die). One Die object is the computer’s die, and the other Die object is the user’s die.

The program should have a loop that iterates 10 times. Each time the loop iterates, it should roll both dice. The die with the highest value wins. (In case of a tie, there is no winner for that particular roll of the dice.)

As the loop iterates, the program should keep count of the number of times the computer wins, and the number of times that the user wins. After the loop performs all of its iterations, the program should display who was the grand winner, the computer or the user.

Solutions

Expert Solution

Source Code:

public class PairOfDice {
     
        private int die1;   
        private int die2;   
        
        public PairOfDice() {
            roll();          }
        
        public void roll() {
               
            die1 = (int)(Math.random()*6) + 1;
            die2 = (int)(Math.random()*6) + 1;
        }
                 
        public int getDie1() {

           return die1;
        }
        
        public int getDie2() {
           return die2;
        }
        
        public int getTotal() {
           return die1 + die2;
        }
        
     }  

     
     

     public class Main {
     
        public static void main(String[] args) {
           
           PairOfDice dice;         
           int rollCount;   
     
           dice = new PairOfDice(); 
           rollCount = 0;
           
           
           do {
               dice.roll();
               System.out.println("The dice come up " + dice.getDie1() 
                                                + " and " + dice.getDie2());
               rollCount++;
           } while (dice.getTotal() != 2);
           
=           
           System.out.println("\nIt took " + rollCount + " rolls to get a 2.");
           
        }
        
     }  

Related Solutions

Please answer the following questions. All questions are separate, not related. Thanks You just graduated and...
Please answer the following questions. All questions are separate, not related. Thanks You just graduated and landed your first job in your new career. You remember that your favorite finance professor told you to begin the painless job of saving for retirement as soon as possible, so you decided to put away $5,000 at the end of each year in a Roth IRA. Your expected annual rate of return on the IRA is 6%. How much will you accumulate at...
IF YOU CAN NOT COMPLETE IT ALL DO NOT ANSWER THE QUESTIONS (THANKS) Develop a tentative...
IF YOU CAN NOT COMPLETE IT ALL DO NOT ANSWER THE QUESTIONS (THANKS) Develop a tentative plan of care for a client who has bipolar disorder. Include: -Nursing Diagnosis - Assessment Data (objective and subjective ) - Goal and outcome' - Nursing Intervention - Rationale - Outcome evaluation and replanning Please type your answer do not scan thanks
Please show all work!! You have been asked by the president of your company to evaluate...
Please show all work!! You have been asked by the president of your company to evaluate the proposed acquisition of a new spectrometer for the firm’s R&D department. The equipment’s basic price is $70,000 and it would cost another $15,000 to modify it for special use by your firm. The spectrometer, which has a MACRS 3-year recovery period, would be sold after 3 years for $30,000. Use of the equipment would require an increase in net working capital (spare parts...
You have been asked to create a program that builds a tower base on the number...
You have been asked to create a program that builds a tower base on the number of items given as input. When the only 1 item is given you create no tower you just place the item on top of the stand. When 2 items are given you build the base of the tower with one foot to the right and one foot to the left. The feet will be in the ground and support the tower. For every additional...
Please answer all questions, thanks 1. Money is defined by economists as: A. All stores of...
Please answer all questions, thanks 1. Money is defined by economists as: A. All stores of value. B. Any liquid asset. C. Legal tender. D. Anything that performs the functions of money. E. Currency and coin. 2. The primary form of money in the modern world is: A. Household and business liabilities. B. The debt issued by the Federal government. C. Coins. D. The deposit liabilities of banks. E. Paper currency. 3. Commodity money is: A. A money that never...
You have been asked to create a python program that will ask the user how many...
You have been asked to create a python program that will ask the user how many tickets the user would like to purchase for graduation. The user will then choose the number of tickets needed and the program will list the cost of the tickets, a service fee, tax, and the total of the purchase. Specifications: The program should only accept whole numbers when receiving input for the number of tickets needed. No calculations should be performed if input is...
Imagine you work in a correctional institution and have been asked to create a strategy plan...
Imagine you work in a correctional institution and have been asked to create a strategy plan for working with specific correctional populations. Your supervisor would like you to create an executive summary of your strategy. Select 1 correctional population to focus on. Write a 525- to 700-word executive summary in which you: Describe elements in the classification process. Explain the importance of proper classification. Discuss what challenges are associated with special populations, such as individuals with a mental illness and...
Please answer and explain questions 1 to 4 from this problem thanks. Class: Game Theory Problem...
Please answer and explain questions 1 to 4 from this problem thanks. Class: Game Theory Problem 1 Two players, A and B play the following game. First Player A must choose IN or OUT. If Player A chooses OUT the game ends, and the payoffs are Player A gets 4, and Player B gets 0. If Player A chooses IN then Player B observes this and must then choose in or out. If Player B chooses out the game ends,...
Suppose you are playing a dice game and you have three options to find a score....
Suppose you are playing a dice game and you have three options to find a score. The options are: A. Rolling an 11-sided die and using the outcome as your score.* B. Rolling two 4-sided dice, adding 1 to their sum, and using that number as your score. C. Rolling two 4-sided dice, doubling the result of the first die and adding it to the result of the second, subtracting 1 from this result, and using this number as your...
Suppose you are playing a dice game and you have three options to find a score....
Suppose you are playing a dice game and you have three options to find a score. The options are: Rolling an 11-sided die and using the outcome as your score.* Rolling two 4-sided dice, adding 1 to their sum, and using that number as your score. Rolling two 4-sided dice, doubling the result of the first die and adding it to the result of the second, subtracting 1from this result, and using this number as your score. Assume that each...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT