Coding Style: In java, use descriptive variable names. Use consistent indentatio
ID: 3801546 • Letter: C
Question
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.
1. Write a class called OneMoreDigit. The program should prompt the user to enter a string consisting of zero or moredigits in non-decreasing order separated by spaces – be sure to read this in as a single string. Non-decreasing order means “sorted” with duplicates allowed. For example, the user might enter the string “2 2 5 8” (of course the user does not type the quotes). Or the user might enter the empty string (by simply pressing the “enter” key at the prompt) since he/she was prompted to enter “zero or more digits in non-decreasing order.” Also, the user can enter the letter q by itself to quit the program.
Next, your program should generate a random digit and inform the user what digit was generated. For example, if the random digit was a 5 then program might display “The following digit was randomly generated at great expense: 5”.
Next, the program should construct a string identical to the string that the user entered but with the randomly-generateddigit inserted into a correct position so that the resulting string is still in non-decreasing order. For example, if the user entered “2 2 5 8” and the randomly-generated digit was 5, then the new string should be “2 2 5 5 8”. Of course if the user entered the empty string and the randomly-generated digit was 7, then the new string would just be “7”. The program should output the new string.
Be sure to test your program thoroughly.
Here is an example run of the program.
Please enter zero or more digits in non-decreasing order (or q to quit): 2 2 5 8
The following digit was randomly generated at great expense: 5
Here is the new sequence including the random digit: 2 2 5 5 8
Please enter zero or more digits in non-decreasing order (or q to quit):
The following digit was randomly generated at great expense: 7
Here is the new sequence including the random digit: 7
Please enter zero or more digits in non-decreasing order, (or q to quit): q
Have a nice day!
Explanation / Answer
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class OneMoreDigit {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String userInput = "";
for (;;) {
System.out
.println("Please enter zero or more digits in non-decreasing order(or q to quit)");
userInput = s.nextLine();
if ("q".equalsIgnoreCase(userInput)) {
break;
} else {
String randomGeneratedNo = String.valueOf(randInt(1, 9));
System.out
.println("The following digit was randomly generated at great expense :"
+ randomGeneratedNo);
if (userInput.isEmpty()) {
System.out
.println("Here is the new sequence including the random digit :"
+ randomGeneratedNo);
} else {
// split the space separated string into an array
String[] temp = userInput.split(" ");
String dest[] = new String[temp.length + 1];
String newSequnece = "";
int k = 0;
while (k < temp.length) {
if (Integer.parseInt(temp[k]) < Integer
.parseInt(randomGeneratedNo)) {
dest[k] = temp[k];
k++;
} else {
break;
}
}
if (k == temp.length) {
dest[k] = randomGeneratedNo;
} else {
System.out.println("k is:" + k);
dest[k] = randomGeneratedNo;
System.arraycopy(temp, k, dest, k + 1, temp.length - k);
}
for (String x : dest) {
newSequnece = newSequnece + " " + x;
}
newSequnece = newSequnece.trim();
System.out
.println("Here is the new sequence including the random digit: "
+ newSequnece);
}
}
}
s.close();
}
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}