Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a Java file named ArrayUtils.java and follow the steps below: a. (3 points) Define a method, named randomArray with two parameters: 1) an integer

Create a Java file named ArrayUtils.java and follow the steps below:

a. (3 points) Define a method, named randomArray with two parameters: 1) an integer indicating the size of the array to be created and 2) a positive integer indicating the upper limit for the range of random numbers to be generated.

If the second parameter is 101, then it would mean that the random numbers will be in the range of 0 and 100 inclusive. This method should create an array of the given size and fill it with random numbers between the given range. The function returns the array created as its return value. In your main call randomArray with two actual arguments: 100 as the size and 21 as the upper limit, and store the returned array into a local variable in main.

Example:

randomArray(5,21) returns [3,16,2,3,19]

b. (3 points)Define a method named print with one parameter of type array of integers, which outputs the elements of the given array to the standard output device, i.e., the computer screen. In your main call print with the local variable that you chose in the step above to see if the elements in the array are printed okay. You will want to use this print function whenever you want to see the elements of an array containing integers. While testing your program, include a call to this function wherever useful, but comment out all the calls to this function in the final version that you hand in.

Example:

print(new int[]{1,2,3,4}) prints [1 2 3 4 ]

c. (3 points) Define a method, named arraySum with one parameter of type array of integers, which returns the sum of all the elements in the array. In your main print the average as a double of all the numbers in the array that you obtained in the first step above. To compute the average, you must use arraySum that you defined earlier. Make sure to add annotations to the output so that it will be meaningful for people seeing the program run.

Example:

arraySum(new int[]{1, 2}) returns 3

d. (3 points) Define a method named contains with two formal parameters: 1) an array of integers and 2) a single integer. The method returns the index of the array where the first occurrence of the second argument is found, if that number is contained within the array. If the second argument is not contained in the first array argument, the method returns 1. Do not use the existing indexOf method for solving this problem, you are writing your own solution. In your main call contains with the array obtained in the first step above (the random array with 100 as the size and 21 as the upper limit) and 12 as the second argument, and prints the result.

Example:

contains(new int[]{1,2,3,4), 2) returns 1

contains(new int[]{1,2,3,4), 5) returns -1

e. (3 points) Define a method named countMultiplesOf and add it to the ArrayUtils class. The method should take two parameters: 1) an array of integers and 2) an integer. This method returns the count of the integers in the array that are multiples of the second parameter. For example, 8 is a multiple of 2, but not a multiple of 3, and zero is a multiple of any number. Hint: I suggest that you also define and use an auxiliary function that tests if a number is a multiple of another number and returns a boolean value.In your main call countMultiplesOf with the array obtained in the first step. Use 7 as the second argument and print the result.

Example:

countMultiplesOf(new int[]{1,2,3,4}, 2) returns 2 (because 2 and 4 are multiples of 2).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

The given question instructs you to create a Java class named ArrayUtils that contains various methods for working with arrays of integers Heres a breakdown of each step randomArray This method genera... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Complex Variables and Applications

Authors: James Brown, Ruel Churchill

8th edition

73051942, 978-0073051949

More Books

Students also viewed these Programming questions