225k views
3 votes
1. Change the method getRandomNumberArray. (max 5 points)

It should return an array that includes n uniformly random, odd numbers that are greater or equal to 10 million and less than 500 million.

§ The array should include exactly n random numbers (1 point)

§ The array should include only odd numbers. (2 points)

§ The array should include only numbers greater or equal to 10 million (1 point)

§ The array should include only numbers less than 500 million. (1 point)

FYI: The array may include duplicates.

2. Change the method measureTime (max 20 points)

§ Rather than sorting numbers, it should identify the 10 smallest numbers and store them in a new smaller array.
To do so, create a new array bottomTen of size 10. Then fill it with the 10 smallest numbers from the array numbers. (7 points)
The smallest number should be on index 0, the second-smallest number on index 1, etc.
If the array numbers gets modified in the process, that's okay.

§ Measure the time it takes to identify the 10 smallest numbers and to store them in the array bottomTen. (2 points)
Do not measure the time it takes to create the array.

§ Perform a series of time measurements to determine the time complexity of the task (identifying the 10 smallest numbers and storing them in the array) (7 points)

§ Add a block comment at the end of the method as described below. (4 points)
It should state which of the following two categories best describes the time complexity of the task. (identifying the 5 largest numbers and storing them in the array)
category A) quadratic time complexity // O(N²)
category B) linear or linearithmic time complexity // O(N) or O(N log N)
The block comment should also include a description of how the series of time measurements allowed you to determine the category.

3. Add a Comparator BY_NAME (max 13 points)

§ Add a comparator to class Friend. (7 points)
It should be implemented as a nested private class.

§ Also, add a public static final field BY_NAME of type Comparator to class Friend. (3 points)
It should allow other classes to access the comparator.

4. Change the method displayFriends (max 7 points)

§ It should print the friends sorted by name. (1 point)
The output should match the format used before.

§ Add a third section with the header "Friends sorted by name:"
Sort the array friends by name. Within a given age group, the friends should remain sorted by age. (5 points)
To do so, choose one of the following algs4 sorting classes: Heap, Insertion, Merge, Quick, Selection.
Print the newly sorted friends again.

User Jon Barker
by
8.5k points

1 Answer

3 votes

To change the method getRandomNumberArray, you can create an array and use a loop to generate random odd numbers within the desired range. For measureTime, create a new array and use a sorting algorithm to find the 10 smallest numbers. Add a nested private class to implement the BY_NAME comparator and use a suitable sorting algorithm to display friends sorted by name.

To change the method getRandomNumberArray to return an array of n uniformly random, odd numbers between 10 million and 500 million, you can follow these steps:

Create an empty array called randomArray

Use a loop to generate n random numbers

Check if each number is odd and falls within the desired range

If the number meets the criteria, add it to the randomArray

Repeat the loop until you have n odd numbers in the randomArray

For the method measureTime, you can implement the following steps:

Create an empty array called bottomTen with a size of 10

Use a sorting algorithm to sort the numbers array

Copy the first 10 smallest numbers from the sorted array to the bottomTen array

Measure the time it takes to perform the above steps

Perform multiple time measurements with different input sizes to determine the time complexity

Add a block comment at the end of the method stating the appropriate time complexity category based on the measurements

Regarding the Comparator BY_NAME, you can:

Add a nested private class named BY_NAME inside the Friend class to implement the comparator

Add a public static final field called BY_NAME of type Comparator to allow other classes to access the comparator

In the method displayFriends, to print the friends sorted by name, you can:

Add a third section with the header Friends sorted by name:

Use a suitable sorting algorithm like Merge or Quick sort to sort the friends array by name

Print the sorted friends array in the desired format

User David Arango
by
7.9k points