Question

public static List roll(int numberOfDice) { //line 28 static Random rnd = new Random(); //Use the...

public static List roll(int numberOfDice) { //line 28 static Random rnd = new Random();

//Use the Random rnd variable declared on line 28 to generate random numbers. // Don't create another Random object. // // // TODO create an ArrayList of Integer values. // TODO Roll the given number of dice. Store the values in an ArrayList and return it. return null; // TODO Replace with your code } public static int diceTotal(List diceValues) { // TODO if the diceValues List is null, return 0. (hint: do this check first) // TODO if the diceValues List is empty, return 0. // TODO add up all of the values in the List and return this total. // TODO this should still work for any number of dice in the diceValues List. return 0; // TODO Replace with your code. } public static boolean allSameValue(List diceValues) { // TODO if the diceValues List is null, return false. (hint: do this check first) // TODO if the diceValues List is empty, return false // TODO return true if all of the values in the diceValues List are the same. // TODO this method should work for 0 dice, 1 dice, 2 dice, 3 dice, 100 dice... return false; // TODO Replace with your code } }

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

CODE

public static List<Integer> roll(int numberOfDice) { //line 28 static

Random rnd = new Random();

List<Integer> res = new ArrayList<>();

for (int i=0; i<numberOfDice; i++) {

res.add(rnd.nextInt(6) + 1);

}

return res;

}

public static int diceTotal(List<Integer> diceValues) {

if (diceValues == null) {

return 0;

}

int sum = 0;

for (int el : diceValues) {

sum += el;

}

return sum;

}

public static boolean allSameValue(List<Integer> diceValues) {

if (diceValues == null) {

return false;

}

for (int i=1; i<diceValues.size(); i++) {

if (diceValues.get(i) != diceValues.get(i-1)) {

return false;

}

}

return false;

}

Add a comment
Know the answer?
Add Answer to:
public static List roll(int numberOfDice) { //line 28 static Random rnd = new Random(); //Use the...
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
  • 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,...

  • Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {...

    Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {            if(arr == null || arr.length == 0) {                return -1;            }            for (int i = 0; i < arr.length; i++) {                if(arr[i] == ch) {                    return i;                }            }        return -1;       ...

  • Hello, I've been working on this for a while and I can't figure the rest out....

    Hello, I've been working on this for a while and I can't figure the rest out. Need asap if anyone can help. maxBSt,minBST,isBST, and inOrder package lab7; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; public class TreeExercise {    /*    * Construct BST from preorder traversal    */    public static Node<Integer> consBSTfromPreOrder(int[] arr, int start, int end)    {                       if(start > end) return null;               Node<Integer> root = new Node<Integer>(arr[start],...

  • The following code skeleton contains a number of uncompleted methods. With a partner, work to complete...

    The following code skeleton contains a number of uncompleted methods. With a partner, work to complete the method implementations so that the main method runs correctly: /** * DESCRIPTION OF PROGRAM HERE * @author YOUR NAME HERE * @author PARTNER NAME HERE * @version DATE HERE * */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class ArrayExercises { /** * Given a random number generator and a length, create a new array of that * length and fill it from...

  • static public int[] Question() // retrieve an integer 'n' from the console // the first input...

    static public int[] Question() // retrieve an integer 'n' from the console // the first input WILL be a valid integer, for 'n' only. // then read in a further 'n' integers into an array. // in the case of bad values, ignore them, and continue reading // values until enough integers have been read. // this question should never print "Bad Input" like in lab // hint: make subfunctions to reduce your code complexity return null; static public int...

  • Can someone help me with the writing of this code please? public static int promptInt(Scanner input,...

    Can someone help me with the writing of this code please? public static int promptInt(Scanner input, String prompt, int min, int max) { return 0; //TODO replace }    /**    * Returns the index within arr of the first occurrence of the specified character.    * If arr is null or 0 length then return -1. For all arrays, don't assume a length    * but use the array .length attribute.    * @param arr The array to look...

  • import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class ListPractice {       private static final int[]...

    import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class ListPractice {       private static final int[] arr = new int[100000];    public static void main(String[] args) {        for(int i=0; i<100000; i++)            arr[i] = i;               //TODO comment out this line        LinkedList<Integer> list = new LinkedList<Integer>();               //TODO uncomment this line        //List<Integer> list = new ArrayList<Integer>();               //TODO change the rest of the...

  • For this lab you will write a Java program that plays the dice game High-Low. In...

    For this lab you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totaling 8 or higher), Low (totaling 6 or less) or Sevens (totaling exactly 7). If the player wins, they receive a payout based on the schedule given in the table below: Choice Payout ------ ------ High 1 x Wager Low 1 x Wager Sevens 4 x...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • public class Node { public int data; public Node next; public Node(int d, Node n) {...

    public class Node { public int data; public Node next; public Node(int d, Node n) { data = d; next = n; } } Please complete the following 5 tasks. 1./** * * @param start * @param low * @param high * @return the sum of all values starting at start that are * in the range [low...high] (inclusive on both sides). 0 if none. * start -> 10 -> 70 -> 20 -> 80 -> null * low =...

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