Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Homework 3 (Methods and Arrays)

1. (14 points) EliminateDuplicates.java: Write the following method that returns a copy of the argument str after eliminating all duplicate characters. The first occurrence of each character      must be kept and the characters must stay in order relative to the other letters in the string:

public static String eliminateDuplicates(String str)

For example, if str is bzzaaf” then the return value should be “bzaf” and not something else like “abfz” or zfba” . The str parameter can contain any printable ASCII characters or spaces, but not tabs or newlines. Include the following method calls in your main program and add two more      method calls with your selected strings.

Example test cases:

eliminateDuplicates("abracadabra") should return abrcd                                   eliminateDuplicates("Stony Brook University") should return Stony BrkUives eliminateDuplicates("This is a test sentence.") should return This atenc.

2. (14 points) InterleaveArrays.java: Write the following method that takes in two arrays of integers. Create and return a new array by interleaving the array together. Once an array is       empty, use all the elements from the remaining array. You may assume that the numbers in the arrays are unique and there are no duplicate values.

public static int[] interleaveArrays(int[] array1, int[] array2)

Include the following method calls in your main program and add two more method calls with your selected arrays.

interleaveArrays(new int[]{1,3,5}, new int[]{2,4,6}) should return [1, 2, 3, 4, 5, 6]

interleaveArrays(new int[]{10,20,30,40,50,60,70,80}, new int[]{2,4,6,8}) should return [10, 2, 20, 4, 30, 6, 40, 8, 50, 60, 70, 80]

interleaveArrays(new int[]{1,3,5}, new int[]{2,4,6,8,10}) should return [1, 2, 3, 4, 5, 6, 8, 10]

3. (65 points) ArrayUtils.java: Create a Java file named ArrayUtils that has a main program and includes the following methods:

a. (13 points) Define a method, named randomArray with two parameters:

1) an integer indicating the size of the array to be created and

2) an 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 ofthe given size and fill it with          random numbers between the given range and return 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.

b. (13 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. To test your work, call print in your main program with the local variable that you        chose in part (a) to see if the elements in the array are printed correctly. You will want to use this print method 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.

c. (13 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 program, print the average of all the numbers in the array that you obtained in part (a). To compute the average, you must use  arraySum and divide by the number of elements in the array. Make sure to add annotations to    the output so that it will be meaningful for people seeing the program run.

d. (13 points) Define a method named contains with two parameters:

1) an array of integers and

2) a single integer named target.

The method returns the index of the array where the first occurrence of the target is found, if that number is contained within the array. If the target 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. Call contains in your main program with the array obtained in the  part (a) and 12 as the target, and print the result.

e. (13 points) Define a method named countMultiplesOf with 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 method that tests if a number is a            multiple of another number and returns a boolean value. Call countMultiplesOf in your main program with the array obtained in part (a). Use 7 as the second argument and print the result.