Write a class called InputOrGenerateDiceRolls. The program should ask the user w
ID: 3804640 • Letter: W
Question
Write a class called InputOrGenerateDiceRolls. The program should ask the user whether the user prefers 1) to roll his/her own dice, 2) use computer-generated dice rolls, or 3) quit the program. If the user prefers to roll his/her own dice, the program should prompt the user to enter 5 digits in the range from 1-6 separated by spaces in any order with duplicates allowed. However, if the user prefers to use computer-generated dice rolls, generate 5 random digits in the range from 1-6 with duplicates allowed. Next, regardless of the user’s preference, display the 5 digits (representing 5 dice rolls) in non-decreasing order.
Be sure to test your program thoroughly.
Here is an example run of the program.
Please enter 1) to roll your own dice, 2) to let the computer roll the dice, or 3) to quit: 1
Please enter the five dice rolls: 4 2 6 1 1
The five rolls in non-decreasing order are: 1 1 2 4 6
Please enter 1) to roll your own dice, 2) to let the computer roll the dice, or 3) to quit: 2
The five rolls in non-decreasing order are: 2 4 4 5 5
Please enter 1) to roll your own dice, 2) to let the computer roll the dice, or 3) to quit: 3
Have a nice day!
Coding Style: In java, use descriptive variable names. Use consistent indentation. Only import java.util.Scanner to solve the problem. Use standard Java naming conventions forvariableAndMethodNames, ClassNames, CONSTANT_NAMES. Include a reasonable amount of comments. Create a class by a specific name. The class should have a main method. You are also encouraged (but not required) to write “helper” methods for each class which “help” the main method by breaking the problem into smaller pieces. Please do not call any methods in the Java class library specifically intended for sorting.
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class InputOrGenerateDiceRolls {
public InputOrGenerateDiceRolls() {
Scanner sc = new Scanner(System.in);
while (true) {
displayMenu();
int choice = Integer.valueOf(sc.nextLine());
switch (choice) {
case 1 :
rollOwnDice(sc);
break;
case 2 :
autoRollDice();
break;
case 3 :
sc.close();
System.out.println("Have a nice day!");
return;
default :
System.out.println("Wrong option");
}
}
}
/**
* Method to roll your own dice
* Takes valid input from user, sorts it and displays
*
* @param scanner
*/
private void rollOwnDice(Scanner sc) {
System.out.print("Please enter five dice rolls : ");
String[] splittedVals = sc.nextLine().split(" ");
int[] rolls = new int[5];
for (int i=0; i<5; i++) {
rolls[i] = Integer.valueOf(splittedVals[i]);
if (rolls[i] <1 && rolls[i] >6) {
System.out.println("Invalid face value!");
return;
}
}
sort(rolls);
printRolls(rolls);
}
/**
* Automatic Roll Dice, using random number generator
*/
private void autoRollDice() {
Random random = new Random();
int[] rolls = new int[5];
for (int i=0; i<5; i++) {
rolls[i] = random.nextInt(6)+1;
}
sort(rolls);
printRolls(rolls);
}
/**
* printing the given array
* @param rolls
*/
private void printRolls(int rolls[]) {
for (int i=0; i<rolls.length; i++) {
System.out.print(rolls[i] + " ");
}
System.out.println();
}
/**
* Menu Details
*/
private void displayMenu() {
System.out.println(System.lineSeparator());
System.out.println("Menu");
System.out.println("1. Roll your own dice : ");
System.out.println("2. Let Computer roll the dice : ");
System.out.println("3. Quit : ");
System.out.print("Enter your choice : ");
}
/**
* Selection sort method to sort the given array
*
* @param arr
*/
private void sort(int arr[]) {
for (int i=0; i<arr.length-1; i++) {
int index = i;
for (int j=i+1; j<arr.length; j++) {
if (arr[j] < arr[index]) {
index = j;
}
}
int small = arr[index];
arr[index] = arr[i];
arr[i] = small;
}
}
public static void main(String args[]) {
new InputOrGenerateDiceRolls();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.