Problem

(Execution time) Write a program that randomly generates an array of 100,000 integers and...

(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 1. Sort the array and estimate the execution time of invoking the binarySearch method in Listing 2. 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;

LISTING 1 TestDoWhile.java

1 import java.util.Scanner;

2

3 public class TestDoWhile {

4 /** Main method */

5 public static void main(String[] args) {

6 int data;

7 int sum = 0;

8

9 // Create a Scanner

10 Scanner input = new Scanner(System.in);

11

12 // Keep reading data until the input is 0

13 do {

14 // Read the next data

15 System.out.print(

16 "Enter an integer (the input ends if it is 0): ");

17 data = input.nextInt();

18

19 sum += data;

20 } while (data != 0);

21

22 System.out.println("The sum is " + sum);

23 }

24 }

LISTING 2 MultiplicationTable.java

1 public class MultiplicationTable {

2 /** Main method */

3 public static void main(String[] args) {

4 // Display the table heading

5 System.out.println(" Multiplication Table");

6

7 // Display the number title

8 System.out.print(" ");

9 for (int j = 1; j <= 9; j++)

10 System.out.print(" " + j);

11

12 System.out.println("\n———————————————————————————————————————");

13

14 // Display table body

15 for (int i = 1; i <= 9; i++) {

16 System.out.print(i + " | ");

17 for (int j = 1; j <= 9; j++) {

18 // Display the product and align properly

19 System.out.printf("%4d", i * j);

20 }

21 System.out.println();

22 }

23 }

24 }

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT