Write a class that completes the following tasks: In your main () method, prompt
ID: 3657205 • Letter: W
Question
Write a class that completes the following tasks: In your main () method, prompt the user to enter a positive integer. Use this integer to create an array of integers of that size and use a for-loop to initialize it with random numbers between 0 and 1000. Display your array ONLY if the user-entered integer is less than 15. Otherwise display a message that says "array too large to display". If you use a method for displaying your array, make sure that this if-statement is in the main () method as your array display method should work for any-size array. Write a method with the following header: public static void bubbleSort(int[] array) this method uses the bubble sort algorithm (see slides) to sort the array. Call your bubble sort method from main ( ) to sort your array and display it after sorting. Write a method with the following header: public static int binarySearch (int[] array, int value) This methods uses the binary search algorithm to search through a sorted array for a value. Your method returns the index of the first occurrence of the value if it is found or -1 if it is not found. Because we want to see how many searches the algorithm uses to find (or not find) the value, you have my permission :P to print (System, out .println ( )) at the end of your method (before returning) the number of values that were searched. Use the non-recursive version (see the slides) instead of the recursive version in the book. If you use the recursive version from the book you are responsible for figuring out how to maintain the counter variable. In your main method prompt the user for a value to search for (-1 to quit) in the sorted array. Print out the index (which is returned from the method) in addition to the number of searches performed (which actually is printed inside of the method).Explanation / Answer
import java.io.*; public class ReadFile{ public static void main(String[] args){ try { /* Sets up a file reader to read the file passed on the command line one character at a time */ FileReader input = new FileReader(args[0]); /* Filter FileReader through a Buffered read to read a line at a time */ BufferedReader bufRead = new BufferedReader(input); String line; // String that holds current file line int count = 0; // Line number of count // Read first line line = bufRead.readLine(); count++; // Read through file one line at time. Print line # and line while (line != null){ System.out.println(count+": "+line); line = bufRead.readLine(); count++; } bufRead.close(); }catch (ArrayIndexOutOfBoundsException e){ /* If no file was passed on the command line, this expception is generated. A message indicating how to the class should be called is displayed */ System.out.println("Usage: java ReadFile filename "); }catch (IOException e){ // If another exception is generated, print a stack trace e.printStackTrace(); } }// end main }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.