- To take User’s input of # of items, a minimum number, and a maximum number to create a randomized array with the length as the # of items and the randomized numbers can only be within the minimum and maximum number range then provide User with information from their choice of the main menu (Maximum, Minimum, Average, Mode, # of Even numbers, # of Odd numbers, Sample Set, # of Times a Certain Number Occurs in the Array)
- A number for the # of items in the list
- A minimum number for the list
- A maximum number for the list
- Maximum of the list
- Minimum of the list
- Average of the list
- Mode of the list
- # of even numbers in the list
- # of odd numbers in the list
- A sample set output of all the numbers in the list
- # times a certain number occurs in the list
Lines 1-6: The header for this code: name, date, purpose, inputs, and outputs.
Lines 7: Importing the Java Scanner library; it’s also more specific then importing the entire util library to reduce memory usage.
Lines 9-11: Typical statements for a Java program. Line 9 is the class name and line 10 is the main statement that Eclipse will run from. This is also the class that eclipse will run as the main driver because of the main statement.
Lines 13-15: Declaring the variables that the user will input for this program. Instantiating G to decide whether the while loop will continue or not. Creates a Scanner object called keyboard.
Line 1: Welcome message to user.
Lines 2-3: Asks user for # of items and reference that to items.
Lines 4-5: Asks user for minimum number in the array and reference that to minimum.
Lines 6-7: Asks user for maximum number in the array and reference that to maximum.
Line 8: Creates a Stats object with three instantiated variables.
Line 1: While loop runs only if g is equal to 1.
Lines 3-11: Displays the program’s menu to the user: max, min, average, mode, # of even numbers, and # of odd numbers from the array; Sample set of the entire array and how many times a number occurs in the program’s randomized array.
Line 12: Program waits for user to decide what he/she wants from the menu and then references it to choice.
Lines 1-5: If user selects choice 1: displays the maximum number in the array.
Lines 6-10: If user selects choice 2: displays the minimum number in the array.
Lines 11-15: If user selects choice 3: displays the average number in the array.
Lines 16-20: If user selects choice 4: displays the mode in the array.
Lines 21-25: If user selects choice 5: displays the # of even numbers in the array.
Lines 26-30: If user selects choice 6: displays the # of odd numbers in the array.
Lines 31-35: If user selects choice 7: displays the sample set of all the numbers in the array.
Lines 36-42: If user selects choice 8: asks user what number he/she wants to check and then displays the # of times that number has occurred in the array.
Lines 43-47: After user has made his/her decision, the program asks user if he/she wants to do something else from the menu. This prompts the program to either end or show the menu again by referencing user’s decision to g.
Lines 1-3: The program displays a closing statement to the user thanking him/her for using this program after the while loop (the program) ceases to run.
Lines 1-4: Import the java util random library to use the random object. Line 4 is the class’ name.
Lines 6-7: Creates two private integer arrays: data & modecount.
Lines 8-18: Defines the variables the are instantiated when a stats object is created: n, min, and max.
Line 10: Creates a random object called r.
Lines 11-12: Instantiates the two private arrays by using the given variables when the object is created.
Lines 13-18: Fills in the data array by using the random object and note that n is the size of the array (# of items that was predefined).
Lines 21-23: This is an integer method that returns an integer. Line 23 instantiates an integer called smallest to be the first number in the data array (data).
Lines 24-30: A for-loop that will compare the current smallest number to the next number in the array until it reaches the last number in the array (data.length). The if statement changes the number stored in smallest if the current number being compared is smaller than the current number for smallest.
Line 31: This statement basically returns the variable smallest after it has been compared to the entire array. (return has to be returning an integer for the method to be valid in the Java IDE)
Lines 34-36: This is an integer method that returns an integer. Line 36 instantiates an integer called largest to be the first number in the data array (data).
Lines 37-43: A for-loop that will compare the current largest number to the next number in the array until it reaches the last number in the array (data.length). The if statement changes the number stored in largest if the current number being compared is larger than the current number for largest.
Line 44: This statement basically returns the variable largest after it has been compared to the entire array. (return has to be returning an integer for the method to be valid in the Java IDE)
Lines 47-49: This is an double method that returns a double. Line 49 instantiates an integer called sum as 0.
Lines 50-53: A for-loop that adds all the numbers in the array to sum. It adds one number in the array at a time until it reaches the last number.
Line 54: Instantiates a double variable called avg to be the casting of sum to a double and then dividing it by the # of items in the array (finding the sum of any collection of numbers).
Line 55: This statement returns the variable avg and because it is a double, this method is valid.
Line 58: An integer method that returns the mode of the array of items.
Lines 61-63: Instantiates an array with the size of data (array) and declaring index and count as 0.
Lines 64-67: Fills in the array with zeroes to prevent a null pointer error.
Lines 68-80: Fills in the array array with the number of times each number in each index has occurred in the data array.
Line 82: Instantiates an integer called largest to be the first number in the array array.
Lines 84-104: Compares largest to each number in the array and then replacing the current number of largest to the index being compared if the number is larger. If the number is the same, then the program compares the actual number and NOT the number of times that number has occurred. The program then takes the smaller number as largest.
Line 103: Returns the number at data[array] because that will be the mode of the data array.
Line 107: An integer method that returns the total # of even numbers in the array.
Line 109: Instantiates an integer called even as 0.
Lines 110-116: A for-loop that goes through the entire data array and adds 1 to the integer even if the number being % 2 is an even number. Note that even numbers have no remainder so the logic is if there’s no remainder after dividing by 2 then the number is even.
Line 118: Returns the integer even which is the total # of even numbers in the array.
Line 121: An integer method that returns the total # of odd numbers in the array.
Line 123: Instantiates an integer called odd as 0.
Lines 124-130: A for-loop that goes through the entire data array and adds 1 to the integer odd if the number being % 2 is an odd number. Note that odd numbers have a remainder so the logic is if there’s a remainder after dividing by 2 then the number is odd.
Line 132: Returns the integer odd which is the total # of odd numbers in the array.
Line 135: A void method that returns nothing but displays all the #’s in the generated array in rows of 10 numbers.
Line 137: Instantiates k as 0.
Lines 138-150: A for-loop that will continue to print the entire generated array in rows of 10.
Lines 140-144: A if statement that uses k to determine how many numbers have already been printed on the current row. (note: this statement uses print and not println); This statement prints out a number and then leaves a tab (\t). If this if statement is to execute, then a number is to be printed along with adding 1 to the k counter.
Lines 145-149: This is an else statement that when the counter is at 9 (the 10th number), it shall print the number out and then move onto the next line (\n).
Line 154: This is an integer method that returns an the number of times a # has occurred in the generated array. It also has to be used along with a given number to match (int match).
Lines 156-159: This for-loop fills in the modecount array with # of times each number in data array has occurred in the array. Note that the modecount array size is max+1 and not the same size as data array because there will be a nullpointer error because there has to be an index of the modecount array as the largest possible generated number.
Line 161: Returns the number of times the user designated # occurred in data array. Note how the number being designated can be the maximum number which makes the size of data array the incorrect size because data array size depends on the # of items and not the maximum possible number generated (an index that must be accounted for).