Question: Complete the following JAVA program: import java.util.Scanner; // Please do not use any other library public class A9 { // Start of main method public
Complete the following JAVA program:
import java.util.Scanner; // Please do not use any other library
public class A9 { // Start of main method public static void main (String [] args) { Scanner input = new Scanner(System.in); // Ask the user for the size of the array System.out.println ("This program will create a list of random numbers."); System.out.println ("How many random numbers would you like in your list?"); int size = input.nextInt();
// Create an array with the size entered by the user. int[] randomNumbers = new int[size];
// Populate the array with random numbers. populateArray(randomNumbers);
// Print the contents of the array numbers. printArray(randomNumbers); System.out.println(" ------------");
// Get a new array topTen, returned by the method getTopTen() int[] topTen = getTopTen(randomNumbers);
// Display the contents of the array topTen ; printArray(topTen); System.out.println(" ------------");
// Ask the user for 2 lucky numbers. System.out.println("Enter 2 lucky numbers between 1 and 10: "); byte num1 = input.nextByte(); byte num2 = input.nextByte();
// Calls checkLuckyNumbers() to check if the user's numbers appear next to // each other on the array topTen, or on the array numbers. if (checkLuckyNumbers(topTen, num1, num2))System.out.println("Top Ten Winner!"); else if (checkLuckyNumbers(randomNumbers, num1, num2))System.out.println("Winner!"); elseSystem.out.println("Sorry, not a winner.");
System.out.println(" ------------");
// Let's now get an array with single-digit numbers only. // Call replaceNumbers(), passing a valid range. // Any numbers out of range will be replaced with 0. // Print the contents of this new array returned by replaceNumbers().
System.out.println("Removing double-digit numbers from the list..."); int[] singleDigits = replaceNumbers(randomNumbers, 0, 9); printArray(singleDigits); System.out.println(" ------------");
// Prints the original array of random numbers System.out.println("The original list again..."); printArray(randomNumbers); System.out.println(" ------------");
// Populates an array frequencies with the total number of times // each digit (from 0 to 9) appears in the array singleDigits. int[] frequencies = countDigits(singleDigits);
// Prints the array with the counts (frequency distribution) System.out.println("The number of times each digit appears is: "); printArray(frequencies); System.out.println(" ------------");
// Prints a graph using asterisks (to display the frequency distribution) printGraph(frequencies); } // End of main() method
// ---- Other Methods ----
// populateArray takes an array of integers as a parameter, and // populates this array with random numbers (between 1 and 15). public static void populateArray(int[] numbers) { for (int i = 0; i < numbers.length; i++) { numbers[i] = (int)(Math.random() * 15) + 1; } }
// printArray takes an array of integers as a parameter and // displays each element in this array, separated by spaces. public static void printArray(int[] numbers) { for (int i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + " "); } }
// getTopTen takes an array and returns a new array with only the first 10 elements in the given array. public static int[] getTopTen(int[] numbers) {
// do not return numbers... this is just a placeholder (so that this compiles). // change "numbers" below to the name of your new array that has only 10 numbers. return numbers; }
// checkLuckyNumbers searches the given array for the occurrence of the 2 given numbers // Returns true if it finds the 2 numbers next to each other in the array. public static boolean checkLuckyNumbers(int[] numbers, byte num1, byte num2) { boolean found = false; return found; }
// replaceNumbers takes an array of integers, and 2 integers as parameters. // It returns a new array where all numbers < minNumber or > maxNumber have been replaced with 0. public static int[] replaceNumbers(int[] numbers, int minNumber, int maxNumber) { int[] singleDigits = new int[numbers.length];
return singleDigits; }
// countNumbers counts the number of times each digit (0 to 9) appears in the given array. // Returns a new array with the counts for each digit in the corresponding array position. public static int[] countDigits(int[] numbers) { int[] frequencies = new int[10];
return frequencies; }
// printGraph displays a line of asterisks for each element in the given array. // The number of asterisks in each line, is the value of that array element. public static void printGraph(int[] frequencies) {
} }
*Most of the program is already written. A big part of this practice is to understand what the program is doing. *DO NOT make any changes to the main() method. It must stay exactly as it is. *The program compiles as-is but does not do anything because the methods are almost empty. *You are being asked to complete only 5 methods: 1)getTopTen -Expects an array of integers as a parameter. -Returns a new array with only the first 10 elements in the given array. -If the given array has less than 10 elements, the new array will have the same number of elements as the given array. 2)checkLuckyNumbers -Expects 3 parameters: an array of integers, and 2 numbers. -Searches the given array for the occurrence of the 2 given numbers side-by-side. -Returns true if it finds the 2 numbers next to each other in the array, and false otherwise. 3)replaceNumbers -Expects 3 parameters: an array of integers, and 2 numbers (a minNumber and a maxNumber). -Returns a version of the given array where all numbers that are out of range (less than minNumber or greater than maxNumber) have been replaced with 0. -The original array (passed as a parameter) does not change. 4)countDigits -Expects an array of integers as a parameter. -Counts the number of times each digit (0 to 9) appears in the given array. -Returns an array, returnArray (with length = 10), with the counts for each digit, where: --returnArray[0] has the number of occurrences of the digit 0 in the given array, --returnArray[1] has the number of occurrences of the digit 1 in the given array, --and so on... 5)printGraph -Expects an array of integers as a parameter. -For each element in the given array, print a line of asterisks. -The number of asterisks in each line, is the value of that array element. --E.g. if the name of the given array is frequencies, and frequencies[0] is 5, and frequencies[1] is 3, print: ---0 * * * * * ---1 * * *
**Important Notes: -Keep the name of your program as-is: A9.java. -Do not make any changes to main(). -Do not change the methods populateArray() and printArray(). -You must make sure your program is free of runtime errors. -Practice good programming habits: --Make your logic and your code clean and readable. --Avoid unnecessary processing and unnecessary variables. --Use meaningful variable names. --Use indentation. --Use comments to explain your logic. -I suggest working on the methods in order. --You may comment the unfinished methods (and parts of main) so you can focus on one method at a time. -Please do not use anything not covered in class (in the classroom, in the class slides, or sample code). --Do not use the java.util.Arrays library...
Sample output: ----jGRASP exec: java A9 This program will create a list of random numbers. How many random numbers would you like in your list? 45 2 11 15 13 3 15 4 11 8 7 8 5 14 9 11 3 4 13 8 4 7 15 15 9 11 6 3 8 7 5 1 8 14 3 14 6 15 15 12 14 15 15 5 11 1 ------------ 2 11 15 13 3 15 4 11 8 7 ------------ Enter 2 lucky numbers between 1 and 10: 3 4 Winner!
------------ Removing double-digit numbers from the list... 2 0 0 0 3 0 4 0 8 7 8 5 0 9 0 3 4 0 8 4 7 0 0 9 0 6 3 8 7 5 1 8 0 3 0 6 0 0 0 0 0 0 5 0 1 ------------ The original list again... 2 11 15 13 3 15 4 11 8 7 8 5 14 9 11 3 4 13 8 4 7 15 15 9 11 6 3 8 7 5 1 8 14 3 14 6 15 15 12 14 15 15 5 11 1 ------------ The number of times each digit appears is: 20 2 1 4 3 3 2 3 5 2 ------------
0 * * * * * * * * * * * * * * * * * * * * 1 * * 2 * 3 * * * * 4 * * * 5 * * * 6 * * 7 * * * 8 * * * * * 9 * * ----jGRASP: operation complete.
----jGRASP exec: java A9 This program will create a list of random numbers. How many random numbers would you like in your list? 25 3 4 14 8 15 13 1 10 2 14 1 14 9 8 4 15 15 15 6 10 3 8 12 8 3 ------------ 3 4 14 8 15 13 1 10 2 14 ------------ Enter 2 lucky numbers between 1 and 10: 4 3 Top Ten Winner!
------------ Removing double-digit numbers from the list... 3 4 0 8 0 0 1 0 2 0 1 0 9 8 4 0 0 0 6 0 3 8 0 8 3 ------------ The original list again... 3 4 14 8 15 13 1 10 2 14 1 14 9 8 4 15 15 15 6 10 3 8 12 8 3 ------------ The number of times each digit appears is: 11 2 1 3 2 0 1 0 4 1 ------------
0 * * * * * * * * * * * 1 * * 2 * 3 * * * 4 * * 5 6 * 7 8 * * * * 9 * ----jGRASP: operation complete.
----jGRASP exec: java A9 This program will create a list of random numbers. How many random numbers would you like in your list? 15 6 11 14 8 1 14 2 8 8 1 7 15 13 13 14 ------------ 6 11 14 8 1 14 2 8 8 1 ------------ Enter 2 lucky numbers between 1 and 10: 4 4 Sorry, not a winner.
------------ Removing double-digit numbers from the list... 6 0 0 8 1 0 2 8 8 1 7 0 0 0 0 ------------ The original list again... 6 11 14 8 1 14 2 8 8 1 7 15 13 13 14 ------------ The number of times each digit appears is: 7 2 1 0 0 0 1 1 3 0 ------------
0 * * * * * * * 1 * * 2 * 3 4 5 6 * 7 * 8 * * * 9 ----jGRASP: operation complete.
----jGRASP exec: java A9 This program will create a list of random numbers. How many random numbers would you like in your list? 10 9 11 13 10 13 5 11 2 5 7 ------------ 9 11 13 10 13 5 11 2 5 7 ------------ Enter 2 lucky numbers between 1 and 10: 6 10 Sorry, not a winner.
------------ Removing double-digit numbers from the list... 9 0 0 0 0 5 0 2 5 7 ------------ The original list again... 9 11 13 10 13 5 11 2 5 7 ------------ The number of times each digit appears is: 5 0 1 0 0 2 0 1 0 1 ------------
0 * * * * * 1 2 * 3 4 5 * * 6 7 * 8 9 * ----jGRASP: operation complete.
----jGRASP exec: java A9 This program will create a list of random numbers. How many random numbers would you like in your list? 5 6 14 9 3 6 ------------ 6 14 9 3 6 ------------ Enter 2 lucky numbers between 1 and 10: 6 9 Sorry, not a winner.
------------ Removing double-digit numbers from the list... 6 0 9 3 6 ------------ The original list again... 6 14 9 3 6 ------------ The number of times each digit appears is: 1 0 0 1 0 0 2 0 0 1 ------------
0 * 1 2 3 * 4 5 6 * * 7 8 9 * ----jGRASP: operation complete.
----jGRASP exec: java A9
This program will create a list of random numbers. How many random numbers would you like in your list? 0
------------
------------ Enter 2 lucky numbers between 1 and 10: 5 3 Sorry, not a winner.
------------ Removing double-digit numbers from the list...
------------ The original list again...
------------ The number of times each digit appears is: 0 0 0 0 0 0 0 0 0 0 ------------
0 1 2 3 4 5 6 7 8 9 ----jGRASP: operation complete.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
