CS111 Programming Assignment 5

Sorting and Searching
Exception Handling: Reading from a File

Instructor: Trish Cornez


Program 1

Task 1


Create a class named Sorter that contains the following static sorting methods.
NOTE: Do not use any sort methods provided by Java.
  1. selectionsort()
  2. quickSort()
  3. bubbleSort()
    You will need to research Bubblesort on your own. This sorting technique provides the mechanism that allows the smaller elements in the list to float to the top and the larger elements to sink to the bottom of a given list. Adjacent elements are repeatedly compared and swaps may occur until the list is sorted.

    For example, given the following unsorted array, the comparisons and swaps shown below will occur to complete the sort.
  4. Unsorted Array 34 28 43 15
    compare and swap 28 34 43 15
    compare 28 34 43 15
    compare and swap 28 34 15 43
    compare 28 34 15 43
    compare and swap 28 15 34 43
    compare 28 15 34 43
    compare and swap 15 28 34 43
    Sorted Array 15 28 34 43



Task 2

    Construct a test application that contains an array of 100000 random integers.
    Measure the performance of each sort by running this test program and using a stopwatch to
    measure how long it takes to complete the sorts.



Program 2

Construct a BinarySearch class that contains recursive binary search static method.
Test your recursive binary search on a sorted array holding 16 integers.
Display the contents of the array before and after the sort method is called.


Program 3

  1. Construct a Student class. This class must be constructed to store the student's first and last names and their intended major. Code the class so that it implements the Comparable interface. This will require you to implement the compareTo() method. Assume that comparisons are based on the length of a student's name.
    For example: "Tim" "Lee" is less than "Thomas" "Brown" because Tim Lee has only 6 letters, while Thomas Brown has 11 letters.

  2. This program requires student data to be read from a text file. Create the text file containing the following student names and their major. The student name (first and last) will appear on the line above their major.
    Notes for reading from a file and writing to a file require Exception Handling. See the video from Week 2: Video: Exception Handling Writing to a File
      James Baca
      Computer Science
      Julia Swetson
      History
      Tilda Pravishtar
      Chemistry
      James Baker
      Biology
      Roger Childs
      English Studies


  3. Complete the program so that it reads the student data located in the text file from task 1.
    NOTE: You are responsible for researching how to read from a file.
    TIP: Store Student data in an ArrayList. Remember to specify the data type as Student objects.

  4. Sort the ArrayList in increasing order so that the student with the shortest name appears at the start of the array and the students with the longest name appears at the end of the array.

    1. Example Execution :

      Data read from the file ....
      James Baca : Computer Science
      Julia Swetson : History
      Tilda Pravishtar : Chemistry
      James Baker : Biology
      Roger Childs : English Studies


      Names sorted by length (smallest to largest) ....
      James Baca : Computer Science
      James Baker : Biology
      Roger Childs : English Studies
      Julia Swetson : History
      Tilda Pravishtar : Chemistry


    Helper Java code for performing file input and output:

          // INPUT FILE AND SCANNER THAT READS THE FILE
          File inputFile;
          Scanner fileInputScan = null;
          PrintWriter out = null;
    
    
          try {
            // INSTANTIATE A FILE SCANNER AND LINK IT TO THE INPUT FILE
            // UNIX PATHWAY : "/Users/TrishCornez/Desktop/fileI.txt"
            // WINDOWS PATHWAY: "C:\\Users\\patricia_cornez\\Desktop\\fileI.txt
            inputFile = new File("C:\\Users\\patricia_cornez\\Desktop\\fileI.txt");
            fileInputScan = new Scanner(inputFile);
    
            //INSTANTIATE AN OUTPUT FILE STREAM AND LINK IT TO THE OUTPUT FILE
            out = new PrintWriter("C:\\Users\\patricia_cornez\\Desktop\\fileO.txt");
    
            // OUTPUT HELLO WORLD TO THE OUTPUT FILE
            out.print("Hello World");
    
    
          } catch (FileNotFoundException e) {
            System.out.println("Error - This file could not be found.");
          } finally {
            if (fileInputScan != null)
            fileInputScan.close();
            if (out != null)
            out.close();
          }