Assignments Help

Programming Exercise 7.18

**7.18 (Bubble sort) Write a sort method that uses the bubble-sort algorithm. The bubblesort 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…

Read More

Programming Exercise 7.17

**7.17 (Sort students) Write a program that prompts the user to enter the number of students, the students’ names, and their scores, and prints student names in decreasing order of their scores.

Read More

Programming Exercise 7.16

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 invoking the binarySearch method in Listing 7.7. You can use the following code template to obtain the…

Read More

Programming Exercise 7.23

**7.23 (Game: locker puzzle) A school has 100 lockers and 100 students. All lockers are closed on the first day of school. As the students enter, the first student, denoted S1, opens every locker. Then the second student, S2, begins with the second locker, denoted L2, and closes every other locker. Student S3 begins with…

Read More

Programming Exercise 7.15

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…

Read More

Programming Exercise 7.14

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.

Read More

Programming Exercise 7.13

*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)

Read More

Programming Exercise 7.12

*7.12 (Reverse an array) The reverse method in Section 7.7 reverses an array by copying it to a new array. Rewrite the method that reverses the array passed in the argument and returns this array. Write a test program that prompts the user to enter ten numbers, invokes the method to reverse the numbers, and…

Read More

Programming Exercise 7.11

*7.11 (Statistics: compute deviation) Programming Exercise 5.45 computes the standard deviation of numbers. This exercise uses a different but equivalent formula to compute the standard deviation of n numbers. [mean = frac{sum_{i=1}^nx_{i}}{n} = frac{x_{1} + x_{2} + ...+x_{n}}{n}] [deviation =sqrt{frac{sum_{i=1}^n(x_{i}-mean)^2}{n-1}}] To compute the standard deviation with this formula, you have to store the individual numbers…

Read More

Programming Exercise 7.10

7.10 (Find the index of the smallest element) Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: public static int indexOfSmallestElement(double[] array) Write a test program that prompts the user…

Read More

Programming Exercise 7.9

7.9 (Find the smallest element) Write a method that finds the smallest element in an array of double values using the following header: public static double min(double[] array) Write a test program that prompts the user to enter ten numbers, invokes this method to return the minimum value, and displays the minimum value. Here is…

Read More

Programming Exercise 7.8

7.8 (Average an array) Write two overloaded methods that return the average of an array with the following headers: public static int average(int[] array) public static double average(double[] array) Write a test program that prompts the user to enter ten double values, invokes this method, and displays the average value.

Read More

Programming Exercise 7.7

*7.7 (Count single digits) Write a program that generates 100 random integers between 0 and 9 and displays the count for each number. (Hint: Use an array of ten integers, say counts, to store the counts for the number of 0s, 1s, . . . , 9s.)

Read More

Programming Exercise 7.6

*7.6 (Revise Listing 5.15, PrimeNumber.java) Listing 5.15 determines whether a number n is prime by checking whether 2, 3, 4, 5, 6, . . . , n/2 is a divisor. If a divisor is found, n is not prime. A more efficient approach is to check whether any of the prime numbers less than or…

Read More

Programming Exercise 7.5

**7.5 (Print distinct numbers) Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If…

Read More