QuestionQuestion

Transcribed TextTranscribed Text

This program will review many programming concepts in Java. You will create instances of classes to use (both Java built-in and other classes) and use if statements, loops (2 kinds), constants, arithmetic operations, typecasting, and escape characters. Be sure your program is named DiceGame.java. The problem: Suppose someone comes to you and says “I have a 6-sided Die and different one that has 4 sides. I would like to propose a game. We will roll both dice and I will give you $2.00 if the total is less than 5 and $8.00 if the total is exactly equal to 5. But if the total is greater than 5, you give me $3.00.” Should you do it? The Math behind this: This is called Expected Value; in Statistics, the Expected Value of a probability distribution is the value you would “expect” to get if you did an experiment multiple times and then averaged the results. The formula for it is: ExpectedValue   (eachpossibleresult)(itsprobability) In this case, eachpossibleresult is that you can either: Win $2.00 (+2) or Win $8.00 (+8) or Lose $3.00 (-2) You can calculate the probabilities by looking at the chances of rolling certain numbers. Since the first die has 6 sides and the second has 4 sides, the all possibilities are the numbers 2-10, shown on the inside of the table below. First die result 1 2 3 4 5 6 1 2 3 4 5 6 7 2 3 4 5 6 7 8 3 4 5 6 7 8 9 second die result 4 5 6 7 8 9 10 You can see that there are 24 possible results. The probability of getting a 8 is 24 3 , the probability of getting a 8 is 24 2 , etc. So you can calculate the probability of getting a number less than 5, equal to 5, and greater than 5 and then use the formula to figure out the Expected Result: A side note: The same concept can be used for lottery tickets. If you pay $1 for a lottery ticket, most of the time you will lose your $1. Occasionally you will win some money and there is an extremely small probability that you will win a large jackpot. If you calculated the Expected Value of your lottery ticket by multiplying (-1)(prob. of winning nothing) + ($2-$1you paid)(prob. of winning $2) + …. + ($1,000,000-$1you paid)(the very small prob. of winning $1,000,000), the “Expected Value” of your lottery ticket would be something like -40 cents. Your program will simulate this: You are given a class called Die, which has a default constructor (makes it 6-sided) and has a parameterized constructor that accepts a number of sides (minimum 4 sides or else it will throw an exception). It also has a method to roll itself and a method to return the number of sides it has. You will write a program called DiceGame.java that simulates running the game over and over by doing the following: Ask the user how many sides the second die should have; store the response. Ask the user to enter their name – minimum 5 characters – and store the response. The name can have spaces in it. (NOTE: be sure to ask for the number of sides and THEN the name in that order). As long as (“while”) the user enters the name incorrectly, tell them it’s wrong and ask them to re-enter it. Create a 6-sided die (using its default constructor) Create a die with as many sides as the user entered (using its parameterized constructor) Skip a line (use \n in your next output to do this) and then print “Experiment by: “ and then the first 5 characters of the name. Tell both dice to roll themselves 50,000 times. As this is happening,  At every roll, add up the dice and keep track of how much money you have won or lost since the game began.  Every 5,000 rolls, print how many rolls have occurred, then a tab, then the total money won/lost, then a tab, then the average money won/lost (total divided by number of rolls) As additional requirements, you are to use named constants for numbers that do not change during your experiment, but might later be altered during a different experiment. There are at least 5 numbers that could be named constants; you must make at least 2 of them be named constants (they should also be all uppercase and declared first). Example: Please enter the number of sides on the second die: 4 Please enter your name (minimum 5 characters): Joe name must be at least 5 characters long. Please reenter: Pete name must be at least 5 characters long. Please reenter: Justin Bieber Experiment by: Justi 5000 529 0.1058 10000 874 0.0874 15000 1531 0.10206666666666667 20000 1921 0.09605 25000 2147 0.08588 30000 2328 0.0776 35000 2979 0.08511428571428571 40000 3953 0.098825 45000 4460 0.09911111111111111 50000 4785 0.0957

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.util.Scanner;


public class DiceGame {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
      
       // constant
       //There are at least 5 numbers that could be named constants;
       // you must make at least 2 of them be named constants
       // (they should also be all uppercase and declared first).
       final int MIDDLE_VALUE = 5;
       final int LESSER_QUOTE = 2;
       final int EQUALLY_QUOTE = 8;
       final int LARGER_QUOTE = -3;
       final int CHECKING_POINT = 5000;
       final int FINISHED_POINT = 50000 ;
      
       // input stream
       Scanner keyboard = new Scanner(System.in);
      
       // Ask the user how many sides the second die should have; store the response.
         System.out.print("Please enter the number of sides on the second die: ");
       int nunber = Integer.parseInt(keyboard.nextLine());...
$32.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats