Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class called InputOrGenerateDiceRolls. The program should ask the user w

ID: 3806888 • 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. 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

InputOrGenerateDiceRolls.java

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;


public class InputOrGenerateDiceRolls {
   public static void main(String[] args) {
       final int NUM_OF_DICES = 5;
   Scanner scan = new Scanner(System.in);
   int dice[] = new int[NUM_OF_DICES];
   while(true){
   System.out.print("Please enter 1) to roll your own dice, 2) to let the computer roll the dice, or 3) to quit: ");
   int choice = scan.nextInt();
   if(choice == 1){
       System.out.println("Please enter the five dice rolls: ");
       for(int i=0; i<NUM_OF_DICES ; i++){
           dice[i]=scan.nextInt();
       }
       bubbleSort(dice);
       System.out.println("The five rolls in non-decreasing order are: "+Arrays.toString(dice));
   }
   else if(choice == 2){
       Random r = new Random();
       for(int i=0; i<NUM_OF_DICES ; i++){
           dice[i]=r.nextInt(6)+1;
       }
       bubbleSort(dice);
       System.out.println("The five rolls in non-decreasing order are: "+Arrays.toString(dice));
   }
   else {
       System.out.println("Have a nice day!");
       break;
   }
   }
   }
   private static void bubbleSort(int[] dice) {

  
  
int n = dice.length;
int temp = 0;
  
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
  
if(dice[j-1] > dice[j]){
//swap the elements!
temp = dice[j-1];
dice[j-1] = dice[j];
dice[j] = temp;
}
  
}
}

}
}

Output:

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: [1, 1, 1, 4, 4]
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!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote