Question

Write a class named DiceGame.java that contains a method called diceSum that prompts the use for...

Write a class named DiceGame.java that contains a method called diceSum that prompts the use for a desired sum, then repeatedly simulates the rolling of 2 six-sided dice until their sum is the desired sum (use a do/while loop). Here is a sample dialogue with the user:
Desired dice sum: 9
4 and 3 = 7
3 and 5 = 8
5 and 6 = 11
5 and 6 = 11
1 and 5 = 6
6 and 3 = 9
You must perform checks to make sure the user enters an integer between 2 and 12 and reprompt if not. You must also reprompt for non-int values. Your code should include assertion comments for assertions that are always true (provable).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

class Solution

{

    public static void main(String[] args)

    {

        // create object to get user input

        Scanner sc = new Scanner(System.in);

       

        int sum;

       

        // infinite loop

        while(true)

        {

            System.out.println("Desired dice sum:");

           

            // get user input

            sum = sc.nextInt();

           

            // if the entered sum is not correct

            if( sum >= 2 && sum <= 12 )

                break;

           

            System.out.println("Entered value is not correct.\n");

        }

       

        int x, y;

       

        // loop untill the sum is not equal to sum

        do{

            // generate random number in range 1 - 6

            x = 1 + (int)(Math.random() * ((6 - 1) + 1));

           

            // generate random number in range 1 - 6

            y = 1 + (int)(Math.random() * ((6 - 1) + 1));

           

            System.out.println(x + " and " + y + " = " + (x + y));

        }

        while( x + y != sum );

    }

}


Sample Output

C:\Users\Rishab\Desktop javac Solution.java C:NUsers\Rishab\Desktop java Solution Desired dice sum 5 land 5-6 3 and 47 3 and

Add a comment
Know the answer?
Add Answer to:
Write a class named DiceGame.java that contains a method called diceSum that prompts the use for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE complete each task in python Write a console program that prompts the user for the...

    PLEASE complete each task in python Write a console program that prompts the user for the desired sum, then repeatedly rolls two six-sided dice (using a Random or RandomGenerator object to generate random numbers from 1-6) until the sum of the two dice values is the desired sum. Here is the expected dialogue with the user: Desired sum: 9 4 and 3 = 7 3 and 5 = 8 5 and 6 = 1 1 5 and 6 = 11...

  • question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user...

    question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user to enter the number of times n to roll a six-sided die. The program should print to the screen the iteration of the loop and result for each roll 1 ) How many times would you like to roll? 3 roll 6 6 5 1 2) PE 05b 02 (Flash Cards) Write a counter-controlled program that creates addition problems for an elementary kid to...

  • This problem investigates how you can use random numbers to simulate a computer dice game write a function called twooice that simulates the rolling of two sik-sided dice. The function takes no input...

    This problem investigates how you can use random numbers to simulate a computer dice game write a function called twooice that simulates the rolling of two sik-sided dice. The function takes no inputs. Instead, it generates two random integers between 1 and 6, and output their sum. You may submit your work as many times as you want Try to get 100%) Your Function MATLAB Documentation Reset Code to call your function C Reset 1s-tuoDice ss-twoDice This problem investigates how...

  • in java Write an application that: 1. Prompts the user for a number of trials to...

    in java Write an application that: 1. Prompts the user for a number of trials to perform (I will call this "N"). This operation should be type-safe for integers. 2. Simulates N trials of rolling two dice and finding the sum. 3. Uses an array to keep track of how many times each outcome has been rolled (you do not need to keep track of the actual rolls themselves). 4. Computes the percentage of the total number of rolls for...

  • Make a public class called ManyExamples and in the class... Write a function named isEven which...

    Make a public class called ManyExamples and in the class... Write a function named isEven which takes in a single int parameter and returns a boolean. This function should return true if the given int parameter is an even number and false if the parameter is odd. Write a function named close10 which takes two int parameters and returns an int. This function should return whichever parameter value is nearest to the value 10. It should return 0 in the...

  • 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field ...

    12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field .A String named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up The Coin class should have the following methods .A no-arg constructor that randomly determines the side of the coin that is facing up (heads" or "tails") and initializes the aideUp field accordingly .A void method named toss that simulates the...

  • need help!! c++ HW_6b - Calculate the average Use a do-while loop Write a program that...

    need help!! c++ HW_6b - Calculate the average Use a do-while loop Write a program that first prompts the user for an upper limit: Enter the number of entries: 5 € (user enters 5) After the user enters a number, the program should prompt the user to enter that many numbers. For example, if the user enters 5, then the program will ask the user to enter 5 values. Use a do-while loop to add the numbers. o With each...

  • Write a program named "VegetablePricer" which prompts the user for a vegetable from the pricing table...

    Write a program named "VegetablePricer" which prompts the user for a vegetable from the pricing table shown below. The program will then display the cost per pound for that vegetable, again using the table provided. Your program must use a switch statement to process the input value and display the desired output. Be sure to provide a default case in your switch statement which handles unrecognized input. Use named constants for all constant strings (e.g. prompts and vegetable names) and...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

    ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT