Designing Java Programs using 1-Dimensional Arrays and Files

We shall complete the following exercises in order to improve our skills to:

  1. Design methods to process text/character files

In addition, we review a few concepts related to 1-dimensional arrays:

  1. Design methods to sort a single-dimensional array
  2. Design methods to sort parallel arrays
  3. Design methods to binary search in sorted arrays

Most of these exercises are not on CodeLab, and you should practice these on your own. However, make sure you complete the exercises on CodeLab.

Exercises 1 - 3. Using Arrays to Process Files

(Exercise 1) Write the following Java method that returns the number of lines in the given file.

public static int getNumberOfLines(String inFilePath) throws FileNotFoundException

(Exercise 2) Write the following Java method that returns the all lines in the given file.

public static String[] getAllLines(String inFilePath) throws FileNotFoundException

(Exercise 3) Write a Java program that uses the above methods, convert all letters from lower case to upper case if any. A user would use the program in the fashion exhibited by the example below:

java UpperToLower input.txt output.txt

You should complete these exercises on CodeLab.

Exercises 4 - 6. Comparing three sorting algorithms

Implement three sorting algorithms, each in a Java method. These methods are:

  1. (Exercise 4) Selection Sort
    public static void selectionSort(int[] arr)
    
  2. (Exercise 2) Bubble Sort
    public static void bubbleSort(int[] arr)
    
  3. (Exercise 3) Insertion Sort
    public static void insertionSort(int[] arr)
    

Exercises 7 - 9. Sorting Parallel Arrays (Parallel Sort)

Implement three sorting algorithms, each in a Java method. These methods are:

  1. (Exercise 7) Selection Sort of Parallel Arrays
    public static void parallelSelectionSort(int[] dataArr, int[] keyArr)
    
  2. (Exercise 8) Bubble Sort of Parallel Arrays
    public static void parallelBubbleSort(int[] dataArr, int[] keyArr)
    
  3. (Exercise 9) Insertion Sort of Parallel Arrays
    public static void parallelInsertionSort(int[] dataArr, int[] keyArr)
    

(Exercise 10) Implement the binary search algorithm. The array returns the index of key if it is found. If it is not, return -(insertion point+1). If the algorithm is implemented correctly, the low index is the insertion point.

public static int binarySearch(int[] arr, int key)

(Exercise 11) Implement the linear search algorithm. The array returns the index of key if it is found; otherwise, -1.

public static int linearSearch(int[] arr, int key)

(Exercise 12) Additionally, write a program that generate a large random arrays, and compare elapsed times of the two search algorithms.