Question

package week_4; import input.InputUtils; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Random; import static input.InputUtils.positiveIntInput; import static input.InputUtils.yesNoInput;...

package week_4;

import input.InputUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;

import static input.InputUtils.positiveIntInput;
import static input.InputUtils.yesNoInput;

/**

 Write a program to roll a set of dice. Generate a random number between 1 and 6 for
 each dice to be rolled, and save the values in an ArrayList.

 Display the total of all the dice rolled.

 In some games, rolling the same number on all dice has a special meaning.
 In your program, check if all dice have the same value, and print a message
 if all the dice show the same value.  In other words, you'll need a method that
 tests if all of the values in an ArrayList are the same.
 */

public class Question_2_Dice_Rolls_ArrayList {

    public final String SAME_VALUES = "All the dice have the same value!";

    Random rnd = new Random();   // Use this Random number generator in your code.

    public static void main(String[] args) {
        Question_2_Dice_Rolls_ArrayList dice = new Question_2_Dice_Rolls_ArrayList();
        dice.rollDice();
    }


    public void rollDice() {

        // How many dice to roll?
        int numberOfDice = positiveIntInput("How many dice would you like to roll?");

        // A do loop is similar to a while loop, but the condition is
        // checked at the end of one loop iteration.
        // So the set of dice are always rolled at least one time.

        do {

            // Roll the dice
            ArrayList<Integer> diceValues = roll(numberOfDice);

            // Print the dice values rolled
            System.out.println("The dice have the values: " + diceValues);
            System.out.println("The total of all dice: " + diceTotal(diceValues));

            if (allSameValue(diceValues)) {
                System.out.println(SAME_VALUES);
            }

        } while (yesNoInput("Do you want to roll again?"));
    }



    public ArrayList<Integer> roll(int numberOfDice) {

        // TODO Roll the given number of dice. Store the values in an ArrayList and return it.

        ArrayList<Integer>al = new ArrayList<Integer>();
        for(int i=0;i<numberOfDice;i++)
            al.add(rnd.nextInt(6)+1);
// TODO Roll the given number of dice. Store the values in an ArrayList and return it.
// Use the global Random rnd to generate random numbers
        return al;
        // Use the global Random rnd to generate random numbers

       }


    public int diceTotal(ArrayList<Integer> diceValues) {

        // TODO add up all of the values in the ArrayList and return this total.
        int sum=0;
        if(diceValues==null)
            return 0;
        for(Integer i:diceValues)
            sum+=i;
        // TODO this should still work for any number of dice in the diceValues ArrayList.
        // TODO if the diceValues ArrayList is empty or null, return 0 (zero). Hint: test if the ArrayList is null first

        return sum;  // Replace with your code.

    }


    public boolean allSameValue(ArrayList<Integer> diceValues) {

        // TODO return true if all of the values in the diceValues ArrayList are the same.

        if(diceValues==null)
            return false;
        int num = diceValues.get(1);
        for(int i=1 ;i<diceValues.size();i++)
            if(diceValues.get(i)!=num)
                return true;
        // TODO return false for an empty or null ArrayList.
        // TODO this method should work for 0 dice, 1 dice, 2 dice, 3 dice, 100 dice...
        // TODO if the diceValues ArrayList is empty or null, return false.

       return false; // Replace with your code

    }


}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
package week_4;

import input.InputUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;

import static input.InputUtils.positiveIntInput;
import static input.InputUtils.yesNoInput;

/**
 * Write a program to roll a set of dice. Generate a random number between 1 and 6 for
 * each dice to be rolled, and save the values in an ArrayList.
 * <p>
 * Display the total of all the dice rolled.
 * <p>
 * In some games, rolling the same number on all dice has a special meaning.
 * In your program, check if all dice have the same value, and print a message
 * if all the dice show the same value.  In other words, you'll need a method that
 * tests if all of the values in an ArrayList are the same.
 */

public class Question_2_Dice_Rolls_ArrayList {

    public final String SAME_VALUES = "All the dice have the same value!";

    Random rnd = new Random();   // Use this Random number generator in your code.

    public static void main(String[] args) {
        Question_2_Dice_Rolls_ArrayList dice = new Question_2_Dice_Rolls_ArrayList();
        dice.rollDice();
    }


    public void rollDice() {

        // How many dice to roll?
        int numberOfDice = positiveIntInput("How many dice would you like to roll?");

        // A do loop is similar to a while loop, but the condition is
        // checked at the end of one loop iteration.
        // So the set of dice are always rolled at least one time.

        do {

            // Roll the dice
            ArrayList<Integer> diceValues = roll(numberOfDice);

            // Print the dice values rolled
            System.out.println("The dice have the values: " + diceValues);
            System.out.println("The total of all dice: " + diceTotal(diceValues));

            if (allSameValue(diceValues)) {
                System.out.println(SAME_VALUES);
            }

        } while (yesNoInput("Do you want to roll again?"));
    }


    public ArrayList<Integer> roll(int numberOfDice) {
        ArrayList<Integer> al = new ArrayList<Integer>();
        for (int i = 0; i < numberOfDice; i++)
            al.add(rnd.nextInt(6) + 1);
        return al;
    }


    public int diceTotal(ArrayList<Integer> diceValues) {
        if (diceValues == null || diceValues.isEmpty()) {
            return 0;
        }
        int sum = 0;
        for (int i = 0; i < diceValues.size(); i++) {
            sum += diceValues.get(i);
        }
        return sum;
    }


    public boolean allSameValue(ArrayList<Integer> diceValues) {
        if (diceValues == null || diceValues.isEmpty()) {
            return false;
        }
        int num = diceValues.get(0);
        for (int i = 0; i < diceValues.size(); i++) {
            if (num != diceValues.get(i)) {
                return false;
            }
        }
        return true;
    }
}
Add a comment
Know the answer?
Add Answer to:
package week_4; import input.InputUtils; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Random; import static input.InputUtils.positiveIntInput; import static input.InputUtils.yesNoInput;...
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
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