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

You have been asked to create a list of all MAC addresses and corresponding IP addresses...
You have been asked to create a list of all MAC addresses and corresponding IP addresses and computer names in your network. Propose at least two methods for performing this task. Your network has almost 100 computers in a Windows Server 2016 domain network with statically assigned IP addresses. Using the tools available in Windows Server 2016, carry out the procedure you think will work best. Write a short report of your results and submit it to your instructor.
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...
Imagine that you are a corporate trainer at BP Amoco. You have been asked to create...
Imagine that you are a corporate trainer at BP Amoco. You have been asked to create a training program by your manager, but she needs to understand how you will determine the content for the training program as well as success of it. Your presentation should be compelling while including the follow components: • various evaluation designs that are possible at BP Amoco This is your opportunity to display and apply your knowledge by responding to a potential client engagement....
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...
  Python-“Ship Dice” Ship Dice: Ship Dice is a game that you will be creating. The rules...
  Python-“Ship Dice” Ship Dice: Ship Dice is a game that you will be creating. The rules of the game are fairly simple. The game is normally played with 2-6 people, but our version will only be one player who is playing. The rules are simple, player rolls 3 dice randomly (Red, Green, and Blue), and the player has couple of options to choose from to win: 1- 3 chances to roll the dice randomly and get all dices 6 2-...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT