Java Reference
In-Depth Information
enter ten numbers, invokes the method to reverse the numbers, and displays the
numbers.
Section 7.9
*7.13
(
Random number chooser
) Write a method that returns a random number between
1 and 54, excluding the numbers passed in the argument. The method header is
specified as follows:
public static int
getRandom(
int
... numbers)
7.14
(
Computing gcd
) Write a method that returns the gcd of an unspecified number
of integers. The method header is specified as follows:
public static int
gcd(
int
... numbers)
Write a test program that prompts the user to enter five numbers, invokes the
method to find the gcd of these numbers, and displays the gcd.
Sections 7.10-7.12
7.15
(
Eliminate duplicates
) Write a method that returns a new array by eliminating the
duplicate values in the array using the following method header:
public static int
[] eliminateDuplicates(
int
[] list)
Write a test program that reads in ten integers, invokes the method, and displays
the result. Here is the sample run of the program:
Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
The distinct numbers are: 1 2 3 6 4 5
7.16
(
Execution time
) Write a program that randomly generates an array of 100,000
integers and a key. Estimate the execution time of invoking the
linearSearch
method in Listing 7.6. Sort the array and estimate the execution time of invok-
ing the
binarySearch
method in Listing 7.7. You can use the following code
template to obtain the execution time:
long
startTime = System.currentTimeMillis();
perform the task;
long
endTime = System.currentTimeMillis();
long
executionTime = endTime - startTime;
**7.17
(
Sort students
) Write a program that prompts the user to enter the number of stu-
dents, the students' names, and their scores, and prints student names in decreas-
ing order of their scores.
**7.18
(
Bubble sort
) Write a sort method that uses the bubble-sort algorithm. The bubble-
sort algorithm makes several passes through the array. On each pass, successive
neighboring pairs are compared. If a pair is not in order, its values are swapped;
otherwise, the values remain unchanged. The technique is called a
bubble sort
or
sinking sort
because the smaller values gradually “bubble” their way to the top
and the larger values “sink” to the bottom. Write a test program that reads in ten
double numbers, invokes the method, and displays the sorted numbers.
Search WWH ::
Custom Search