You are welcome to use arrays, but please do not import or otherwise make use of
ID: 3808987 • Letter: Y
Question
You are welcome to use arrays, but please do not
import or otherwise make use of java.util.Arrays or call any method
from any library that operates on entire arrays. Use descriptive variable names. Use consistent indentation. Use standard Java naming conventions forvariableAndMethodNames, ClassNames, CONSTANT_NAMES. Include a reasonable amount of comments. Each 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.
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!
Explanation / Answer
InputOrGenerateDiceRolls.java
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.print("The five rolls in non-decreasing order are: ");
for(int i=0; i<dice.length; i++){
System.out.print(dice[i]+" ");
}
System.out.println();
}
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.print("The five rolls in non-decreasing order are: ");
for(int i=0; i<dice.length; i++){
System.out.print(dice[i]+" ");
}
System.out.println();
}
else {
System.out.println("Have a nice day!");
break;
}
}
}
private static void bubbleSort(int[] dice) {
/*
* In bubble sort, we basically traverse the array from first
* to array_length - 1 position and compare the element with the next one.
* Element is swapped with the next element if the next element is greater.
*
* Bubble sort steps are as follows.
*
* 1. Compare array[0] & array[1]
* 2. If array[0] > array [1] swap it.
* 3. Compare array[1] & array[2]
* 4. If array[1] > array[2] swap it.
* ...
* 5. Compare array[n-1] & array[n]
* 6. if [n-1] > array[n] then swap it.
*
* After this step we will have largest element at the last index.
*
* Repeat the same steps for array[1] to array[n-1]
*
*/
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 3 4 5 6
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!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.