Your program will create an array of random int numbers, search the array to see if any…
- User
Your program will create an array of random int numbers, search the array to see if any two elements add to a specific number, and display the results. The size of the array and the specific number to be searched for are defined by user input (and hence, can vary on each run of the program).
Your code should comply with standard Java style guidelines (such as those established in Chapter 1.2 of the textbook and demonstrated in class throughout the term). Please read the question fully; it is recommended that you take notes of key points as you read.
The functionality of your program should include the following behaviors:
Welcome the user to the program and give a brief explanation of what to expect or the purpose of the program. Prompt the user for a positive whole number; if a non-integer, zero, or negative number is entered prompt repeatedly until a positive whole number is entered. Once a number is properly entered, create an array of that number of int values and give each element of the array a random value between 1 and 20. Display the array contents to the user. Prompt the user for a target number they wish to search for as the sum of any two elements of the array. If a non-integer is entered prompt repeatedly until a whole number is entered. If the target number is less than 2 or greater than 40 (and, hence, cannot be the sum of any two numbers in any generated random array), tell the user that the target number cannot possibly be in the array. If the target number is from 2 to 40, search the array, and indicate if the target sum was found anywhere in the array or not. If the target sum was found display the array indexes of the numbers that formed the sum (see examples below). Example output:
Welcome to my program! It creates a random number array and checks to see if any two elements add to a given target!
Enter the size for the array of random numbers: 19
What sum of any two array elements shall I search for? 4
Here's the array: [1, 14, 19, 16, 18, 7, 9, 16, 20, 20, 12, 10, 3, 1, 4, 6, 3, 12, 9]
Elements 0 and 12 add to 4.
Another example output:
Welcome to my program! It creates a random number array and checks to see if any two elements add to a given target!
Enter the size for the array of random numbers: 6
What sum of any two array elements shall I search for? 13
Here's the array: [7, 9, 7, 9, 11, 8]
No pair of elements add to 13.
And one more example output:
Welcome to my program! It creates a random number array and checks to see if any two elements add to a given target!
Enter the size for the array of random numbers: fish
Invalid input, try again.
Enter the size for the array of random numbers: 0
Invalid input, must be 1 or more, try again.
Enter the size for the array of random numbers: do not care
- Assistant
- User
use simpler code and avoid builder.append
- Assistant