CSCI-1320 Lab: Strings and Arrays II

Instructor: Todd Dole

Purpose Statement

This assignment gives further practice manipulating arrays, including arrays of strings and sorting. You will have two weeks to complete this assignment.

Instructions

Create a class called EmployeeData.

Download the employee data file and place it in the folder with your code. File here: employee_data.csv

Create a method called loadData, which reads the employee data from the file. You will need to use split to separate the data and place it into a series of arrays (employeeIds, employeeNames, etc.)

Then, create the following methods: (You will also create a main method as described below.)

(Hint: Before you begin writing each method, think through your approach and jot down a plan. What steps do you need to take?)

  1. public static void printReport()
    Iterate through the EmployeeData variables and print a report, that looks similar to the following:
                     Employee ID   Name                  Address                                                         Phone
                     ===========   ===================   =============================================================   ============
                              13   John Doe              1234 Mockingbird Lane, Abilene, TX 79605                        325-555-1234
                              ... (more data here)
    Hint: You can use printf with codes like %10d to help with formatting. I have also included a helper class that will pad a string with spaces to the right to help with the formatting.
  2. public static int[] sortedIds()

    returns a copy of employeeIds with the numbers in sorted order

    Hint #1: You can create a fresh copy of employeeId with the following code:

    int[] copiedArray = originalArray.clone();
    (Think about why this is necessary -- what happens to your report if you sort employee Id's but leave names, addresses and phone numbers unchanged?)

    Here is a helpful reference page on selection sort: https://www.geeksforgeeks.org/selection-sort-algorithm-2/

  3. public static void sortEmployeeData()
    Use selection sort to sort all the employee data by employee Id. Note that we are now sorting the original variables rather than making copies.

    Hint: Every time you swap two employee IDs, you will also need to swap the corresponding Name, Address and Phone Number so that all the data stays together.

  4. main method: Write a main method that will test each of these:
    1. First, call loadData() to load all the data into a series of arrays.
    2. Next, call printReport() to print out an inital copy of the report
    3. Third, call sortedIds() and print out the results.
    4. Finally, call sortEmployeeData() and then call printReport() again to print out the (now sorted) results.

Submit your working .java file through Canvas.

Copy and paste into your EmployeeData class:


            /***
            * rightPad(String start, int total_length) returns a copy of start with spaces added to the end, to make the total length
            * of the string equal to total_length.
            **/
            public static String rightPad(String start, int total_length) {
                String newString = new String(start);
                for (int i=start.length(); i < total_length; i++) newString+=" ";
                return newString;
            }