Java (50 points) Sorting Create 3 text files, one with the 50000 integers 1 thro
ID: 3887683 • Letter: J
Question
Java (50 points) Sorting Create 3 text files, one with the 50000 integers 1 through 50000 in order, one with the 50000 integers 1 through 50000 in reverse order, and one with 50000 random integers (between 1 and 50000). Write a program to Read the name of a file Store the contents of the file into a 50000 element array, Sort the array using an Insertion Sort, and Print the actual running time (without any server overhead) of the sort. Print the number of comparisons used in the sort. Print just the first 30 values in the resulting array Turn in a printed copy of the program, along with a printout of the output from three separate runs (using the three text files). Extra Credit (20 points) Implement the Bag class for a bag of integers as described in the file Chap02 Bag Class.ppt posted in the folder Lecture Notes on the class website. Also create a driver class similar to that for the Die class above to show that the Bag class works.
Explanation / Answer
Random random = new Random();
for(int i =0; i<5; i++){
int randomInteger = random.nextInt();
System.out.println("Random Integer in Java: " + randomInteger);
}
Output:
Random Integer in Java: -1194538938
Random Integer in Java: -973476571
Random Integer in Java: -1283186469
Random Integer in Java: 1176677327
Random Integer in Java: 265551019
You can see java.util.Random by default generates random numbers in with range of integers in Java which is -2^31 to 2^31-1, which consists both positive and negative integers. By the way, you can also use Math.random() to create random numbers, here is an example of generating random numbers using Math.random() in Java:
for(int i =0; i<3; i++){
double randomDouble = Math.random();
System.out.println("Random Number in Java: " + randomDouble);
}
Output:
Random Number in Java: 0.767752638941705
Random Number in Java: 0.482517390182052
Random Number in Java: 0.28243911879792283
Random random = new Random();
for(int i =0; i<5; i++){
int randomInteger = random.nextInt();
System.out.println("Random Integer in Java: " + randomInteger);
}
Output:
Random Integer in Java: -1194538938
Random Integer in Java: -973476571
Random Integer in Java: -1283186469
Random Integer in Java: 1176677327
Random Integer in Java: 265551019
You can see java.util.Random by default generates random numbers in with range of integers in Java which is -2^31 to 2^31-1, which consists both positive and negative integers. By the way, you can also use Math.random() to create random numbers, here is an example of generating random numbers using Math.random() in Java:
for(int i =0; i<3; i++){
double randomDouble = Math.random();
System.out.println("Random Number in Java: " + randomDouble);
}
Output:
Random Number in Java: 0.767752638941705
Random Number in Java: 0.482517390182052
Random Number in Java: 0.28243911879792283
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.