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 } }
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; } }
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;...
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...
import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...
complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */ public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null;...
JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet { /** * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList * containing all the indexes of Strings in arr that match String s. For this method, two Strings, * p and q, match when p.equals(q) returns true or if both of the compared references are null. * * @param s The string...
the RollDie.java: import java.util.Random; import java.util.Scanner; /* * HEADER HERE !! */ public class RollDie { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); // Step 1: Declare and initialize array and // initialize random number generator int[] rollValueCounts = new int[7]; Random randGen = new Random(); // Step 2: Determine the number of rolls System.out.print("How many times do you want to roll the die?"); System.out.print(" [Max value is 100] "); int numRolls = scnr.nextInt(); // TODO:...
complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */ public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null; //TODO...
package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private static int cardNumber[] = {1,2,3,4,5,6,7,8,9,10,11,12,13}; private static char suitName[] = { 'c', 'd', 'h', 's' }; public static void main(String[] args) throws CardException, DeckException, HandException { Scanner kb = new Scanner(System.in); System.out.println("How many Players? "); int numHands = kb.nextInt(); int cards = 0; if (numHands > 0) { cards = 52 / numHands; System.out.println("Each player gets " + cards + " cards\n"); } else...
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...
complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Finds the specified set of words in the specified file and returns them * as an ArrayList. This finds the specified set in the file which is on the * line number of the set. The first line and set in the file is 1. * * This returns an ArrayList with the keyword first, then : and then followed...
my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first current number in main method but when I store after 10 second i'm not getting the update current number. I would appreciate any help. NOTE: ***I want the Runnable to be in the same class, Not in main method. my goal...